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

import dev.lions.unionflow.server.api.enums.mutuelle.credit.StatutDemandeCredit;
import dev.lions.unionflow.server.api.enums.mutuelle.credit.TypeCredit;
import dev.lions.unionflow.server.entity.BaseEntity;
import dev.lions.unionflow.server.entity.Membre;
import dev.lions.unionflow.server.entity.mutuelle.epargne.CompteEpargne;

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

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "demandes_credit", indexes = {
        @Index(name = "idx_credit_membre", columnList = "membre_id"),
        @Index(name = "idx_credit_numero", columnList = "numero_dossier", unique = true)
})
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class DemandeCredit extends BaseEntity {

    @Column(name = "numero_dossier", unique = true, nullable = false, length = 50)
    private String numeroDossier;

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

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "type_credit", nullable = false, length = 50)
    private TypeCredit typeCredit;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "compte_lie_id")
    private CompteEpargne compteLie;

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

    @NotNull
    @Column(name = "duree_mois_demande", nullable = false)
    private Integer dureeMoisDemande;

    @Column(name = "justification_detaillee", columnDefinition = "TEXT")
    private String justificationDetaillee;

    @Column(name = "montant_approuve", precision = 19, scale = 4)
    private BigDecimal montantApprouve;

    @Column(name = "duree_mois_approuvee")
    private Integer dureeMoisApprouvee;

    @Column(name = "taux_interet_annuel", precision = 5, scale = 2)
    private BigDecimal tauxInteretAnnuel;

    @Column(name = "cout_total_credit", precision = 19, scale = 4)
    private BigDecimal coutTotalCredit;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "statut", nullable = false, length = 50)
    @Builder.Default
    private StatutDemandeCredit statut = StatutDemandeCredit.SOUMISE;

    @Column(name = "notes_comite", columnDefinition = "TEXT")
    private String notesComite;

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

    @Column(name = "date_validation")
    private LocalDate dateValidation;

    @Column(name = "date_premier_echeance")
    private LocalDate datePremierEcheance;

    @OneToMany(mappedBy = "demandeCredit", cascade = CascadeType.ALL, orphanRemoval = true)
    @Builder.Default
    private List<GarantieDemande> garanties = new ArrayList<>();

    @OneToMany(mappedBy = "demandeCredit", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("ordre ASC")
    @Builder.Default
    private List<EcheanceCredit> echeancier = new ArrayList<>();
}
