package fr.titionfire.ffsaf.data.model;

import fr.titionfire.ffsaf.data.id.RegisterId;
import fr.titionfire.ffsaf.utils.Categorie;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@RegisterForReflection

@Entity
@Table(name = "register")
public class RegisterModel {

    @EmbeddedId
    RegisterId id;

    @MapsId("competitionId")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_competition")
    CompetitionModel competition;

    @MapsId("membreId")
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "id_membre")
    MembreModel membre;

    Integer weight;
    int overCategory = 0;
    Categorie categorie;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "club")
    ClubModel club = null;

    @Column(nullable = false, columnDefinition = "boolean default false")
    boolean lockEdit = false;

    public RegisterModel(CompetitionModel competition, MembreModel membre, Integer weight, int overCategory,
                         Categorie categorie, ClubModel club) {
        this.id = new RegisterId(competition.getId(), membre.getId());
        this.competition = competition;
        this.membre = membre;
        this.weight = weight;
        this.overCategory = overCategory;
        this.categorie = categorie;
        this.club = club;
    }

    public String getName() {
        return membre.fname + " " + membre.lname;
    }

    public ClubModel getClub2() {
        if (club == null)
            return membre.club;
        return club;
    }
}
