package dev.lions.unionflow.server.entity.tontine;

import dev.lions.unionflow.server.api.enums.tontine.FrequenceTour;
import dev.lions.unionflow.server.api.enums.tontine.StatutTontine;
import dev.lions.unionflow.server.api.enums.tontine.TypeTontine;
import dev.lions.unionflow.server.entity.BaseEntity;
import dev.lions.unionflow.server.entity.Organisation;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
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 = "tontines", indexes = {
        @Index(name = "idx_tontine_organisation", columnList = "organisation_id")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class Tontine extends BaseEntity {

    @NotBlank
    @Column(name = "nom", nullable = false, length = 150)
    private String nom;

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

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

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "type_tontine", nullable = false, length = 50)
    private TypeTontine typeTontine;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "frequence", nullable = false, length = 50)
    private FrequenceTour frequence;

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

    @Column(name = "date_debut_effective")
    private LocalDate dateDebutEffective;

    @Column(name = "date_fin_prevue")
    private LocalDate dateFinPrevue;

    @Column(name = "montant_mise_tour", precision = 19, scale = 4)
    private BigDecimal montantMiseParTour;

    @Column(name = "limite_participants")
    private Integer limiteParticipants;

    @OneToMany(mappedBy = "tontine", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("ordreTour ASC")
    @Builder.Default
    private List<TourTontine> calendrierTours = new ArrayList<>();
}
