package cz.muni.fi.pa165.soccermanager.factory;

import cz.muni.fi.pa165.soccermanager.dto.request.UpdateTeamDto;
import cz.muni.fi.pa165.soccermanager.entity.Championship;
import cz.muni.fi.pa165.soccermanager.entity.League;
import cz.muni.fi.pa165.soccermanager.entity.Team;
import cz.muni.fi.pa165.soccermanager.entity.User;
import cz.muni.fi.pa165.soccermanager.repository.ChampionshipRepository;
import cz.muni.fi.pa165.soccermanager.repository.LeagueRepository;
import cz.muni.fi.pa165.soccermanager.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.mapstruct.ObjectFactory;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class TeamFactory {
  private final LeagueRepository leagueRepository;
  private final ChampionshipRepository championshipRepository;
  private final UserRepository userRepository;

  @ObjectFactory
  public Team prepare(UpdateTeamDto dto) {
    final var team = new Team();
    team.setChampionship(getChampionshipById(dto.getChampionship()));
    return team;
  }

  public League getLeagueById(Long value) {
    return value == null ? null : leagueRepository.findById(value).orElse(null);
  }

  public User getUserById(Long value) {
    return value == null ? null : userRepository.findById(value).orElse(null);
  }

  public Championship getChampionshipById(Long value) {
    return value == null ? null : championshipRepository.findById(value).orElse(null);
  }
}
