import {
  POST_ACK_API,
  GET_ALERT_FORMID_API,
  GET_ALERT_LOG_API,
  GET_ALERT_LOG_LIST_API,
  POST_OPERATION_RECORD_API,
  GET_ALERT_SUB_LIST_API,
  GET_OUTLIERS_LIST_API,
  GET_OUTLIERS_DEVLIST_API,
  GET_OUTLIERS_POINTS_API,
  POST_OUTLIERS_SETTING_API,
  DELETE_OUTLIERS_SETTING_API,
  GET_FACTOR_API,
  GET_ALERT_MEMBER_LIST_API,
  GET_ALERT_MEMBER,
  POST_ALERT_MEMBER,
  DELETE_ALERT_MEMBER,
  GET_NOTICE_LIST_API,
  GET_SHOW_ALERT_API,
  GET_ALERT_SCHEDULE_LIST_API,
  POST_ALERT_SCHEDULE,
  DELETE_ALERT_SCHEDULE,
  POST_ALERT_MQTT_REFRESH,
  POST_QUERY_ALARM_LOG,
} from "./api";
import instance from "@/util/request";
import apihandler from "@/util/apihandler";

export const getAlertFormId = async (uuid) => {
  const res = await instance.post(GET_ALERT_FORMID_API, uuid);
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAlertLog = async ({
  Start_date,
  End_date,
  isRecovery,
  device_name_tag,
}) => {
  const res = await instance.post(GET_ALERT_LOG_API, {
    Start_date,
    End_date,
    isRecovery,
    device_name_tag,
  });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAlertLogList = async (building_guid) => {
  const res = await instance.post(GET_ALERT_LOG_LIST_API, {
    building_guid,
  });
  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const postOperationRecord = async (formData) => {
  const res = await instance.post(POST_OPERATION_RECORD_API, formData);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAlertSubList = async (building_guid) => {
  const res = await instance.post(GET_ALERT_SUB_LIST_API, {
    building_guid,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAlarmMemberList = async () => {
  const res = await instance.post(GET_ALERT_MEMBER_LIST_API, {});

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getNoticeList = async (lang) => {
  const res = await instance.post(GET_NOTICE_LIST_API, { lang });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const postAlertMember = async (data) => {
  const res = await instance.post(POST_ALERT_MEMBER, data);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const deleteAlarmMember = async (id) => {
  try {
    const res = await instance.post(DELETE_ALERT_MEMBER, { id });
    console.log("Delete Alarm Member Response:", res);
    return {
      isSuccess: res.code === "0000",
      msg: res.msg || "刪除成功",
    };
  } catch (error) {
    console.error("API request failed", error);
    return { isSuccess: false, msg: "API request failed" };
  }
};

export const getAlarmMember = async (data) => {
  try {
    const res = await instance.post(GET_ALERT_MEMBER, data);
    return res.data;
  } catch (error) {
    console.error("API request failed", error);
    return { isSuccess: false, msg: "API request failed" };
  }
};

export const getOutliersList = async (id) => {
  const res = await instance.post(GET_OUTLIERS_LIST_API, id);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getOutliersDevList = async (id) => {
  const res = await instance.post(GET_OUTLIERS_DEVLIST_API, id);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getOutliersPoints = async (id) => {
  const res = await instance.post(GET_OUTLIERS_POINTS_API, id);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getFactors = async () => {
  const res = await instance.post(GET_FACTOR_API);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const postOutliersSetting = async (data) => {
  const res = await instance.post(POST_OUTLIERS_SETTING_API, data);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const delOutliersSetting = async (Id) => {
  const res = await instance.post(DELETE_OUTLIERS_SETTING_API, {
    Id,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getShowAlarm = async () => {
  const res = await instance.post(GET_SHOW_ALERT_API);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const getAlarmScheduleList = async () => {
  const res = await instance.post(GET_ALERT_SCHEDULE_LIST_API, {});

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const postAlertSchedule = async (data) => {
  const res = await instance.post(POST_ALERT_SCHEDULE, data);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

export const deleteAlarmSchedule = async (id) => {
  try {
    const res = await instance.post(DELETE_ALERT_SCHEDULE, { id });
    return {
      isSuccess: res.code === "0000",
      msg: res.msg || "刪除成功",
    };
  } catch (error) {
    console.error("API request failed", error);
    return { isSuccess: false, msg: "API request failed" };
  }
};

export const postMQTTRefresh = async () => {
  const res = await instance.post(POST_ALERT_MQTT_REFRESH);

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};

/**
 * 查詢裝置告警紀錄（支援起訖日）
 * @param {{ id: number, start_date: string, end_date: string }} payload
 *   - id: main_id（裝置/攝影機/樓層主鍵等）
 *   - start_date: "YYYY-MM-DD"
 *   - end_date: "YYYY-MM-DD"
 * @returns {Promise<any>} apihandler 標準回傳（成功時回 data 陣列）
 */
export const postQueryAlarmLog = async ({ id, start_date, end_date }) => {
  const res = await instance.post(POST_QUERY_ALARM_LOG, {
    id,
    start_date,
    end_date,
  });

  return apihandler(res.code, res.data, {
    msg: res.msg,
    code: res.code,
  });
};
