package com.gzzm.lobster.tool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * SchemaBuilder —— 最小 JSON Schema 构造工具 / Minimal JSON Schema helper.
 *
 * <p>避免手动写冗长嵌套 Map；仅覆盖 object + 基础类型，够内置工具定义使用。
 * Covers {@code object} + primitive types — sufficient for built-in tool schemas.
 */
public final class SchemaBuilder {

    private final Map<String, Object> root = new LinkedHashMap<>();
    private final Map<String, Object> properties = new LinkedHashMap<>();
    private final List<String> required = new ArrayList<>();

    private SchemaBuilder() {
        root.put("type", "object");
        root.put("properties", properties);
    }

    public static SchemaBuilder obj() { return new SchemaBuilder(); }

    public SchemaBuilder prop(String name, String type, String description) {
        Map<String, Object> p = new LinkedHashMap<>();
        p.put("type", type);
        if (description != null) p.put("description", description);
        properties.put(name, p);
        return this;
    }

    public SchemaBuilder propStringMax(String name, String description, int maxLength) {
        Map<String, Object> p = new LinkedHashMap<>();
        p.put("type", "string");
        if (maxLength > 0) p.put("maxLength", maxLength);
        if (description != null) p.put("description", description);
        properties.put(name, p);
        return this;
    }

    public SchemaBuilder propEnum(String name, String description, String... values) {
        Map<String, Object> p = new LinkedHashMap<>();
        p.put("type", "string");
        p.put("enum", Arrays.asList(values));
        if (description != null) p.put("description", description);
        properties.put(name, p);
        return this;
    }

    public SchemaBuilder propInt(String name, String description) {
        return prop(name, "integer", description);
    }

    public SchemaBuilder propBool(String name, String description) {
        return prop(name, "boolean", description);
    }

    public SchemaBuilder propObject(String name, String description, Map<String, Object> nestedSchema) {
        Map<String, Object> p = new LinkedHashMap<>(nestedSchema);
        if (description != null) p.put("description", description);
        properties.put(name, p);
        return this;
    }

    public SchemaBuilder propArray(String name, String description, Map<String, Object> itemsSchema) {
        Map<String, Object> p = new LinkedHashMap<>();
        p.put("type", "array");
        if (itemsSchema != null) p.put("items", itemsSchema);
        if (description != null) p.put("description", description);
        properties.put(name, p);
        return this;
    }

    public SchemaBuilder required(String... names) {
        for (String n : names) required.add(n);
        return this;
    }

    public Map<String, Object> build() {
        Map<String, Object> out = new LinkedHashMap<>(root);
        if (!required.isEmpty()) out.put("required", required);
        return out;
    }
}
