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 java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;

@Service
public class TeamOvertimeServiceImpl implements TeamOvertimeService {

    @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();
        LocalDate[] dateRange = normalizeDateRange(
                reqVO == null ? null : reqVO.getOvertimeDateStart(),
                reqVO == null ? null : reqVO.getOvertimeDateEnd());
        LocalDate startDate = dateRange[0];
        LocalDate endDate = dateRange[1];

        TeamOvertimeSummaryRespVO respVO = new TeamOvertimeSummaryRespVO();
        respVO.setOvertimeDateStart(startDate);
        respVO.setOvertimeDateEnd(endDate);
        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[] { startDate, endDate });
        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 LocalDate[] normalizeDateRange(LocalDate start, LocalDate end) {
        if (start == null && end == null) {
            YearMonth currentMonth = YearMonth.now();
            return new LocalDate[] { currentMonth.atDay(1), currentMonth.atEndOfMonth() };
        }
        if (start == null) {
            start = end.withDayOfMonth(1);
        }
        if (end == null) {
            end = start.withDayOfMonth(start.lengthOfMonth());
        }
        return new LocalDate[] { start, end };
    }
}
