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

import at.procon.dip.architecture.SchemaNames;
import at.procon.dip.domain.access.DocumentAccessContext;
import at.procon.dip.domain.access.DocumentVisibility;
import at.procon.dip.domain.document.CanonicalDocumentMetadata;
import at.procon.dip.domain.document.DocumentFamily;
import at.procon.dip.domain.document.DocumentStatus;
import at.procon.dip.domain.document.DocumentType;
import at.procon.dip.domain.tenant.entity.DocumentTenant;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Index;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
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;

/**
 * Canonical document root entity for the generalized DOC schema.
 */
@Entity
@Table(schema = SchemaNames.DOC, name = "doc_document", indexes = {
        @Index(name = "idx_doc_document_type", columnList = "document_type"),
        @Index(name = "idx_doc_document_family", columnList = "document_family"),
        @Index(name = "idx_doc_document_status", columnList = "status"),
        @Index(name = "idx_doc_document_visibility", columnList = "visibility"),
        @Index(name = "idx_doc_document_owner_tenant", columnList = "owner_tenant_id"),
        @Index(name = "idx_doc_document_dedup_hash", columnList = "dedup_hash"),
        @Index(name = "idx_doc_document_business_key", columnList = "business_key"),
        @Index(name = "idx_doc_document_created_at", columnList = "created_at")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Document {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "owner_tenant_id")
    private DocumentTenant ownerTenant;

    @Enumerated(EnumType.STRING)
    @Column(name = "visibility", nullable = false, length = 32)
    @Builder.Default
    private DocumentVisibility visibility = DocumentVisibility.PUBLIC;

    @Enumerated(EnumType.STRING)
    @Column(name = "document_type", nullable = false, length = 64)
    private DocumentType documentType;

    @Enumerated(EnumType.STRING)
    @Column(name = "document_family", nullable = false, length = 64)
    private DocumentFamily documentFamily;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 32)
    @Builder.Default
    private DocumentStatus status = DocumentStatus.RECEIVED;

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

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

    @Column(name = "language_code", length = 16)
    private String languageCode;

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

    @Column(name = "business_key", length = 255)
    private String businessKey;

    @Column(name = "dedup_hash", length = 64)
    private String dedupHash;

    @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();
    }

    public CanonicalDocumentMetadata toCanonicalMetadata() {
        return new CanonicalDocumentMetadata(
                id,
                new DocumentAccessContext(ownerTenant == null ? null : new at.procon.dip.domain.tenant.TenantRef(
                        ownerTenant.getId().toString(), ownerTenant.getTenantKey(), ownerTenant.getDisplayName()), visibility),
                documentType,
                documentFamily,
                status,
                title,
                languageCode,
                mimeType,
                dedupHash,
                createdAt,
                updatedAt
        );
    }
}
