package com.huimv.employment.service.draft;

import com.huimv.employment.service.draft.dto.McpDraftSchema;
import com.huimv.employment.service.draft.dto.McpDraftSchemaField;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 用工草稿字段 Schema 与必填规则（对齐交互规则 v2 §6.3.1）。
 */
public final class DraftSchemaRegistry {

    private static final List<String> REQUIRED_FIELD_KEYS = Collections.unmodifiableList(Arrays.asList(
            "worker_count",
            "total_amount",
            "work_days",
            "work_type",
            "work_location",
            "settlement_mode"
    ));

    private static final List<McpDraftSchemaField> SCHEMA_FIELDS;

    static {
        SCHEMA_FIELDS = Collections.unmodifiableList(Arrays.asList(
                field("worker_count", "用工人数", "integer", true),
                field("total_amount", "预估总支出（元）", "decimal", true),
                field("work_days", "工作天数", "number", true),
                field("work_type", "岗位/工种", "string", true),
                field("work_content", "工作内容", "text", false),
                field("work_location", "工作地点", "string", true),
                field("settlement_mode", "结算方式", "enum", true),
                field("daily_wage", "日薪标准（元）", "decimal", false),
                field("job_requirements", "岗位要求", "text", false),
                field("work_start_date", "计划开工日期", "date", false),
                field("work_end_date", "计划结束日期", "date", false)
        ));
    }

    private DraftSchemaRegistry() {
    }

    public static McpDraftSchema buildSchema() {
        McpDraftSchema schema = new McpDraftSchema();
        schema.setFields(SCHEMA_FIELDS);
        Map<String, Object> rules = new LinkedHashMap<>();
        rules.put("required_fields", REQUIRED_FIELD_KEYS);
        rules.put("settlement_mode_values", Arrays.asList("daily", "piece", "monthly"));
        schema.setRules(rules);
        return schema;
    }

    public static List<String> requiredFieldKeys() {
        return REQUIRED_FIELD_KEYS;
    }

    public static Map<String, String> fieldLabels() {
        Map<String, String> labels = new HashMap<>();
        for (McpDraftSchemaField field : SCHEMA_FIELDS) {
            labels.put(field.getKey(), field.getLabel());
        }
        return labels;
    }

    private static McpDraftSchemaField field(String key, String label, String type, boolean required) {
        McpDraftSchemaField field = new McpDraftSchemaField();
        field.setKey(key);
        field.setLabel(label);
        field.setType(type);
        field.setRequired(required);
        field.setEditable(true);
        return field;
    }
}
