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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 驾驶员机动车驾驶证复印件数据提供者
 *
 * @author shihongwei
 */
@Component
@RequiredArgsConstructor
public class DriverLicenseCopyProvider implements IReportDataProvider {

    private final IHotDriverService driverService;

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

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

    @Override
    public String getReportTitle() {
        return "驾驶员机动车驾驶证复印件";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        String driverId = MapUtil.getStr(params, "driverId");
        if (StrUtil.isBlank(driverId)) {
            return MapUtil.empty();
        }

        HotDriverVo driver = driverService.queryById(driverId);
        if (driver == null) {
            return MapUtil.empty();
        }

        Map<String, Object> data = new HashMap<>();
        data.put("driver", driver);

        // Date formatting
        data.put("driverLicenseFirstIssueDate", driver.getDriverLicenseFirstIssueDate() != null ? DateUtil.format(driver.getDriverLicenseFirstIssueDate(), "yyyy-MM-dd") : "");
        // Valid period handling
        String validPeriod = "长期";
        if (driver.getDriverLicenseLongTerm() == null || driver.getDriverLicenseLongTerm() == 0) {
            validPeriod = driver.getDriverLicenseExpireDate() != null ? DateUtil.format(driver.getDriverLicenseExpireDate(), "yyyy-MM-dd") : "";
        }
        data.put("validPeriod", validPeriod);

        // Image URLs will be handled by template using $tool.getOssUrls()

        return data;
    }
}
