package com.nbport.zgwl.controller;

import com.nbport.zgwl.common.R;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/admin/dict")
public class AdminDictController {

    private static final String TEMPLATE_FIELD_DICT_TYPE = "CONTRACT_TEMPLATE_FIELDS";

    @PostMapping("/getDictDataByType")
    public R<List<DictDataItem>> getDictDataByType(@RequestBody(required = false) Map<String, Object> body) {
        String dictType = resolveDictType(body);
        if (TEMPLATE_FIELD_DICT_TYPE.equals(dictType)) {
            return R.success(buildTemplateFieldItems());
        }
        return R.success(List.of());
    }

    private String resolveDictType(Map<String, Object> body) {
        if (body == null) {
            return null;
        }
        Object params = body.get("params");
        if (params instanceof Map<?, ?> paramMap) {
            Object dictType = paramMap.get("dictType");
            return dictType == null ? null : String.valueOf(dictType).trim();
        }
        Object dictType = body.get("dictType");
        return dictType == null ? null : String.valueOf(dictType).trim();
    }

    private List<DictDataItem> buildTemplateFieldItems() {
        return List.of(
                new DictDataItem("合同名称", "contractName", "合同正文中的合同名称"),
                new DictDataItem("合同编码", "contractCode", "合同正文中的合同编码"),
                new DictDataItem("合同类型", "contractType", "合同正文中的合同类型"),
                new DictDataItem("合同金额", "contractAmountDisplay", "用于展示金额文本"),
                new DictDataItem("有效期起", "effectiveStart", "合同有效期起始日期"),
                new DictDataItem("有效期止", "effectiveEnd", "合同有效期结束日期"),
                new DictDataItem("有效期区间", "effectivePeriod", "合同有效期区间文本"),
                new DictDataItem("付款方式", "paymentMethod", "合同中的付款方式说明"),
                new DictDataItem("合同标的说明", "primaryContent", "合同主要内容或标的说明"),
                new DictDataItem("业务备注", "amountExplain", "可用于维护金额说明或业务备注"),
                new DictDataItem("甲方名称", "partyAName", "模板变量值，由用户自行维护"),
                new DictDataItem("乙方名称", "partyBName", "模板变量值，由用户自行维护"),
                new DictDataItem("丙方名称", "partyCName", "模板变量值，由用户自行维护"),
                new DictDataItem("合同方摘要", "counterpartySummary", "模板变量值，由用户自行维护"),
                new DictDataItem("附件情况", "attachmentSummary", "模板变量值，由用户自行维护"),
                new DictDataItem("模板名称", "templateLabel", "模板变量值，由用户自行维护"),
                new DictDataItem("签署日期", "signDate", "模板变量值，由用户自行维护"),
                new DictDataItem("A的TAx", "partyATaxNo", "模板变量值，由用户自行维护"),
                new DictDataItem("B的TAx", "partyBTaxNo", "模板变量值，由用户自行维护")

        );
    }

    @Data
    @AllArgsConstructor
    private static class DictDataItem {
        private String k;
        private String v;
        private String remark;
    }
}
