package com.njcn.rdms.module.project.service.overtime.team;

import com.njcn.rdms.framework.common.pojo.PageParam;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.constant.OvertimeApplicationConstants;
import com.njcn.rdms.module.project.controller.admin.overtime.team.vo.TeamOvertimeSummaryReqVO;
import com.njcn.rdms.module.project.controller.admin.overtime.team.vo.TeamOvertimeSummaryRespVO;
import com.njcn.rdms.module.project.controller.admin.overtime.vo.OvertimeApplicationPageReqVO;
import com.njcn.rdms.module.project.controller.admin.overtime.vo.OvertimeApplicationRespVO;
import com.njcn.rdms.module.project.service.overtime.OvertimeApplicationService;
import com.njcn.rdms.module.project.service.team.TeamDashboardAccessService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;

import static com.njcn.rdms.framework.common.exception.util.ServiceExceptionUtil.invalidParamException;

@Service
public class TeamOvertimeServiceImpl implements TeamOvertimeService {

    private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");

    @Resource
    private TeamDashboardAccessService teamDashboardAccessService;
    @Resource
    private OvertimeApplicationService overtimeApplicationService;

    @Override
    public TeamOvertimeSummaryRespVO getSummary(TeamOvertimeSummaryReqVO reqVO) {
        teamDashboardAccessService.validateTeamDashboardPermission(
                OvertimeApplicationConstants.PERMISSION_TEAM_DASHBOARD);
        List<Long> subordinateIds = teamDashboardAccessService.getAllSubordinateUserIds();
        YearMonth month = parseMonth(reqVO == null ? null : reqVO.getMonth());

        TeamOvertimeSummaryRespVO respVO = new TeamOvertimeSummaryRespVO();
        respVO.setMonth(month.format(MONTH_FORMATTER));
        if (subordinateIds.isEmpty()) {
            respVO.setTotalApplicationCount(0);
            respVO.setPendingCount(0);
            respVO.setApprovedCount(0);
            respVO.setRejectedCount(0);
            return respVO;
        }

        OvertimeApplicationPageReqVO pageReqVO = new OvertimeApplicationPageReqVO();
        pageReqVO.setApplicantIds(subordinateIds);
        pageReqVO.setPageNo(1);
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setOvertimeDate(new LocalDate[]{month.atDay(1), month.atEndOfMonth()});
        PageResult<OvertimeApplicationRespVO> page = overtimeApplicationService.getMyPage(pageReqVO);

        int pendingCount = 0;
        int approvedCount = 0;
        int rejectedCount = 0;
        for (OvertimeApplicationRespVO item : page.getList()) {
            if (OvertimeApplicationConstants.STATUS_PENDING.equals(item.getStatusCode())) {
                pendingCount++;
            } else if (OvertimeApplicationConstants.STATUS_APPROVED.equals(item.getStatusCode())) {
                approvedCount++;
            } else if (OvertimeApplicationConstants.STATUS_REJECTED.equals(item.getStatusCode())) {
                rejectedCount++;
            }
        }
        respVO.setTotalApplicationCount(page.getList().size());
        respVO.setPendingCount(pendingCount);
        respVO.setApprovedCount(approvedCount);
        respVO.setRejectedCount(rejectedCount);
        return respVO;
    }

    private YearMonth parseMonth(String month) {
        if (!StringUtils.hasText(month)) {
            return YearMonth.now();
        }
        try {
            return YearMonth.parse(month, MONTH_FORMATTER);
        } catch (DateTimeParseException ex) {
            throw invalidParamException("统计月份格式不正确，应为 yyyy-MM");
        }
    }
}
