package br.gov.sigefp.rh.domain;

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

import java.math.BigDecimal;
import java.util.UUID;

/**
 * Entidade que representa um item de folha de pagamento (provento ou desconto).
 */
@Entity
@Table(name = "payroll_item", indexes = {
        @Index(name = "idx_payroll_item_run", columnList = "payroll_run_id"),
        @Index(name = "idx_payroll_item_agent", columnList = "agent_id"),
        @Index(name = "idx_payroll_item_type", columnList = "line_type")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class PayrollItem extends AuditableEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "payroll_run_id", nullable = false)
    private PayrollRun payrollRun;

    @Column(name = "agent_id", nullable = false)
    private UUID agent; // Referência ao agente

    @Column(nullable = false, length = 20, name = "line_type")
    private String lineType; // EARNING, DEDUCTION

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "earning_type_id")
    private EarningType earningType; // Tipo de provento (se lineType = EARNING)

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "deduction_type_id")
    private DeductionType deductionType; // Tipo de desconto (se lineType = DEDUCTION)

    @Column(length = 500)
    private String description;

    @Column(precision = 10, scale = 2)
    private BigDecimal quantity; // Quantidade (horas, dias, etc.)

    @Column(precision = 19, scale = 2, name = "unit_amount")
    private BigDecimal unitAmount; // Valor unitário

    @Column(nullable = false, precision = 19, scale = 2, name = "total_amount")
    private BigDecimal totalAmount; // Valor total (quantity * unitAmount ou valor fixo)

    @Column(name = "budget_line_id")
    private UUID budgetLine; // Referência à linha orçamentária (para rastreabilidade)
}
