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

import cz.muni.fi.pa165.soccermanager.dto.request.CreatePlayerRequest;
import cz.muni.fi.pa165.soccermanager.entity.Player;
import cz.muni.fi.pa165.soccermanager.entity.Team;
import cz.muni.fi.pa165.soccermanager.repository.TeamRepository;
import lombok.RequiredArgsConstructor;
import org.mapstruct.ObjectFactory;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PlayerFactory {

  private final TeamRepository teamRepository;

  @ObjectFactory
  public Player prepare(CreatePlayerRequest request) {
    final var player = new Player();
    player.setTeam(getTeamById(request.getTeam()));
    return player;
  }

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

}
