package at.procon.dip.migration.audit.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.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_audit_finding", indexes = {
        @Index(name = "idx_doc_legacy_audit_find_run", columnList = "run_id"),
        @Index(name = "idx_doc_legacy_audit_find_type", columnList = "finding_type"),
        @Index(name = "idx_doc_legacy_audit_find_severity", columnList = "severity"),
        @Index(name = "idx_doc_legacy_audit_find_legacy_doc", columnList = "legacy_procurement_document_id"),
        @Index(name = "idx_doc_legacy_audit_find_document", columnList = "document_id")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class LegacyTedAuditFinding {

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

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

    @Enumerated(EnumType.STRING)
    @Column(name = "severity", nullable = false, length = 16)
    private LegacyTedAuditSeverity severity;

    @Enumerated(EnumType.STRING)
    @Column(name = "finding_type", nullable = false, length = 64)
    private LegacyTedAuditFindingType findingType;

    @Column(name = "package_identifier", length = 20)
    private String packageIdentifier;

    @Column(name = "legacy_procurement_document_id")
    private UUID legacyProcurementDocumentId;

    @Column(name = "document_id")
    private UUID documentId;

    @Column(name = "ted_notice_projection_id")
    private UUID tedNoticeProjectionId;

    @Column(name = "reference_key", length = 255)
    private String referenceKey;

    @Column(name = "message", nullable = false, columnDefinition = "TEXT")
    private String message;

    @Column(name = "details_text", columnDefinition = "TEXT")
    private String detailsText;

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

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