package com.zcloud.safetyDutyList.command.query.tasklist;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.safetyDutyList.command.convertor.tasklist.TaskListCoConvertor;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.FeedbackExceptionGateway;
import com.zcloud.safetyDutyList.dto.clientobject.tasklist.*;
import com.zcloud.safetyDutyList.dto.tasklist.TaskDetailPageQry;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.FeedbackExceptionDO;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.TaskDetailDO;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.FeedbackExceptionRepository;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.TaskDetailRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 任务详情查询执行器
 * <p>
 * 处理任务详情相关的查询业务：
 *   - 任务详情分页列表查询
 *   - 任务详情详情查询（含所属清单信息）
 */
@Component
@AllArgsConstructor
public class TaskDetailQueryExe {
    private final TaskDetailRepository taskDetailRepository;
    private final FeedbackExceptionRepository feedbackExceptionRepository;
    private final TaskListCoConvertor taskListCoConvertor;

    public PageResponse<TaskDetailCO> listTaskDetailPage(TaskDetailPageQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<TaskDetailDO> pageResponse = taskDetailRepository.listPage(params);
        List<TaskDetailCO> coList = pageResponse.getData().stream().map(d -> {
            TaskDetailCO co = new TaskDetailCO();
            BeanUtils.copyProperties(d, co);
            return co;
        }).collect(Collectors.toList());
        return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public PageResponse<TaskDetailFullCO> listPage(TaskDetailPageQry qry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(qry);
        PageResponse<TaskDetailDO> pageResponse = taskDetailRepository.listPage(params);
        List<TaskDetailDO> detailDOList = pageResponse.getData();
        if (detailDOList == null || detailDOList.isEmpty()) {
            return PageResponse.of(new ArrayList<>(), pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
        }

        String taskListId = qry.getTaskListId();
        Map<String, List<FeedbackExceptionDO>> exceptionMap = new java.util.HashMap<>();
        if (taskListId != null && !taskListId.isEmpty()) {
            List<FeedbackExceptionDO> allExceptions = feedbackExceptionRepository.listByTaskListId(taskListId);
            exceptionMap = allExceptions.stream()
                    .collect(Collectors.groupingBy(FeedbackExceptionDO::getTaskDetailId));
        }

        Map<String, List<FeedbackExceptionDO>> finalExceptionMap = exceptionMap;
        List<TaskDetailFullCO> coList = detailDOList.stream().map(d -> {
            TaskDetailFullCO co = new TaskDetailFullCO();
            BeanUtils.copyProperties(d, co);

            List<FeedbackExceptionDO> exList = finalExceptionMap.getOrDefault(d.getTaskDetailId(), new ArrayList<>());
            List<FeedbackExceptionCO> exCOList = exList.stream().map(e -> {
                FeedbackExceptionCO eco = new FeedbackExceptionCO();
                BeanUtils.copyProperties(e, eco);
                return eco;
            }).collect(Collectors.toList());
            co.setExceptionList(exCOList);
            return co;
        }).collect(Collectors.toList());

        return PageResponse.of(coList, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public SingleResponse<TaskDetailInfoCO> getByDetailId(String detailId) {
        TaskDetailDO detailDO = taskDetailRepository.getByTaskDetailId(detailId);
        if (detailDO == null) {
            throw new BizException("任务详情不存在");
        }
        TaskDetailInfoCO co = new TaskDetailInfoCO();
        BeanUtils.copyProperties(detailDO, co);
        co.setTaskListName(detailDO.getTaskListName());
        co.setTaskLevel(detailDO.getTaskLevel());
        co.setResponsibilityPost(detailDO.getResponsibilityPost());
        return SingleResponse.of(co);
    }
}
