package at.procon.dip.migration.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;

@Entity
@Table(schema = SchemaNames.DOC, name = "doc_legacy_ted_migration_checkpoint", indexes = {
        @Index(name = "idx_doc_legacy_ted_mig_ckpt_run", columnList = "run_id"),
        @Index(name = "idx_doc_legacy_ted_mig_ckpt_batch", columnList = "batch_number")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class LegacyTedMigrationCheckpoint {

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

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "run_id", nullable = false)
    private LegacyTedMigrationRun run;

    @Column(name = "batch_number", nullable = false)
    private int batchNumber;

    @Column(name = "batch_processed_count", nullable = false)
    private int batchProcessedCount;

    @Column(name = "cumulative_processed_count", nullable = false)
    private long cumulativeProcessedCount;

    @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 = "note", columnDefinition = "TEXT")
    private String note;

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

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