// import { CloudObject, CloudQuery, CloudUser } from './ncloud';

// export class CloudSpPlan extends CloudObject {
//   constructor() {
//     super("FPlan"); // 假设表名为 "FPlan"
//   }

//   /** 获取单个SP规划 */
//   async getSpPlan(id: string): Promise<CloudObject | null> {
//     const url = `http://dev.fmode.cn:1337/parse/classes/FPlan/${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);
//   }

//   /** 获取当前登录用户的SP规划信息 */
//   async getCurrentUserSpPlan(): Promise<CloudObject | null> {
//     const cloudUser = new CloudUser();
//     const currentUser = await cloudUser.current();

//     if (!currentUser) {
//       console.error("用户未登录");
//       return null;
//     }

//     const currentUserId = currentUser.objectId;

//     // 通过 _User 的 objectId 查询 FPlan 表中的记录
//     const query = new CloudQuery("FPlan");
//     query.equalTo("user", { "__type": "Pointer", "className": "_User", "objectId": currentUserId });

//     const spPlan = await query.first();
//     return spPlan ? this.dataToObj(spPlan) : null;
//   }

//   /** 创建或保存SP规划信息 */
//   async saveSpPlan(spPlanData: Record<string, any>): Promise<CloudObject | null> {
//     const cloudUser = new CloudUser();
//     const currentUser = await cloudUser.current();

//     if (!currentUser) {
//       console.error("用户未登录");
//       return null;
//     }

//     const userPointer = { "__type": "Pointer", "className": "_User", "objectId": currentUser.objectId };

//     const newSpPlan = {
//       ...spPlanData,
//       user: userPointer // 将当前用户作为外键关联到 FPlan 表
//     };

//     const url = `http://dev.fmode.cn:1337/parse/classes/FPlan`;

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

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

//     return this.dataToObj(result);
//   }

//   /** 更新SP规划信息 */
//   async updateSpPlan(updatedData: Record<string, any>, spPlanId: string): Promise<CloudSpPlan> {
//     const spPlan = new CloudSpPlan();
//     spPlan.id = spPlanId;
//     spPlan.set(updatedData);

//     await spPlan.save(); // 调用 save 方法更新数据
//     return spPlan;
//   }

//   /** 删除SP规划 */
//   async deleteSpPlan(spPlanId: string): Promise<boolean> {
//     const spPlan = new CloudSpPlan();
//     spPlan.id = spPlanId;
//     await spPlan.destroy();

//     return true;
//   }

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


import { CloudObject, CloudQuery, CloudUser } from './ncloud';

export class CloudFPlan extends CloudObject {
  constructor() {
    super("FPlan"); // 假设表名为 "FPlan"
  }

  /** 获取单个FPlan */
  async getFPlan(id: string): Promise<CloudObject | null> {
    const url = `http://dev.fmode.cn:1337/parse/classes/FPlan/${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);
  }

  /** 获取当前登录用户的FPlan信息 */
  async getCurrentUserFPlan(): Promise<CloudObject | null> {
    const cloudUser = new CloudUser();
    const currentUser = await cloudUser.current();

    if (!currentUser) {
      console.error("用户未登录");
      return null;
    }

    const currentUserId = currentUser.objectId;

    // 通过 _User 的 objectId 查询 FPlan 表中的记录
    const query = new CloudQuery("FPlan");
    query.equalTo("user", { "__type": "Pointer", "className": "_User", "objectId": currentUserId });

    const fPlan = await query.first();
    return fPlan ? this.dataToObj(fPlan) : null;
  }

  /** 创建或保存FPlan信息 */
  async saveFPlan(planContent: string): Promise<CloudObject | null> {
    const cloudUser = new CloudUser();
    const currentUser = await cloudUser.current();

    if (!currentUser) {
      console.error("用户未登录");
      return null;
    }

    const userPointer = { "__type": "Pointer", "className": "_User", "objectId": currentUser.objectId };

    const newFPlan = {
      Plan: planContent,  // 将运动计划内容保存在 "Plan" 字段
      user: userPointer // 将当前用户作为外键关联到 FPlan 表
    };

    const url = `http://dev.fmode.cn:1337/parse/classes/FPlan`;

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

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

    return this.dataToObj(result);
  }

  /** 更新FPlan信息 */
  async updateFPlan(updatedData: Record<string, any>, fPlanId: string): Promise<CloudFPlan> {
    const fPlan = new CloudFPlan();
    fPlan.id = fPlanId;
    fPlan.set(updatedData);

    await fPlan.save(); // 调用 save 方法更新数据
    return fPlan;
  }

  /** 删除FPlan */
  async deleteFPlan(fPlanId: string): Promise<boolean> {
    const fPlan = new CloudFPlan();
    fPlan.id = fPlanId;
    await fPlan.destroy();

    return true;
  }

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