package at.procon.dip.migration.entity;

import at.procon.dip.architecture.SchemaNames;
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_legacy_ted_migration_run", indexes = {
        @Index(name = "idx_doc_legacy_ted_mig_run_status", columnList = "status"),
        @Index(name = "idx_doc_legacy_ted_mig_run_started", columnList = "started_at DESC")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class LegacyTedMigrationRun {

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

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

    @Column(name = "import_batch_id", length = 255)
    private String importBatchId;

    @Column(name = "queue_embeddings", nullable = false)
    private boolean queueEmbeddings;

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

    @Column(name = "batch_size", nullable = false)
    private int batchSize;

    @Column(name = "max_documents_per_run")
    private Long maxDocumentsPerRun;

    @Column(name = "processed_count", nullable = false)
    @Builder.Default
    private long processedCount = 0;

    @Column(name = "success_count", nullable = false)
    @Builder.Default
    private long successCount = 0;

    @Column(name = "failed_count", nullable = false)
    @Builder.Default
    private long failedCount = 0;

    @Column(name = "last_legacy_created_at")
    private OffsetDateTime lastLegacyCreatedAt;

    @Column(name = "last_legacy_document_id")
    private UUID lastLegacyDocumentId;

    @Column(name = "last_doc_document_id")
    private UUID lastDocDocumentId;

    @Column(name = "last_projection_id")
    private UUID lastProjectionId;

    @Column(name = "last_error", columnDefinition = "TEXT")
    private String lastError;

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

    @Builder.Default
    @Column(name = "updated_at", nullable = false)
    private OffsetDateTime updatedAt = OffsetDateTime.now();

    @Column(name = "completed_at")
    private OffsetDateTime completedAt;

    @PrePersist
    protected void onCreate() {
        startedAt = startedAt == null ? OffsetDateTime.now() : startedAt;
        updatedAt = OffsetDateTime.now();
    }

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