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。
 * <p>生成草稿时必填：worker_source、worker_count、work_days、work_type、work_location、
 * settlement_mode、unit_amount。total_amount 非必填。多工种时仍可通过 worker_groups 整表更新。</p>
 */
public final class DraftSchemaRegistry {

    /** 生成/补全阶段必填（扁平字段名，供 Agent missing_fields 使用） */
    private static final List<String> REQUIRED_FIELD_KEYS = Collections.unmodifiableList(Arrays.asList(
            "worker_source",
            "worker_count",
            "work_days",
            "work_type",
            "work_location",
            "settlement_mode",
            "unit_amount"
    ));

    /** 组内必填（对应扁平字段，不含 worker_source） */
    private static final List<String> GROUP_REQUIRED_KEYS = Collections.unmodifiableList(Arrays.asList(
            "worker_count",
            "work_days",
            "work_type",
            "work_location",
            "settlement_mode",
            "unit_amount"
    ));

    private static final List<String> WORKER_SOURCE_OPTIONS = Collections.unmodifiableList(Arrays.asList(
            "existing", "recruit"));

    private static final List<String> SETTLEMENT_MODE_OPTIONS = Collections.unmodifiableList(Arrays.asList(
            "daily", "weekly", "monthly"));

    private static final List<McpDraftSchemaField> SCHEMA_FIELDS;

    static {
        McpDraftSchemaField workerSource = field("worker_source", "临时工来源", "enum", true);
        workerSource.setOptions(WORKER_SOURCE_OPTIONS);

        McpDraftSchemaField settlementMode = field("settlement_mode", "结算方式", "enum", true);
        settlementMode.setOptions(SETTLEMENT_MODE_OPTIONS);

        Map<String, Object> groupItemSchema = new LinkedHashMap<>();
        groupItemSchema.put("fields", Arrays.asList(
                itemField("group_id", "string", false, null),
                itemField("work_type", "string", true, null),
                itemField("worker_count", "integer", true, null),
                itemField("work_days", "number", true, null),
                itemField("work_location", "string", true, null),
                itemField("settlement_mode", "enum", true, SETTLEMENT_MODE_OPTIONS),
                itemField("unit_amount", "decimal", true, null),
                itemField("total_amount", "decimal", false, null),
                itemField("work_content", "text", false, null),
                itemField("work_start_date", "date", false, null),
                itemField("work_end_date", "date", false, null)
        ));
        McpDraftSchemaField workerGroups = field("worker_groups", "用工明细组（多工种）", "array", false);
        workerGroups.setItemSchema(groupItemSchema);

        SCHEMA_FIELDS = Collections.unmodifiableList(Arrays.asList(
                workerSource,
                field("worker_count", "用工人数", "integer", true),
                field("work_days", "工作天数", "number", true),
                field("work_type", "工种", "string", true),
                field("work_location", "工作地点", "string", true),
                settlementMode,
                field("unit_amount", "单价", "decimal", true),
                field("total_amount", "劳务费合计", "decimal", false),
                workerGroups,
                field("title", "草稿标题", "string", 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);
        Map<String, Object> groupRules = new LinkedHashMap<>();
        groupRules.put("min_items", 1);
        groupRules.put("max_items", 10);
        groupRules.put("required_item_fields", GROUP_REQUIRED_KEYS);
        rules.put("worker_groups", groupRules);
        schema.setRules(rules);
        return schema;
    }

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

    public static List<String> groupRequiredFieldKeys() {
        return GROUP_REQUIRED_KEYS;
    }

    public static Map<String, String> fieldLabels() {
        Map<String, String> labels = new HashMap<>();
        for (McpDraftSchemaField field : SCHEMA_FIELDS) {
            labels.put(field.getKey(), field.getLabel());
        }
        labels.put("job_type", "工种");
        labels.put("region", "工作地点");
        labels.put("settlement_cycle", "结算周期");
        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;
    }

    private static Map<String, Object> itemField(String key, String type, boolean required, List<String> options) {
        Map<String, Object> field = new LinkedHashMap<>();
        field.put("key", key);
        field.put("type", type);
        field.put("required", required);
        if (options != null) {
            field.put("options", options);
        }
        return field;
    }
}
