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

import cz.muni.fi.pa165.soccermanager.dto.request.CreateMatchRequest;
import cz.muni.fi.pa165.soccermanager.entity.League;
import cz.muni.fi.pa165.soccermanager.entity.Match;
import cz.muni.fi.pa165.soccermanager.entity.Team;
import cz.muni.fi.pa165.soccermanager.repository.LeagueRepository;
import cz.muni.fi.pa165.soccermanager.repository.TeamRepository;
import lombok.RequiredArgsConstructor;
import org.mapstruct.ObjectFactory;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MatchFactory {
  private final LeagueRepository leagueRepository;
  private final TeamRepository teamRepository;

  @ObjectFactory
  public Match prepare(CreateMatchRequest dto) {
    final var match = new Match();
    match.setChallenger(getTeamById(dto.getChallenger()));
    match.setDefender(getTeamById(dto.getDefender()));

    return match;
  }

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

  public Team getTeamById(Long value) {
    return value == null ? null : teamRepository.findById(value).orElse(null);
  }

}
