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

import at.procon.dip.architecture.SchemaNames;
import at.procon.dip.domain.document.EmbeddingStatus;
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 jakarta.persistence.Transient;
import java.time.OffsetDateTime;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * Generic vectorization record separated from the canonical document structure.
 * <p>
 * The actual pgvector payload is persisted in the {@code embedding_vector} column via native SQL
 * in later phases. The transient field exists only as a convenient in-memory carrier.
 */
@Entity
@Table(schema = SchemaNames.DOC, name = "doc_embedding", indexes = {
        @Index(name = "idx_doc_embedding_document", columnList = "document_id"),
        @Index(name = "idx_doc_embedding_repr", columnList = "representation_id"),
        @Index(name = "idx_doc_embedding_model", columnList = "model_id"),
        @Index(name = "idx_doc_embedding_status", columnList = "embedding_status"),
        @Index(name = "idx_doc_embedding_embedded_at", columnList = "embedded_at"),
        @Index(name = "idx_doc_embedding_prefix_profile", columnList = "prefix_profile_id")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DocumentEmbedding {

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

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "document_id", nullable = false)
    private Document document;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "representation_id", nullable = false)
    private DocumentTextRepresentation representation;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "model_id", nullable = false)
    private DocumentEmbeddingModel model;

    @Enumerated(EnumType.STRING)
    @Column(name = "embedding_status", nullable = false, length = 32)
    @Builder.Default
    private EmbeddingStatus embeddingStatus = EmbeddingStatus.PENDING;

    @Column(name = "token_count")
    private Integer tokenCount;

    @Column(name = "embedding_dimensions")
    private Integer embeddingDimensions;

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

    @Column(name = "embedded_at")
    private OffsetDateTime embeddedAt;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "prefix_profile_id")
    private DocumentEmbeddingPrefixProfile prefixProfile;


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

    @Transient
    private float[] embeddingVector;

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

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