package at.procon.ted.model.entity;

import jakarta.persistence.*;
import lombok.*;

import java.time.OffsetDateTime;
import java.util.UUID;

/**
 * JPA Entity for logging document processing events.
 * Provides audit trail and debugging information.
 * 
 * @author Martin.Schweitzer@procon.co.at and claude.ai
 */
@Entity
@Table(name = "processing_log", indexes = {
    @Index(name = "idx_log_document", columnList = "document_id"),
    @Index(name = "idx_log_created", columnList = "created_at"),
    @Index(name = "idx_log_event_type", columnList = "event_type")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ProcessingLog {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "document_id")
    private ProcurementDocument document;

    @Column(name = "document_hash", length = 64)
    private String documentHash;

    @Column(name = "event_type", nullable = false, length = 50)
    private String eventType;

    @Column(name = "event_status", nullable = false, length = 20)
    private String eventStatus;

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

    @Column(name = "error_details", columnDefinition = "TEXT")
    private String errorDetails;

    @Column(name = "source_filename", length = 500)
    private String sourceFilename;

    @Column(name = "duration_ms")
    private Integer durationMs;

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

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

    /**
     * Event types for processing log entries.
     */
    public static final class EventType {
        public static final String RECEIVED = "RECEIVED";
        public static final String VALIDATED = "VALIDATED";
        public static final String PARSED = "PARSED";
        public static final String STORED = "STORED";
        public static final String VECTORIZED = "VECTORIZED";
        public static final String DUPLICATE = "DUPLICATE";
        public static final String ERROR = "ERROR";
        
        private EventType() {}
    }

    /**
     * Status values for processing log entries.
     */
    public static final class EventStatus {
        public static final String SUCCESS = "SUCCESS";
        public static final String FAILURE = "FAILURE";
        public static final String SKIPPED = "SKIPPED";
        
        private EventStatus() {}
    }
}
