package com.zt.plat.module.base.service.doctemplate.render.provider;

import com.zt.plat.module.base.service.doctemplate.render.IBusinessDataProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * 合同数据提供者
 * 提供合同相关的业务数据
 *
 * @author hwc
 */
@Component("contractDataProvider")
@Slf4j
public class ContractDataProvider implements IBusinessDataProvider {

    @Override
    public Map<String, Object> provideData(Object context, List<String> fieldNames) {
        Long contractId = null;
        if (context instanceof Long) {
            contractId = (Long) context;
        } else if (context instanceof String) {
            try {
                contractId = Long.valueOf((String) context);
            } catch (NumberFormatException e) {
                log.warn("无效的合同ID格式: {}", context);
                return Map.of();
            }
        }

        if (contractId == null) {
            log.warn("合同ID为空");
            return Map.of();
        }

        log.debug("获取合同数据，合同ID: {}, 请求字段: {}", contractId, fieldNames);

        // TODO: 实际实现应该：
        // 1. 根据合同ID从数据库或服务中查询合同信息
        // 2. 根据请求的字段名返回对应的数据
        // 3. 处理数据类型转换

        // 临时实现：返回一些示例数据
        Map<String, Object> data = new HashMap<>();

        // 模拟合同数据
        for (String fieldName : fieldNames) {
            switch (fieldName) {
                case "contractName":
                case "contract_name":
                    data.put(fieldName, "中铜采购合同-" + contractId);
                    break;
                case "supplier":
                case "supplier_name":
                    data.put(fieldName, "上海金属贸易有限公司");
                    break;
                case "amount":
                case "total_amount":
                    data.put(fieldName, 5000000.00);
                    break;
                case "deliveryDate":
                case "delivery_date":
                    data.put(fieldName, "2025-06-30");
                    break;
                case "contractNumber":
                case "contract_number":
                    data.put(fieldName, "HT-2025-" + String.format("%04d", contractId % 10000));
                    break;
                case "signDate":
                case "sign_date":
                    data.put(fieldName, "2025-01-15");
                    break;
                case "paymentTerms":
                case "payment_terms":
                    data.put(fieldName, "预付30%，发货付60%，验收付10%");
                    break;
                default:
                    log.debug("未处理的合同字段: {}", fieldName);
                    break;
            }
        }

        log.debug("返回合同数据: {}", data.keySet());
        return data;
    }
}