package br.gov.sigefp.budget.domain;

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

import java.util.UUID;

/**
 * Entidade que representa uma linha orçamentária.
 */
@Entity
@Table(name = "budget_line", indexes = {
        @Index(name = "idx_budget_line_code", columnList = "code"),
        @Index(name = "idx_budget_line_fiscal_year", columnList = "fiscal_year_id"),
        @Index(name = "idx_budget_line_ministry", columnList = "ministry_id"),
        @Index(name = "idx_budget_line_org_unit", columnList = "org_unit_id")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class BudgetLine extends BaseEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fiscal_year_id", nullable = false)
    private FiscalYear fiscalYear;

    @Column(nullable = false, unique = true, length = 50)
    private String code;

    @Column(nullable = false, length = 500)
    private String description;

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

    @Column(name = "org_unit_id", nullable = false)
    private UUID orgUnit; // Referência à unidade organizacional (org.org_unit)

    @Column(nullable = false, length = 100, name = "economic_class")
    private String economicClass; // Classificação econômica

    @org.hibernate.annotations.Formula("(SELECT COALESCE(SUM(be.amount), 0) FROM budget_entry be WHERE be.budget_line_id = id)")
    private java.math.BigDecimal totalAllocated;

    @org.hibernate.annotations.Formula("(SELECT COALESCE(SUM(bex.amount), 0) FROM budget_execution bex WHERE bex.budget_line_id = id AND bex.movement_type = 'COMMITMENT')")
    private java.math.BigDecimal totalCommitted;
}
