package br.gov.sigefp.treasury.domain;

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

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

/**
 * Entidade que representa uma entrada de tesouraria.
 * Similar a BudgetEntry no módulo de Orçamento.
 * Rastreia todas as movimentações de tesouraria com histórico completo.
 */
@Entity
@Table(name = "treasury_entry", indexes = {
    @Index(name = "idx_treasury_entry_cash_account", columnList = "cash_account_id"),
    @Index(name = "idx_treasury_entry_type", columnList = "type"),
    @Index(name = "idx_treasury_entry_status", columnList = "status"),
    @Index(name = "idx_treasury_entry_date", columnList = "transaction_date"),
    @Index(name = "idx_treasury_entry_payment_order", columnList = "payment_order_id")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class TreasuryEntry extends BaseEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cash_account_id", nullable = false)
    private CashAccount cashAccount;

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 50)
    private TreasuryEntryType type;

    @Column(nullable = false, precision = 19, scale = 2)
    private BigDecimal amount;

    @Column(name = "transaction_date", nullable = false)
    private LocalDate transactionDate; // Data da transação

    @Column(name = "document_reference", length = 100)
    private String documentReference; // Ex: "Decreto de Autorização", "Portaria de Pagamento"

    @Column(columnDefinition = "TEXT")
    private String description;

    @Column(nullable = false, length = 20)
    @Builder.Default
    private String status = "DRAFT"; // DRAFT, PENDING_APPROVAL, APPROVED, REJECTED, EXECUTED

    @Column(name = "approval_level")
    private Integer approvalLevel; // Nível de aprovação necessário

    @Column(name = "approved_by")
    private UUID approvedBy; // Usuário que aprovou

    @Column(name = "approved_at")
    private LocalDateTime approvedAt;

    @Column(name = "payment_order_id")
    private UUID paymentOrderId; // Referência à ordem de pagamento (se aplicável)

    @Column(name = "payment_batch_id")
    private UUID paymentBatchId; // Referência ao lote de pagamento (se aplicável)

    @Column(name = "budget_line_id")
    private UUID budgetLineId; // Linha orçamentária relacionada
}
