package com.astalange.core.entity;

import com.astalange.core.validation.ValidRepresentantLegal;
import jakarta.persistence.*;
import java.time.LocalDate;

@Entity
@Table(name = "adherent")
@ValidRepresentantLegal
public class Adherent {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

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

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

    @Column(name = "date_naissance", nullable = false)
    @org.springframework.format.annotation.DateTimeFormat(iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE)
    private LocalDate dateNaissance;

    @Column(name = "lieu_naissance_ville", nullable = false, length = 100)
    private String lieuNaissanceVille;

    @Column(name = "lieu_naissance_pays", nullable = false, length = 100)
    private String lieuNaissancePays;

    @Column(nullable = false, length = 100)
    private String nationalite;

    @Column(nullable = false, length = 100)
    private String email;

    @Column(name = "representant_legal", length = 150)
    private String representantLegal;

    private boolean residentTalange = true;

    @Column(nullable = false, length = 10)
    private String sexe = "MASCULIN";

    @Column(name = "type_maillot", nullable = false, length = 20)
    private String typeMaillot = "JOUEUR";

    // Getters and Setters

    public String getSexe() { return sexe; }
    public void setSexe(String sexe) { this.sexe = sexe; }

    public String getTypeMaillot() { return typeMaillot; }
    public void setTypeMaillot(String typeMaillot) { this.typeMaillot = typeMaillot; }

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getNom() { return nom; }
    public void setNom(String nom) { this.nom = nom; }

    public String getPrenom() { return prenom; }
    public void setPrenom(String prenom) { this.prenom = prenom; }

    public LocalDate getDateNaissance() { return dateNaissance; }
    public void setDateNaissance(LocalDate dateNaissance) { this.dateNaissance = dateNaissance; }

    public String getLieuNaissanceVille() { return lieuNaissanceVille; }
    public void setLieuNaissanceVille(String lieuNaissanceVille) { this.lieuNaissanceVille = lieuNaissanceVille; }

    public String getLieuNaissancePays() { return lieuNaissancePays; }
    public void setLieuNaissancePays(String lieuNaissancePays) { this.lieuNaissancePays = lieuNaissancePays; }

    public String getNationalite() { return nationalite; }
    public void setNationalite(String nationalite) { this.nationalite = nationalite; }

    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }

    public String getRepresentantLegal() { return representantLegal; }
    public void setRepresentantLegal(String representantLegal) { this.representantLegal = representantLegal; }

    public boolean isResidentTalange() { return residentTalange; }
    public void setResidentTalange(boolean residentTalange) { this.residentTalange = residentTalange; }

    @OneToMany(mappedBy = "adherent", cascade = CascadeType.ALL)
    private java.util.List<Licence> licences = new java.util.ArrayList<>();

    public java.util.List<Licence> getLicences() { return licences; }
    public void setLicences(java.util.List<Licence> licences) { this.licences = licences; }

    @Transient
    public Licence getLicenceActuelle() {
        if (licences == null || licences.isEmpty()) {
            return null;
        }
        // Assuming the last added license is the current one (highest ID or latest)
        return licences.get(licences.size() - 1);
    }

    @Transient
    public String getStatutPaiement() {
        Licence current = getLicenceActuelle();
        if (current == null) {
            return "Aucune licence";
        }
        java.math.BigDecimal reste = current.getResteAPayer();
        if (reste.compareTo(java.math.BigDecimal.ZERO) == 0) {
            return "À jour";
        } else {
            return "Reste : " + reste + " €";
        }
    }
}
