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.monitoring.MonitoringStatusResponse;
import ru.soune.nocopy.dto.monitoring.SetMonitoringRequest;
import ru.soune.nocopy.entity.monitoring.MonitoringType;
import ru.soune.nocopy.entity.user.AuthToken;
import ru.soune.nocopy.entity.user.User;
import ru.soune.nocopy.exception.UserNotHavePermission;
import ru.soune.nocopy.service.monitoring.FileMonitoringService;
import ru.soune.nocopy.service.register.AuthService;

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Map;

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

    private final ObjectMapper objectMapper;

    private final FileMonitoringService monitoringService;

    private final AuthService authService;

    @Override
    public BaseResponse handle(BaseRequest request) throws Exception {
        SetMonitoringRequest setRequest = objectMapper.convertValue(
                request.getMessageBody(), SetMonitoringRequest.class);

        String authToken = setRequest.getAuthToken();
        AuthToken userAuthToken = authService.getAuthToken(authToken);

        if (authToken == null || userAuthToken == null) {
            return new BaseResponse(30007, MessageCode.AUTH_TOKEN_NOT_FOUND.getCode(),
                    MessageCode.AUTH_TOKEN_NOT_FOUND.getDescription(),
                    Map.of("token", authToken));
        }

        User user = userAuthToken.getUser();
        Long userId = user.getId();
        String typeStr = setRequest.getMonitoringType();
        MonitoringType monitoringType;

        try {
            monitoringType = MonitoringType.valueOf(typeStr);
        } catch (IllegalArgumentException e) {
            return new BaseResponse(request.getMsgId(),
                    MessageCode.MONITORING_TYPE_NOT_FOUND.getCode(),
                    MessageCode.MONITORING_TYPE_NOT_FOUND.getDescription(),
                    Map.of("monitoring", Arrays.asList(MonitoringType.values())));
        }

        try {
            monitoringService.setMonitoring(userId, setRequest.getFileId(), monitoringType);
        } catch (UserNotHavePermission e) {
            return new BaseResponse(request.getMsgId(),
                    MessageCode.USER_NOT_HAD_PERMISSION.getCode(),
                    MessageCode.USER_NOT_HAD_PERMISSION.getDescription(), Map.of("token", authToken));
        } catch (FileNotFoundException e) {
            return new BaseResponse(request.getMsgId(),
                    MessageCode.FILE_NOT_FOUND.getCode(),
                    MessageCode.FILE_NOT_FOUND.getDescription(), Map.of("fileId", setRequest.getFileId()));
        }

        MonitoringStatusResponse status = monitoringService.getMonitoringStatus(setRequest.getFileId(), userId);

        return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
                "Monitoring settings updated", status);
    }
}
