import { CloudObject } from "./ncloud";

export class CloudShipu extends CloudObject {
    constructor() {
        super("seshipu"); // 假设 seshipu 类在 Parse 中是 "seshipu"
    }

    /** 获取所有 `seshipu` 信息 */
    async getAllShipu() {
        const url = "http://dev.fmode.cn:1337/parse/classes/seshipu";

        const response = await fetch(url, {
            headers: {
                "accept": "*/*",
                "accept-language": "zh-CN,zh;q=0.9",
                "x-parse-application-id": "dev"
            },
            method: "GET",
            mode: "cors",
            credentials: "omit"
        });

        const result = await response?.json();
        if (result?.error) {
            console.error(result?.error);
            return null;
        }

        return result.results.map((item: any) => this.dataToObj(item)); // 假设返回的数据在 `results` 字段中
    }

    /** 获取单个 `seshipu` 信息 */
    async getShipu(id: string) {
        const url = `http://dev.fmode.cn:1337/parse/classes/seshipu/${id}?`;

        const response = await fetch(url, {
            headers: {
                "x-parse-application-id": "dev"
            },
            method: "GET",
            mode: "cors",
            credentials: "omit"
        });

        const result = await response?.json();
        if (result?.error) {
            console.error(result?.error);
            return null;
        }

        return this.dataToObj(result);
    }

    /** 创建 `seshipu` 信息 */
    async saveShipuInfo(shipuData: Record<string, any>) {
        const url = `http://dev.fmode.cn:1337/parse/classes/seshipu`;

        const response = await fetch(url, {
            headers: {
                "Content-Type": "application/json",
                "x-parse-application-id": "dev"
            },
            method: "POST",
            body: JSON.stringify(shipuData),
            mode: "cors",
            credentials: "omit"
        });

        const result = await response?.json();
        if (result?.error) {
            console.error(result?.error);
            return null;
        }

        return this.dataToObj(result);
    }

    /** 更新 `seshipu` 信息 */
    async updateShipuInfo(id: string, updatedData: Record<string, any>) {
        const url = `http://dev.fmode.cn:1337/parse/classes/seshipu/${id}`;

        const response = await fetch(url, {
            headers: {
                "Content-Type": "application/json",
                "x-parse-application-id": "dev"
            },
            method: "PUT",
            body: JSON.stringify(updatedData),
            mode: "cors",
            credentials: "omit"
        });

        const result = await response?.json();
        if (result?.error) {
            console.error(result?.error);
            return null;
        }

        return this.dataToObj(result);
    }

    /** 删除 `seshipu` 信息 */
    async deleteShipuInfo(id: string) {
        const url = `http://dev.fmode.cn:1337/parse/classes/seshipu/${id}`;

        const response = await fetch(url, {
            headers: {
                "x-parse-application-id": "dev"
            },
            method: "DELETE",
            mode: "cors",
            credentials: "omit"
        });

        const result = await response?.json();
        if (result?.error) {
            console.error(result?.error);
            return false;
        }

        return true;
    }

    /** 将查询结果转换为 CloudObject */
    private dataToObj(result: any): CloudObject {
        let shipuObject = new CloudObject(this.className);
        shipuObject.set(result);
        shipuObject.id = result.objectId;
        shipuObject.createdAt = result.createdAt;
        shipuObject.updatedAt = result.updatedAt;
        return shipuObject;
    }
}
