package br.gov.sigefp.rh.domain;

import br.gov.sigefp.common.domain.AuditableEntity;
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 um contrato de trabalho do agente.
 * Usa LocalDate para datas, evitando strings concatenadas.
 */
@Entity
@Table(name = "agent_contract", indexes = {
        @Index(name = "idx_contract_agent", columnList = "agent_id"),
        @Index(name = "idx_contract_dates", columnList = "start_date,end_date"),
        @Index(name = "idx_contract_active", columnList = "is_active")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AgentContract extends AuditableEntity {

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

    @Column(nullable = false, length = 50)
    private String contractType; // PERMANENT, TEMPORARY, INTERNSHIP, FIXED_TERM, etc.

    @Column(nullable = false, name = "start_date")
    private LocalDate startDate;

    @Column(name = "end_date")
    private LocalDate endDate; // Null para contratos sem término definido

    @Column(name = "weekly_hours", precision = 5, scale = 2)
    private BigDecimal weeklyHours; // Horas semanais

    @Column(name = "base_salary_ref", precision = 19, scale = 2)
    private BigDecimal baseSalaryRef; // Referência salarial base

    @Column(name = "org_unit_id")
    private UUID orgUnit;

    @Column(name = "position_id")
    private UUID position;

    @Column(name = "salary_category_id")
    private UUID salaryCategory;

    @Column(name = "salary_grade_id")
    private UUID salaryGrade;

    @Column(name = "salary_step_id")
    private UUID salaryStep;

    @Column(name = "legal_act_reference", length = 100)
    private String legalActReference; // Ex: Decreto n┬║ 12/2023, Despacho n┬║ 45/2023

    @Column(nullable = false, name = "is_active")
    @Builder.Default
    private Boolean isActive = true;

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