package ru.soune.nocopy.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
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.dto.TokenProcessRequest;
import ru.soune.nocopy.exception.NotFoundAuthToken;
import ru.soune.nocopy.service.register.AuthService;

import java.util.Map;

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

    private final ObjectMapper objectMapper;

    private final AuthService authService;

    @Override
    public BaseResponse handle(BaseRequest request) throws NotFoundAuthToken {
        TokenProcessRequest tokenProcessRequest = objectMapper.convertValue(request.getMessageBody(),
                TokenProcessRequest.class);

        authService.useUserAuthToken(tokenProcessRequest.getToken());

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