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.util.UUID;

/**
 * Entidade que representa uma transação de fluxo de caixa.
 * Rastreia entradas e saídas de caixa.
 */
@Entity
@Table(name = "cash_flow", indexes = {
    @Index(name = "idx_cash_flow_account", columnList = "cash_account_id"),
    @Index(name = "idx_cash_flow_date", columnList = "transaction_date"),
    @Index(name = "idx_cash_flow_type", columnList = "type"),
    @Index(name = "idx_cash_flow_reference", columnList = "reference_type,reference_id")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class CashFlow extends BaseEntity {

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

    @Column(name = "transaction_date", nullable = false)
    private LocalDate transactionDate;

    @Column(nullable = false, length = 20)
    private String type; // INFLOW, OUTFLOW

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

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

    @Column(name = "reference_id")
    private UUID referenceId; // ID da entidade relacionada

    @Column(name = "reference_type", length = 50)
    private String referenceType; // PAYMENT_ORDER, TREASURY_ENTRY, etc.

    @Column(name = "balance_after", nullable = false, precision = 19, scale = 2)
    private BigDecimal balanceAfter; // Saldo após a transação
}
