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.statistic.MonitoringStatisticRequest;
import ru.soune.nocopy.dto.statistic.MonitoringStatisticResponse;
import ru.soune.nocopy.entity.company.Company;
import ru.soune.nocopy.entity.monitoring.MonitoringType;
import ru.soune.nocopy.entity.user.User;
import ru.soune.nocopy.repository.FileMonitoringRepository;
import ru.soune.nocopy.repository.UserRepository;
import ru.soune.nocopy.service.register.AuthService;

import java.util.List;

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

    private final ObjectMapper objectMapper;

    private final AuthService authService;

    private final FileMonitoringRepository fileMonitoringRepository;

    private final UserRepository userRepository;

    @Override
    public BaseResponse handle(BaseRequest request) throws Exception {
        MonitoringStatisticRequest monitoringStatisticRequest = objectMapper.convertValue(request.getMessageBody(),
                MonitoringStatisticRequest.class);
        try {
            String authToken = monitoringStatisticRequest.getAuthToken();
            Long userId = authService.useUserAuthToken(authToken);

            User user = userRepository.findById(userId).orElseThrow();
            Company company = user.getCompany();
            List<Long> userIds = company == null ? List.of(userId) :
                    user.getCompany()
                    .getUsers()
                    .stream()
                    .map(User::getId).toList();

            MonitoringStatisticResponse monitoringStatisticResponse = MonitoringStatisticResponse.builder()
                    .everyDay(fileMonitoringRepository.countByUserIdsAndMonitoringType(userIds,
                            MonitoringType.MONITORING_DAILY))
                    .everyMonth(fileMonitoringRepository.countByUserIdsAndMonitoringType(userIds,
                            MonitoringType.MONITORING_MONTHLY))
                    .everyWeekly(fileMonitoringRepository.countByUserIdsAndMonitoringType(userIds,
                            MonitoringType.MONITORING_WEEKLY))
                    .build();

            return new BaseResponse(
                    request.getMsgId(),
                    MessageCode.SUCCESS.getCode(),
                    MessageCode.SUCCESS.getDescription(),
                    monitoringStatisticResponse);
        } catch (Exception e) {
            return new BaseResponse(
                    request.getMsgId(),
                    MessageCode.INVALID_JSON_BODY.getCode(),
                    MessageCode.INVALID_JSON_BODY.getDescription(),
                    null);
        }
    }
}
