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

import at.procon.dip.architecture.SchemaNames;
import at.procon.dip.embedding.model.EmbeddingPrefixMode;
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;

@Entity
@Table(schema = SchemaNames.DOC, name = "doc_embedding_prefix_profile", indexes = {
        @Index(name = "idx_doc_embedding_prefix_profile_code", columnList = "code", unique = true),
        @Index(name = "idx_doc_embedding_prefix_profile_mode", columnList = "prefix_mode")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DocumentEmbeddingPrefixProfile {

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

    @Column(name = "code", nullable = false, length = 128, unique = true)
    private String code;

    @Enumerated(EnumType.STRING)
    @Column(name = "prefix_mode", nullable = false, length = 32)
    private EmbeddingPrefixMode prefixMode;

    @Column(name = "prefix_text", nullable = false, columnDefinition = "TEXT")
    @Builder.Default
    private String prefixText = "";

    @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();
        if (prefixText == null) {
            prefixText = "";
        }
    }

    @PreUpdate
    protected void onUpdate() {
        updatedAt = OffsetDateTime.now();
        if (prefixText == null) {
            prefixText = "";
        }
    }
}
