package dev.lions.unionflow.server.entity.mutuelle.epargne;

import dev.lions.unionflow.server.api.enums.mutuelle.epargne.TypeTransactionEpargne;
import dev.lions.unionflow.server.api.enums.wave.StatutTransactionWave;
import dev.lions.unionflow.server.entity.BaseEntity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Entity
@Table(name = "transactions_epargne", indexes = {
        @Index(name = "idx_tx_epargne_compte", columnList = "compte_id"),
        @Index(name = "idx_tx_epargne_reference", columnList = "reference_externe")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class TransactionEpargne extends BaseEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "compte_id", nullable = false)
    private CompteEpargne compte;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "type_transaction", nullable = false, length = 50)
    private TypeTransactionEpargne type;

    @NotNull
    @Column(name = "montant", nullable = false, precision = 19, scale = 4)
    private BigDecimal montant;

    @Column(name = "solde_avant", precision = 19, scale = 4)
    private BigDecimal soldeAvant;

    @Column(name = "solde_apres", precision = 19, scale = 4)
    private BigDecimal soldeApres;

    @Column(name = "motif", length = 500)
    private String motif;

    @NotNull
    @Column(name = "date_transaction", nullable = false)
    @Builder.Default
    private LocalDateTime dateTransaction = LocalDateTime.now();

    @Column(name = "operateur_id", length = 36)
    private String operateurId;

    @Column(name = "reference_externe", length = 100)
    private String referenceExterne;

    @Enumerated(EnumType.STRING)
    @Column(name = "statut_execution", length = 50)
    private StatutTransactionWave statutExecution;

    /** Origine des fonds (LCB-FT) — obligatoire au-dessus du seuil configuré */
    @Column(name = "origine_fonds", length = 200)
    private String origineFonds;

    /** Pièce justificative (document) pour opérations au-dessus du seuil LCB-FT */
    @Column(name = "piece_justificative_id")
    private java.util.UUID pieceJustificativeId;
}
