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

import cz.muni.fi.pa165.soccermanager.dto.request.CreateUserRequest;
import cz.muni.fi.pa165.soccermanager.entity.Team;
import cz.muni.fi.pa165.soccermanager.entity.User;
import cz.muni.fi.pa165.soccermanager.repository.TeamRepository;
import lombok.RequiredArgsConstructor;
import org.mapstruct.ObjectFactory;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class UserFactory {

  private final PasswordEncoder encoder;
  private final TeamRepository teamRepository;

  @ObjectFactory
  public User prepare(CreateUserRequest request) {
    final var user = new User();
    user.setPassword(encoder.encode(request.getPassword()));
    return user;
  }

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

}
