package at.procon.dip.clustering.entity;

import at.procon.dip.architecture.SchemaNames;
import at.procon.dip.clustering.ClusterRunStatus;
import at.procon.dip.clustering.ClusteringAlgorithm;
import at.procon.dip.clustering.ClusteringExecutionBackend;
import at.procon.dip.clustering.ReductionMethod;
import at.procon.dip.domain.document.DocumentFamily;
import at.procon.dip.domain.document.DocumentType;
import at.procon.dip.domain.document.RepresentationType;
import at.procon.dip.domain.document.entity.DocumentEmbeddingModel;
import at.procon.dip.domain.document.entity.DocumentEmbeddingPrefixProfile;
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;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;

@Entity
@Table(schema = SchemaNames.DOC, name = "doc_embedding_cluster_run", indexes = {
        @Index(name = "idx_doc_cluster_run_status_jpa", columnList = "status"),
        @Index(name = "idx_doc_cluster_run_algorithm_jpa", columnList = "algorithm"),
        @Index(name = "idx_doc_cluster_run_created_at_jpa", columnList = "created_at")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class EmbeddingClusterRun {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cluster_set_id")
    private EmbeddingClusterSet clusterSet;

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

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 32)
    private ClusterRunStatus status;

    @Enumerated(EnumType.STRING)
    @Column(name = "algorithm", nullable = false, length = 64)
    private ClusteringAlgorithm algorithm;

    @Column(name = "algorithm_version", length = 64)
    private String algorithmVersion;

    @Enumerated(EnumType.STRING)
    @Column(name = "execution_backend", length = 64)
    private ClusteringExecutionBackend executionBackend;

    @Enumerated(EnumType.STRING)
    @Column(name = "reduction_method", length = 32)
    private ReductionMethod reductionMethod;

    @Column(name = "reduction_dimensions")
    private Integer reductionDimensions;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "selection_json", nullable = false, columnDefinition = "jsonb")
    private String selectionJson;

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "parameters_json", nullable = false, columnDefinition = "jsonb")
    private String parametersJson;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "embedding_model_id")
    private DocumentEmbeddingModel embeddingModel;

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

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

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

    @Enumerated(EnumType.STRING)
    @Column(name = "representation_type", length = 64)
    private RepresentationType representationType;

    @Column(name = "builder_key", length = 255)
    private String builderKey;

    @Column(name = "item_count")
    private Long itemCount;

    @Column(name = "cluster_count")
    private Long clusterCount;

    @Column(name = "noise_count")
    private Long noiseCount;

    @Column(name = "started_at")
    private OffsetDateTime startedAt;

    @Column(name = "finished_at")
    private OffsetDateTime finishedAt;

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

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

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

    @PreUpdate
    protected void onUpdate() {
        if (status == null) {
            status = ClusterRunStatus.CREATED;
        }
    }
}
