package at.procon.dip.domain.document.entity;

import at.procon.dip.architecture.SchemaNames;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import jakarta.persistence.Table;
import java.time.OffsetDateTime;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(schema = SchemaNames.DOC, name = "doc_mail_attachment", indexes = {
        @Index(name = "idx_doc_mail_attachment_mail", columnList = "mail_document_id"),
        @Index(name = "idx_doc_mail_attachment_document", columnList = "attachment_document_id"),
        @Index(name = "idx_doc_mail_attachment_part_path", columnList = "part_path"),
        @Index(name = "idx_doc_mail_attachment_attachment_index", columnList = "attachment_index")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DocumentMailAttachment {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    @Column(name = "mail_document_id", nullable = false)
    private UUID mailDocumentId;

    @Column(name = "attachment_document_id", nullable = false)
    private UUID attachmentDocumentId;

    @Column(name = "disposition", length = 32)
    private String disposition;

    @Column(name = "content_id", length = 500)
    private String contentId;

    @Column(name = "filename", length = 1000)
    private String filename;

    @Column(name = "mime_type", length = 255)
    private String mimeType;

    @Column(name = "size_bytes")
    private Long sizeBytes;

    @Column(name = "attachment_index")
    private Integer attachmentIndex;

    @Column(name = "part_path", length = 500)
    private String partPath;

    @Column(name = "path_in_archive", columnDefinition = "TEXT")
    private String pathInArchive;

    @Column(name = "extraction_status", nullable = false, length = 32)
    @Builder.Default
    private String extractionStatus = "IMPORTED";

    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    @Builder.Default
    @Column(name = "created_at", nullable = false, updatable = false)
    private OffsetDateTime createdAt = OffsetDateTime.now();

    @Builder.Default
    @Column(name = "updated_at", nullable = false)
    private OffsetDateTime updatedAt = OffsetDateTime.now();

    @PrePersist
    protected void onCreate() {
        createdAt = OffsetDateTime.now();
        updatedAt = OffsetDateTime.now();
    }

    @PreUpdate
    protected void onUpdate() {
        updatedAt = OffsetDateTime.now();
    }
}
