package fr.titionfire.ffsaf.domain.entity;

import fr.titionfire.ffsaf.data.model.CatPresetModel;
import fr.titionfire.ffsaf.data.model.CompetitionGuestModel;
import fr.titionfire.ffsaf.data.model.MembreModel;
import fr.titionfire.ffsaf.data.model.RegisterModel;
import fr.titionfire.ffsaf.utils.Categorie;
import fr.titionfire.ffsaf.utils.Genre;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

@Data
@AllArgsConstructor
@RegisterForReflection
public class CombEntity {
    private long id;
    private String lname;
    private String fname;
    Categorie categorie;
    String club_uuid;
    String club_str;
    Genre genre;
    String country;
    int overCategory;
    Float weight;
    List<CombEntity> teamMembers;
    List<Long> categoriesInscrites;

    public static CombEntity fromModel(MembreModel model) {
        if (model == null)
            return null;

        return new CombEntity(model.getId(), model.getLname(), model.getFname(), model.getCategorie(),
                model.getClub() == null ? null : model.getClub().getClubId(),
                model.getClub() == null ? "Sans club" : model.getClub().getName(), model.getGenre(), model.getCountry(),
                0, null, new ArrayList<>(), new ArrayList<>());
    }


    public static CombEntity fromModel(CompetitionGuestModel model) {
        if (model == null)
            return null;

        return new CombEntity(model.getId() * -1, model.getLname(), model.getFname(), model.getCategorie(), null,
                model.getClub(), model.getGenre(), model.getCountry(), 0,
                model.getWeight2() != null ? model.getWeight2() : model.getWeight(),
                Stream.concat(model.getComb().stream().map(CombEntity::fromModel),
                        model.getGuest().stream().map(CombEntity::fromModel)).toList(),
                new ArrayList<>());
    }

    public CombEntity addCategoriesInscrites(List<CatPresetModel> categoriesInscrites) {
        if (categoriesInscrites == null)
            return this;
        this.categoriesInscrites = categoriesInscrites.stream().map(CatPresetModel::getId).toList();
        return this;
    }

    public static CombEntity fromModel(RegisterModel registerModel) {
        if (registerModel == null || registerModel.getMembre() == null)
            return null;
        MembreModel model = registerModel.getMembre();

        return new CombEntity(model.getId(), model.getLname(), model.getFname(), registerModel.getCategorie(),
                registerModel.getClub2() == null ? null : registerModel.getClub2().getClubId(),
                registerModel.getClub2() == null ? "Sans club" : registerModel.getClub2().getName(), model.getGenre(),
                model.getCountry(), registerModel.getOverCategory(),
                registerModel.getWeight2() != null ? registerModel.getWeight2() : registerModel.getWeight(),
                new ArrayList<>(), new ArrayList<>());
    }
}
