export default function useAttendance() {
  function getAttendances({
    site = "",
    page = 1,
    limit = 10,
    search = "",
    serviceType = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/attendances/site/${site}`,
      {
        method: "GET",
        query: { page, limit, search, serviceType },
      }
    );
  }

  function getMyAttendances({
    site = "",
    page = 1,
    limit = 10,
    search = "",
    serviceType = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/attendances/site/${site}/user`,
      {
        method: "GET",
        query: { page, limit, search, serviceType },
      }
    );
  }

  function getMyAttendanceById(id: string) {
    return useNuxtApp().$api<Record<string, any>>(`/api/attendances/id/${id}`, {
      method: "GET",
    });
  }

  function checkIn(site: string, payload: TAttendanceCheckIn) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/attendances/site/${site}/check-in`,
      {
        method: "POST",
        body: payload,
      }
    );
  }

  function checkOut(id: string, payload: TAttendanceCheckOut) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/attendances/id/${id}/check-out`,
      {
        method: "PUT",
        body: payload,
      }
    );
  }

  function getAttendanceSettings(site: string, serviceType: string = "") {
    return useNuxtApp().$api<TAttendanceSettings>(
      `/api/attendance-settings/site/${site}`,
      {
        method: "GET",
        query: { serviceType },
      }
    );
  }

  function updateAttendanceSettings(
    site: string,
    payload: TAttendanceSettings,
    serviceType: string = ""
  ) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/attendance-settings/site/${site}`,
      {
        method: "PUT",
        body: { ...payload, serviceType },
      }
    );
  }

  return {
    getAttendances,
    getMyAttendances,
    getMyAttendanceById,
    checkIn,
    checkOut,
    getAttendanceSettings,
    updateAttendanceSettings,
  };
}
