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

import dev.lions.unionflow.server.entity.BaseEntity;
import dev.lions.unionflow.server.entity.Membre;
import dev.lions.unionflow.server.entity.Organisation;
import dev.lions.unionflow.server.api.enums.mutuelle.epargne.StatutCompteEpargne;
import dev.lions.unionflow.server.api.enums.mutuelle.epargne.TypeCompteEpargne;

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

import java.math.BigDecimal;
import java.time.LocalDate;

@Entity
@Table(name = "comptes_epargne", indexes = {
        @Index(name = "idx_compte_epargne_numero", columnList = "numero_compte", unique = true),
        @Index(name = "idx_compte_epargne_membre", columnList = "membre_id"),
        @Index(name = "idx_compte_epargne_orga", columnList = "organisation_id")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class CompteEpargne extends BaseEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "membre_id", nullable = false)
    private Membre membre;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "organisation_id", nullable = false)
    private Organisation organisation;

    @NotBlank
    @Column(name = "numero_compte", unique = true, nullable = false, length = 50)
    private String numeroCompte;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "type_compte", nullable = false, length = 50)
    private TypeCompteEpargne typeCompte;

    @NotNull
    @Column(name = "solde_actuel", nullable = false, precision = 19, scale = 4)
    @Builder.Default
    private BigDecimal soldeActuel = BigDecimal.ZERO;

    @NotNull
    @Column(name = "solde_bloque", nullable = false, precision = 19, scale = 4)
    @Builder.Default
    private BigDecimal soldeBloque = BigDecimal.ZERO;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "statut", nullable = false, length = 30)
    @Builder.Default
    private StatutCompteEpargne statut = StatutCompteEpargne.ACTIF;

    @NotNull
    @Column(name = "date_ouverture", nullable = false)
    @Builder.Default
    private LocalDate dateOuverture = LocalDate.now();

    @Column(name = "date_derniere_transaction")
    private LocalDate dateDerniereTransaction;

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