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.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Entidade que representa uma conciliação bancária.
 * Compara saldos e transações do sistema com extratos bancários.
 */
@Entity
@Table(name = "bank_reconciliation", indexes = {
    @Index(name = "idx_reconciliation_cash_account", columnList = "cash_account_id"),
    @Index(name = "idx_reconciliation_date", columnList = "reconciliation_date"),
    @Index(name = "idx_reconciliation_status", columnList = "status")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class BankReconciliation extends BaseEntity {

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

    @Column(name = "reconciliation_date", nullable = false)
    private LocalDate reconciliationDate;

    @Column(name = "statement_balance", nullable = false, precision = 19, scale = 2)
    private BigDecimal statementBalance; // Saldo do extrato bancário

    @Column(name = "system_balance", nullable = false, precision = 19, scale = 2)
    private BigDecimal systemBalance; // Saldo do sistema

    @Column(precision = 19, scale = 2)
    private BigDecimal difference; // Diferença (statementBalance - systemBalance)

    @Column(nullable = false, length = 20)
    @Builder.Default
    private String status = "PENDING"; // PENDING, RECONCILED, DISCREPANCY

    @Column(name = "reconciled_by")
    private UUID reconciledBy; // Usuário que conciliou

    @Column(name = "reconciled_at")
    private LocalDateTime reconciledAt;

    @OneToMany(mappedBy = "reconciliation", cascade = CascadeType.ALL, orphanRemoval = true)
    @Builder.Default
    private List<ReconciliationItem> reconciliationItems = new ArrayList<>();
}
