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

import dev.lions.unionflow.server.api.enums.vote.ModeScrutin;
import dev.lions.unionflow.server.api.enums.vote.StatutVote;
import dev.lions.unionflow.server.api.enums.vote.TypeVote;
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.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "campagnes_vote", indexes = {
        @Index(name = "idx_vote_orga", columnList = "organisation_id")
})
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class CampagneVote extends BaseEntity {

    @NotBlank
    @Column(name = "titre", nullable = false, length = 200)
    private String titre;

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

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

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "type_vote", nullable = false, length = 50)
    private TypeVote typeVote;

    @NotNull
    @Enumerated(EnumType.STRING)
    @Column(name = "mode_scrutin", nullable = false, length = 50)
    private ModeScrutin modeScrutin;

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

    @NotNull
    @Column(name = "date_ouverture", nullable = false)
    private LocalDateTime dateOuverture;

    @NotNull
    @Column(name = "date_fermeture", nullable = false)
    private LocalDateTime dateFermeture;

    @Column(name = "restreindre_membres_ajour", nullable = false)
    @Builder.Default
    private Boolean restreindreMembresAJour = true;

    @Column(name = "autoriser_vote_blanc", nullable = false)
    @Builder.Default
    private Boolean autoriserVoteBlanc = true;

    @Column(name = "total_electeurs")
    private Integer totalElecteursInscrits;

    @Column(name = "total_votants")
    private Integer totalVotantsEffectifs;

    @Column(name = "total_blancs_nuls")
    private Integer totalVotesBlancsOuNuls;

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