package dev.lions.unionflow.server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/** Entité Membre avec UUID */
@Entity
@Table(
    name = "membres",
    indexes = {
      @Index(name = "idx_membre_email", columnList = "email", unique = true),
      @Index(name = "idx_membre_numero", columnList = "numero_membre", unique = true),
      @Index(name = "idx_membre_actif", columnList = "actif")
    })
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class Membre extends BaseEntity {

  @NotBlank
  @Column(name = "numero_membre", unique = true, nullable = false, length = 20)
  private String numeroMembre;

  @NotBlank
  @Column(name = "prenom", nullable = false, length = 100)
  private String prenom;

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

  @Email
  @NotBlank
  @Column(name = "email", unique = true, nullable = false, length = 255)
  private String email;

  @Column(name = "mot_de_passe", length = 255)
  private String motDePasse;

  @Column(name = "telephone", length = 20)
  private String telephone;

  @Pattern(
      regexp = "^\\+225[0-9]{8}$",
      message = "Le numéro de téléphone Wave doit être au format +225XXXXXXXX")
  @Column(name = "telephone_wave", length = 13)
  private String telephoneWave;

  @NotNull
  @Column(name = "date_naissance", nullable = false)
  private LocalDate dateNaissance;

  @NotNull
  @Column(name = "date_adhesion", nullable = false)
  private LocalDate dateAdhesion;

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

  // Relations
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "organisation_id")
  private Organisation organisation;

  @OneToMany(mappedBy = "membre", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @Builder.Default
  private List<Adresse> adresses = new ArrayList<>();

  @OneToMany(mappedBy = "membre", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @Builder.Default
  private List<MembreRole> roles = new ArrayList<>();

  @OneToMany(mappedBy = "membre", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @Builder.Default
  private List<CompteWave> comptesWave = new ArrayList<>();

  @OneToMany(mappedBy = "membre", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  @Builder.Default
  private List<Paiement> paiements = new ArrayList<>();

  /** Méthode métier pour obtenir le nom complet */
  public String getNomComplet() {
    return prenom + " " + nom;
  }

  /** Méthode métier pour vérifier si le membre est majeur */
  public boolean isMajeur() {
    return dateNaissance.isBefore(LocalDate.now().minusYears(18));
  }

  /** Méthode métier pour calculer l'âge */
  public int getAge() {
    return LocalDate.now().getYear() - dateNaissance.getYear();
  }
}
