package ru.kayashov.karthall.service;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import ru.kayashov.karthall.model.entity.Player;
import ru.kayashov.karthall.repository.PlayerRepository;

@Service
@RequiredArgsConstructor
public class PlayerService {

    private final PlayerRepository playerRepository;

    public Player getCurrentVisitor() {
        Long id = ((Player) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();
        return findById(id);
    }

    public Player findById(Long id) {
        return playerRepository.findById(id).orElseThrow(() -> new RuntimeException("пользователь не найден. id: " + id));
    }
}
