export default function () {
  async function getAll({
    page = 1,
    limit = 10,
    sort = "asc",
    search = "",
    org = "",
    site = "",
    dateTo = "",
    dateFrom = "",
    type = "",
    displayNoCheckOut = false,
    status = "",
  } = {}) {
    return await useNuxtApp().$api<Record<string, any>>("/api/people", {
      method: "GET",
      query: {
        page,
        limit,
        sort,
        search,
        org,
        site,
        dateTo,
        dateFrom,
        type,
        status,
      },
    });
  }

  async function findPersonByNRIC(
    nric: string
  ): Promise<null | Partial<TPeople>> {
    return await $fetch<Record<string, any>>(`/api/people/nric/${nric}`, {
      method: "GET",
    });
  }

  async function findPersonByNRICMultipleResult(nric: string, site: string) {
    return await $fetch<Record<any, any>>(`/api/people/all-nric`, {
      method: "GET",
      query: { nric, site },
    });
  }

  async function findPersonByContact(
    contact: string
  ): Promise<null | Partial<TPeople>> {
    return await $fetch<Record<string, any>>(`/api/people/contact/${contact}`, {
      method: "GET",
    });
  }
  async function findUsersByPlateNumber(
    plateNumber: string
  ): Promise<null | Partial<TPeople>> {
    return await $fetch<Record<string, any>>(
      `/api/people/plateNumber/${plateNumber}`,
      {
        method: "GET",
        query: { limit: 20 },
      }
    );
  }

  async function searchCompanyList(
    company: string
  ): Promise<null | Partial<TPeople>> {
    return await $fetch<Record<string, any>>("/api/people/company", {
      method: "GET",
      query: { search: company },
    });
  }

  async function create(payload: Partial<TPeoplePayload>) {
    return await useNuxtApp().$api<Record<string, any>>("/api/people", {
      method: "POST",
      body: payload,
    });
  }

  async function updateById(_id: string, payload: Partial<TPeoplePayload>) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/people/id/${_id}`,
      {
        method: "PUT",
        body: payload,
      }
    );
  }

  async function deleteById(_id: string) {
    return await useNuxtApp().$api<Record<string, any>>(`/api/people/${_id}`, {
      method: "PUT",
    });
  }

  async function getPeopleByUnit(
    _id: string,
    { status = "active", type = "" } = {}
  ) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/people/unit/${_id}`,
      {
        method: "GET",
        query: { status, type },
      }
    );
  }
  async function reviewResidentPerson(
    _id: string,
    payload: Partial<TPeoplePayload>
  ) {
    return await useNuxtApp().$api<Record<string, any>>(`/api/people/${_id}`, {
      method: "PATCH",
      body: payload,
    });
  }

  return {
    create,
    getAll,
    updateById,
    deleteById,
    findPersonByNRIC,
    findPersonByNRICMultipleResult,
    findPersonByContact,
    getPeopleByUnit,
    searchCompanyList,
    findUsersByPlateNumber,
    reviewResidentPerson,
  };
}
