package br.gov.sigefp.rh.domain;

import br.gov.sigefp.common.domain.AuditableEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.*;
import lombok.*;

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

/**
 * Entidade que representa uma regra de desconto específica para um agente.
 * Usa LocalDate para validade, evitando strings concatenadas.
 */
@Entity
@Table(name = "agent_deduction_rule", indexes = {
        @Index(name = "idx_deduction_rule_agent", columnList = "agent_id"),
        @Index(name = "idx_deduction_rule_type", columnList = "deduction_type_id"),
        @Index(name = "idx_deduction_rule_validity", columnList = "valid_from,valid_to")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AgentDeductionRule extends AuditableEntity {

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "agent_id", nullable = false)
    private Agent agent;

    @JsonProperty("agentId")
    public UUID getAgentIdForSerialization() {
        return agent != null ? agent.getId() : null;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "deduction_type_id", nullable = false)
    private DeductionType deductionType;

    @Column(name = "amount_fixed", precision = 19, scale = 2)
    private BigDecimal amountFixed; // Valor fixo (se aplicável)

    @Column(precision = 5, scale = 2)
    private BigDecimal percentage; // Percentual (se aplicável)

    @Column(nullable = false, name = "valid_from")
    private LocalDate validFrom; // Data de início da vigência

    @Column(name = "valid_to")
    private LocalDate validTo; // Data de fim da vigência (null se ainda vigente)
}
