package ru.kayashov.bar.service;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import ru.kayashov.bar.model.entity.Visitor;
import ru.kayashov.bar.repository.VisitorsRepository;

@Service
@RequiredArgsConstructor
public class VisitorService {

    private final VisitorsRepository visitorsRepository;

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

    public Visitor findById(Long id) {
        return visitorsRepository.findById(id).orElseThrow(() -> new RuntimeException("Visitor not found. id: " + id));
    }
}
