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,
} 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,
  });
};
