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.complaint.ComplaintRequest;
import ru.soune.nocopy.dto.statistic.ComplaintStatisticResponse;
import ru.soune.nocopy.service.statistic.ComplaintStatisticService;

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

    private final ComplaintStatisticService complaintStatisticService;

    private final ObjectMapper objectMapper;

    @Override
    public BaseResponse handle(BaseRequest request) throws Exception {
        ComplaintRequest complaintRequest = objectMapper.convertValue(request.getMessageBody(), ComplaintRequest.class);
        ComplaintStatisticResponse complaintStatisticResponse = complaintStatisticService.complaintStatistic(complaintRequest);

        return new BaseResponse(
                request.getMsgId(),
                MessageCode.SUCCESS.getCode(),
                MessageCode.SUCCESS.getDescription(),
                complaintStatisticResponse);
    }
}
