package br.gov.sigefp.rh.domain;

import br.gov.sigefp.common.domain.AuditableEntity;
import jakarta.persistence.*;
import lombok.*;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Entidade que representa uma execução/processamento de folha de pagamento.
 * Usa LocalDateTime para timestamps.
 */
@Entity
@Table(name = "payroll_run", indexes = {
        @Index(name = "idx_payroll_run_period", columnList = "period_id"),
        @Index(name = "idx_payroll_run_ministry", columnList = "ministry_id"),
        @Index(name = "idx_payroll_run_org_unit", columnList = "org_unit_id"),
        @Index(name = "idx_payroll_run_status", columnList = "status")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class PayrollRun extends AuditableEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "period_id", nullable = false)
    private PayrollPeriod period;

    @Column(name = "ministry_id")
    private UUID ministry; // Referência ao ministério

    @Column(name = "org_unit_id")
    private UUID orgUnit; // Referência à unidade organizacional

    @Column(nullable = false, length = 50, name = "run_type")
    private String runType; // REGULAR, BONUS, ADJUSTMENT, etc.

    @Column(nullable = false, length = 20)
    @Builder.Default
    private String status = "PENDING"; // PENDING, PROCESSING, COMPLETED, FAILED

    @OneToMany(mappedBy = "payrollRun", cascade = CascadeType.ALL, orphanRemoval = true)
    @Builder.Default
    private List<PayrollItem> items = new ArrayList<>();
}
