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

import at.procon.dip.architecture.SchemaNames;
import at.procon.dip.domain.document.DistanceMetric;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
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;

/**
 * Embedding model catalog row used by generic vectorization.
 */
@Entity
@Table(schema = SchemaNames.DOC, name = "doc_embedding_model", indexes = {
        @Index(name = "idx_doc_embedding_model_key", columnList = "model_key", unique = true),
        @Index(name = "idx_doc_embedding_model_active", columnList = "active")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DocumentEmbeddingModel {

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

    @Column(name = "model_key", nullable = false, unique = true, length = 255)
    private String modelKey;

    @Column(name = "provider", nullable = false, length = 120)
    private String provider;

    @Column(name = "display_name", length = 255)
    private String displayName;

    @Column(name = "dimensions", nullable = false)
    private Integer dimensions;

    @Enumerated(EnumType.STRING)
    @Column(name = "distance_metric", nullable = false, length = 32)
    @Builder.Default
    private DistanceMetric distanceMetric = DistanceMetric.COSINE;

    @Builder.Default
    @Column(name = "query_prefix_required", nullable = false)
    private boolean queryPrefixRequired = false;

    @Builder.Default
    @Column(name = "active", nullable = false)
    private boolean active = true;

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