package at.procon.dip.clustering.entity;

import at.procon.dip.architecture.SchemaNames;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
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.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", indexes = {
        @Index(name = "idx_doc_cluster_cluster_run_jpa", columnList = "cluster_run_id")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class EmbeddingCluster {

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

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "cluster_run_id", nullable = false)
    private EmbeddingClusterRun clusterRun;

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

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

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

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

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

    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "top_terms_json", columnDefinition = "jsonb")
    private String topTermsJson;

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

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