import { notifications } from "@mantine/notifications";

/**
 * 通用 API 请求封装
 * @param {string} url 请求地址
 * @param {object} options fetch 配置
 * @param {string} successMsg 成功提示文案（可选）
 * @returns {Promise<any>} 返回 JSON 数据
 */
export async function apiRequest(url, options = {}, successMsg) {
    try {
        const res = await fetch(url, options);

        if (!res.ok) {
            let msg = "请求失败";
            try {
                const errData = await res.json();
                if (errData && errData.message) msg = errData.message;
            } catch (e) {
                // ignore json parse error
            }
            throw new Error(msg);
        }

        const data = await res.json();

        if (successMsg) {
            notifications.show({
                title: "成功",
                message: successMsg,
                color: "green",
            });
        }

        return data;
    } catch (err) {
        notifications.show({
            title: "操作失败",
            message: err.message || "接口调用失败",
            color: "red",
        });
        // throw err; // 继续抛出错误，方便上层处理
    }

    // 返回 mock 数据
    if (url.includes("/api/v1/nodes")) {
        if (/\/api\/v1\/nodes\/[^\/]+$/.test(url)) {
            return {
                "id": "node1",
                "name": "Node 1",
                "status": "online",
                "config": {
                    // !!! 格式待定, 下发给agent用的
                    "max_connections": 100,
                    "timeout": 30,
                    "log_level": "info"
                },
                "meta_data": {
                    // 元数据： host, ip, 
                    "host": "192.168.1.1",
                    "ip": "192.168.1.1",
                    "region": "shanghai",
                    "zone": "shanghai-1",
                },
                "label": {
                    // 用户或运维人员绑定到节点上的标签，业务属性， tag
                    "tag": "value1",
                },
                "health": {
                    "agent": "activate",
                    "log": "error",
                    "metric": "activate",
                },
                "last_updated": "2023-10-05T12:00:00Z",
                "type": "worker"
            }

        }
        return {
            "total": 30,
            "data": [
                { id: "node1", name: "节点A", status: "healthy", type: "agent", version: "1.0.0" },
                { id: "node2", name: "节点B", status: "warning", type: "agent", version: "1.0.0"},
                { id: "node3", name: "节点C", status: "error", type: "agent", version: "1.0.0"},
                { id: "node4", name: "节点D", status: "healthy", type: "agent", version: "1.0.0"},
                { id: "node5", name: "节点E", status: "warning", type: "agent", version: "1.0.0"},
                { id: "node6", name: "节点F", status: "error", type: "agent", version: "1.0.0"},
                { id: "node7", name: "节点G", status: "healthy", type: "agent", version: "1.0.0"},
                { id: "node8", name: "节点H", status: "warning",  type: "agent", version: "1.0.0"},
            ]
        };
    }

    return [];

}
