package com.hotwj.platform.workbench.strategy.impl;

import com.hotwj.platform.config.vehicleInspectionConfig.domain.bo.HotVehicleInspectionConfigBo;
import com.hotwj.platform.config.vehicleInspectionConfig.domain.vo.HotVehicleInspectionConfigVo;
import com.hotwj.platform.config.vehicleInspectionConfig.service.IHotVehicleInspectionConfigService;
import com.hotwj.platform.resourceManagement.vehicleAnnualReview.domain.vo.HotVehicleAnnualReviewVo;
import com.hotwj.platform.resourceManagement.vehicleAnnualReview.service.IHotVehicleAnnualReviewService;
import com.hotwj.platform.resourceManagement.vehicleManagement.utils.VehicleUtils;
import com.hotwj.platform.workbench.domain.enums.GovHiddenDangerType;
import com.hotwj.platform.workbench.domain.vo.GovHiddenDangerDetailVo;
import com.hotwj.platform.workbench.domain.vo.VehicleRiskCalcVo;
import com.hotwj.platform.workbench.mapper.GovWorkbenchMapper;
import com.hotwj.platform.workbench.strategy.IGovRiskHandler;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 车辆年审检测隐患处理器
 */
@Component
@RequiredArgsConstructor
public class VehicleAnnualInspectRiskHandler implements IGovRiskHandler {

    private final GovWorkbenchMapper govWorkbenchMapper;
    private final IHotVehicleInspectionConfigService hotVehicleInspectionConfigService;
    private final IHotVehicleAnnualReviewService hotVehicleAnnualReviewService;

    @Override
    public String getType() {
        return GovHiddenDangerType.VEHICLE_ANNUAL_INSPECT.getKey();
    }

    @Override
    public long count(String districtCode, Long companyId) {
        return getRisks(districtCode, companyId).size();
    }

    @Override
    public TableDataInfo<GovHiddenDangerDetailVo> list(String districtCode, Long companyId, PageQuery pageQuery) {
        List<GovHiddenDangerDetailVo> risks = getRisks(districtCode, companyId);

        // 内存分页
        int total = risks.size();
        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();

        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = 10;

        int start = (pageNum - 1) * pageSize;
        int end = Math.min(start + pageSize, total);

        if (start >= total) {
            TableDataInfo<GovHiddenDangerDetailVo> empty = TableDataInfo.build(new ArrayList<>());
            empty.setTotal(total);
            return empty;
        }

        List<GovHiddenDangerDetailVo> pageList = risks.subList(start, end);
        TableDataInfo<GovHiddenDangerDetailVo> tableDataInfo = TableDataInfo.build(pageList);
        tableDataInfo.setTotal(total);
        return tableDataInfo;
    }

    public Map<Long, Integer> countByCompany(String districtCode) {
        Map<Long, Integer> result = new HashMap<>();
        // 处理 districtCode 为 null 的情况，转为空字符串以匹配所有
        String code = districtCode == null ? "" : districtCode;

        // 1. 获取辖区内所有活跃车辆
        List<VehicleRiskCalcVo> vehicles = govWorkbenchMapper.selectVehicleForRiskCalc(code, null);
        if (vehicles.isEmpty()) {
            return result;
        }

        // 2. 按企业分组
        Map<Long, List<VehicleRiskCalcVo>> companyVehicles = vehicles.stream()
                .collect(Collectors.groupingBy(VehicleRiskCalcVo::getCompanyId));

        // 3. 遍历企业处理
        for (Map.Entry<Long, List<VehicleRiskCalcVo>> entry : companyVehicles.entrySet()) {
            Long companyId = entry.getKey();
            List<VehicleRiskCalcVo> cvs = entry.getValue();
            int riskCount = 0;

            // 3.1 获取该企业的年审配置
            try {
                HotVehicleInspectionConfigBo configBo = new HotVehicleInspectionConfigBo();
                configBo.setCompanyId(companyId);
                List<HotVehicleInspectionConfigVo> allConfigs = hotVehicleInspectionConfigService.queryList(configBo);

                List<VehicleUtils.AnnualInspectionRule> rules = allConfigs == null ? new ArrayList<>() : allConfigs.stream()
                        .filter(c -> c != null && (c.getIsEnabled() == null || c.getIsEnabled() == 1L))
                        .filter(c -> c.getIntervalYears() != null && c.getInspectionTimes() != null)
                        .sorted(Comparator.comparing(c -> c.getIntervalYears() == null ? Long.MAX_VALUE : c.getIntervalYears()))
                        .map(c -> new VehicleUtils.AnnualInspectionRule(c.getIntervalYears().intValue(), c.getInspectionTimes().intValue()))
                        .collect(Collectors.toList());

                if (rules.isEmpty()) continue;

                // 3.2 遍历车辆
                for (VehicleRiskCalcVo v : cvs) {
                    if (v.getIssueDate() == null) continue;

                    HotVehicleAnnualReviewVo lastReview = hotVehicleAnnualReviewService.queryLatestByVehicleId(v.getId());
                    LocalDate lastReviewDate = lastReview != null && lastReview.getReviewDate() != null
                            ? lastReview.getReviewDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
                            : null;

                    LocalDate issueDate = v.getIssueDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    VehicleUtils.InspectionResult calc = VehicleUtils.calculateInspectionStatus(rules, issueDate, lastReviewDate);
                    if (calc != null && calc.getOverdueDays() > 0) {
                        riskCount++;
                    }
                }
                
                if (riskCount > 0) {
                    result.put(companyId, riskCount);
                }

            } catch (Exception e) {
                // 忽略异常，继续处理下一个企业
                e.printStackTrace();
            }
        }

        return result;
    }

    private List<GovHiddenDangerDetailVo> getRisks(String districtCode, Long targetCompanyId) {
        List<GovHiddenDangerDetailVo> result = new ArrayList<>();

        // 1. 获取辖区内所有活跃车辆
        List<VehicleRiskCalcVo> vehicles = govWorkbenchMapper.selectVehicleForRiskCalc(districtCode, targetCompanyId);
        if (vehicles.isEmpty()) {
            return result;
        }

        // 2. 按企业分组
        Map<Long, List<VehicleRiskCalcVo>> companyVehicles = vehicles.stream()
                .collect(Collectors.groupingBy(VehicleRiskCalcVo::getCompanyId));

        // 3. 遍历企业处理
        for (Map.Entry<Long, List<VehicleRiskCalcVo>> entry : companyVehicles.entrySet()) {
            Long companyId = entry.getKey();
            if (targetCompanyId != null && !targetCompanyId.equals(companyId)) {
                continue;
            }
            List<VehicleRiskCalcVo> cvs = entry.getValue();

            // 3.1 获取该企业的年审配置
            try {
                HotVehicleInspectionConfigBo configBo = new HotVehicleInspectionConfigBo();
                configBo.setCompanyId(companyId);
                List<HotVehicleInspectionConfigVo> allConfigs = hotVehicleInspectionConfigService.queryList(configBo);

                List<VehicleUtils.AnnualInspectionRule> rules = allConfigs == null ? new ArrayList<>() : allConfigs.stream()
                        .filter(c -> c != null && (c.getIsEnabled() == null || c.getIsEnabled() == 1L))
                        .filter(c -> c.getIntervalYears() != null && c.getInspectionTimes() != null)
                        .sorted(Comparator.comparing(c -> c.getIntervalYears() == null ? Long.MAX_VALUE : c.getIntervalYears()))
                        .map(c -> new VehicleUtils.AnnualInspectionRule(c.getIntervalYears().intValue(), c.getInspectionTimes().intValue()))
                        .collect(Collectors.toList());

                if (rules.isEmpty()) continue;

                // 3.2 遍历车辆
                for (VehicleRiskCalcVo v : cvs) {
                    if (v.getIssueDate() == null) continue;

                    LocalDate issueDate = v.getIssueDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

                    // 获取最近一次年审日期
                    HotVehicleAnnualReviewVo lastReview = hotVehicleAnnualReviewService.queryLatestByVehicleId(v.getId());
                    LocalDate lastReviewDate = lastReview != null && lastReview.getReviewDate() != null
                            ? lastReview.getReviewDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
                            : null;

                    VehicleUtils.InspectionResult calc = VehicleUtils.calculateInspectionStatus(rules, issueDate, lastReviewDate);
                    if (calc != null && calc.getOverdueDays() > 0) {
                        GovHiddenDangerDetailVo vo = new GovHiddenDangerDetailVo();
                        vo.setId(v.getId());
                        vo.setTitle(v.getPlateNumber());
                        vo.setSubTitle("（" + v.getPlateNumber() + "）年审检测：已到期（" + calc.getOverdueDays() + "）天");
                        
                        vo.setStatus("expired");
                        if (calc.getNextInspectionDate() != null) {
                            vo.setDeadline(Date.from(calc.getNextInspectionDate().atStartOfDay(ZoneId.systemDefault()).toInstant()));
                        }
                        vo.setRelatedId(v.getCompanyName());
                        result.add(vo);
                    }
                }

            } catch (Exception e) {
                // 忽略异常，继续处理下一个企业
                e.printStackTrace();
            }
        }

        return result;
    }
}
