package com.hotwj.platform.reportStatistics.provider.driver;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.hotwj.platform.driverManagement.driver.domain.vo.HotDriverVo;
import com.hotwj.platform.driverManagement.driver.service.IHotDriverService;
import com.hotwj.platform.reportStatistics.service.IDriverFilePrintService;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import com.hotwj.platform.securityManagement.accidentArchive.domain.bo.HotAccidentArchiveBo;
import com.hotwj.platform.securityManagement.accidentArchive.domain.vo.HotAccidentArchiveVo;
import com.hotwj.platform.securityManagement.accidentArchive.service.IHotAccidentArchiveService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.OssService;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 驾驶员事故记录数据提供者
 *
 * @author shihongwei
 * @date 2026-03-01
 */
@Component("driverAccidentRecordProvider")
@RequiredArgsConstructor
public class DriverAccidentRecordProvider implements IReportDataProvider {

    private final IHotDriverService driverService;
    private final IHotAccidentArchiveService accidentArchiveService;
    private final OssService ossService;

    @Override
    public String getTemplateCode() {
        return "templates/driver/accidentRecord.html.vm";
    }

    @Override
    public boolean isLandscape() {
        return true;
    }

    @Override
    public String getReportType() {
        return IDriverFilePrintService.TYPE_ACCIDENT_RECORD;
    }

    @Override
    public String getReportTitle() {
        return "驾驶员安全行车事故记录";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {

        String driverId = MapUtil.getStr(params, "driverId");

        HotDriverVo driver = driverService.queryById(driverId);
        if (driver == null) {
            return MapUtil.empty();
        }
        Map<String, Object> data = new HashMap<>();

        data.put("driverName", driver.getName());

        // Fetch accidents
        HotAccidentArchiveBo bo = new HotAccidentArchiveBo();
        bo.setDriverId(driverId);
        bo.setIsDeleted(0L);
        // Ensure not deleted? Service usually handles logic deletion.
        List<HotAccidentArchiveVo> list = accidentArchiveService.queryList(bo);

        // Process list for view (e.g. format dates, process signature if available in accident record)
        List<Map<String, Object>> accidents = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            for (HotAccidentArchiveVo vo : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("accidentTime", vo.getAccidentTime() != null ? DateUtil.format(vo.getAccidentTime(), "yyyy-MM-dd") : "");
                map.put("plateNumber", vo.getPlateNumber());
                map.put("occurLocation", vo.getOccurLocation());
                map.put("mainCause", vo.getMainCause());
                map.put("deathToll", vo.getDeathToll() != null ? vo.getDeathToll() : 0);
                map.put("numberSeriouslyInjured", vo.getNumberSeriouslyInjured() != null ? vo.getNumberSeriouslyInjured() : 0);
                map.put("numberMinorInjuries", vo.getNumberMinorInjuries() != null ? vo.getNumberMinorInjuries() : 0);
                map.put("economicLoss", vo.getEconomicLoss() != null ? vo.getEconomicLoss() : 0);
                map.put("caseClosingTime", vo.getCaseClosingTime() != null ? DateUtil.format(vo.getCaseClosingTime(), "yyyy-MM-dd") : "");
                map.put("divisionResponsibilities", vo.getDivisionResponsibilities()); // Processing result
                map.put("remark", vo.getRemark());

                if (StringUtils.isNotBlank(vo.getResponsiblePersonSignature())) {
                    map.put("signatureUrl", ossService.selectUrlByIds(vo.getResponsiblePersonSignature()));
                }

                accidents.add(map);
            }
        }
        data.put("accidents", accidents);

        return data;
    }
}
