export const useCalendarStore = defineStore("calendar", {
  // arrow function recommended for full type inference
  state: () => ({
    availableCalendars: [
      {
        color: "success",
        label: "展延工期",
      },
      {
        color: "info",
        label: "停工",
      },
    ],
    selectedCalendars: ["展延工期", "停工"],
    durationUnits: [
      {
        label: "全天",
        value: "full day",
      },
      {
        label: "半天",
        value: "half day",
      },
    ],
    dateTypes: [
      {
        label: "一天",
        value: "one day",
      },
      {
        label: "多天",
        value: "multiple days",
      },
      {
        label: "多筆不連續日期",
        value: "inconcecutive days",
      },
    ],
    timeFilterOptions: [
      { label: "時間範圍", value: "range" },
      { label: "固定星期幾", value: "weekday" },
    ],
    timeframe: [
      { label: "整個專案", value: "whole project" },
      { label: "自行輸入時間範圍", value: "select range yourself" },
    ],
    weekdays: [
      { label: "星期一", value: "Monday" },
      { label: "星期二", value: "Tuesday" },
      { label: "星期三", value: "Wednesday" },
      { label: "星期四", value: "Thursday" },
      { label: "星期五", value: "Friday" },
      { label: "星期六", value: "Saturday" },
      { label: "星期日", value: "Sunday" },
    ],
  }),
  actions: {
    async fetchEvents(project_number) {
      try {
        const response = await $api("/pdet/calendar/read", {
          method: "POST",
          body: JSON.stringify({ project_number: project_number }),
        });
        return response;
      } catch (error) {
        console.error("Error adding event:", error);
        throw error;
      }
    },
    async addEvent(event) {
      try {
        const response = await $api("/pdet/calendar/create", {
          method: "POST",
          body: event,
        });
        // 處理事件創建成功的情況
      } catch (error) {
        console.error("Error adding event:", error);
        throw error;
      }
    },
    async addEvents(events) {
      try {
        const response = await $api("/pdet/calendar/createBatch", {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({ events }),
        });
      } catch (error) {
        console.error("Error adding event:", error);
        throw error;
      }
    },
    async updateEvent(event) {
      try {
        const response = await $api("/pdet/calendar/update", {
          method: "POST",
          body: JSON.stringify(event),
        });
      } catch (error) {
        console.error("Error deleting event:", error);
        throw error;
      }
    },
    async removeEvent(event) {
      try {
        const response = await $api("/pdet/calendar/delete", {
          method: "DELETE",
          body: JSON.stringify(event),
        });
      } catch (error) {
        console.error("Error deleting event:", error);
        throw error;
      }
    },
  },
});
