package ru.kirillius.pf.sdn.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Optional;

@Service
@RequiredArgsConstructor
public class SessionService {

    private final AuthService authService;

    public Optional<Long> resolveUserId(String authHeader) {
        return extractToken(authHeader)
                .flatMap(authService::resolveUserId);
    }

    public Optional<String> extractToken(String authHeader) {
        if (StringUtils.hasText(authHeader) && authHeader.startsWith("Bearer ")) {
            return Optional.of(authHeader.substring(7));
        }
        return Optional.empty();
    }

    public void invalidate(String token) {
        authService.invalidateToken(token);
    }
}
