package ru.soune.nocopy.handler;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import ru.soune.nocopy.dto.BaseRequest;
import ru.soune.nocopy.dto.BaseResponse;
import ru.soune.nocopy.dto.MessageCode;
import ru.soune.nocopy.exception.NotFoundAuthToken;
import ru.soune.nocopy.util.JwtUtil;

import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class AuthRequestHandler implements RequestHandler {

    private final HttpServletRequest httpServletRequest;

    private final JwtUtil jwtUtil;

    @Override
    public BaseResponse handle(BaseRequest request) throws NotFoundAuthToken {
        Long userId = (Long) httpServletRequest.getAttribute("userId");
        String email = (String) httpServletRequest.getAttribute("email");
        String token = jwtUtil.generateToken(userId, email);

        return new BaseResponse(request.getMsgId(),
                MessageCode.SUCCESS.getCode(),
                "Token processed successfully",
                Map.of("token", token));
    }
}
