export default function () {
  async function getSiteById(siteId: string) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/sites/${siteId}`,
      {
        method: "GET",
      }
    );
  }

  async function getSiteLevels(siteId: string, query: { block: number }) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/buildings/site/${siteId}`,
      {
        method: "GET",
        query: query,
      }
    );
  }

  async function getSiteUnits(siteId: string, block: number, level: string) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/building-units/site/${siteId}/block/${block}/level/${level}`,
      {
        method: "GET",
      }
    );
  }

  async function updateSite(siteId: string, payload: { block: number }) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/sites/${siteId}/block`,
      {
        method: "PUT",
        body: payload,
      }
    );
  }
  async function updateSitebyId(
    siteId: string,
    payload: { field?: string; value?: number, deliveryCompanyList?: string[] }
  ) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/sites/id/${siteId}`,
      {
        method: "PATCH",
        body: payload,
      }
    );
  }
  async function addCamera(camera: TSiteCamera) {
    return await useNuxtApp().$api<Record<string, any>>(`/api/site-cameras`, {
      method: "POST",
      body: camera,
    });
  }

  async function updateSiteCamera(
    id: string,
    value: Partial<
      Pick<
        TSiteCamera,
        | "host"
        | "category"
        | "guardPost"
        | "password"
        | "username"
        | "direction"
        | "status"
      >
    >
  ) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/site-cameras/id/${id}`,
      {
        method: "PATCH",
        body: value,
      }
    );
  }

  async function deleteSiteCameraById(id: string) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/site-cameras/id/${id}`,
      {
        method: "DELETE",
      }
    );
  }

  async function getAllSiteCameras(payload: {
    site: string;
    type?: string;
    page?: number;
  }) {
    return await useNuxtApp().$api<Record<string, any>>(`/api/site-cameras`, {
      method: "GET",
      query: payload,
    });
  }

  async function updateSiteInformation(
    siteId: string,
    payload: { bgImage: string; description: string; docs: { id: string; name: string }[] }
  ) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/sites/information/id/${siteId}`,
      {
        method: "PATCH",
        body: payload,
      }
    );
  }

  async function setSiteGuardPosts(siteId: string, value: number) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/sites/guard-post/id/${siteId}`,
      {
        method: "PATCH",
        body: { guardPost: value },
      }
    );
  }

  async function getOvernightParkingAvailability(siteId: string): Promise<{ data: TOvernightParkingAvailability }> {
    return await useNuxtApp().$api<TOvernightParkingAvailability>(
      `/api/overnight-parking-approval-settings/site/${siteId}`,
      {
        method: "GET",
      }
    );
  }

  async function updateOvernightParkingAvailability(siteId: string, payload: TOvernightParkingAvailability) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/overnight-parking-approval-settings/site/${siteId}`,
      {
        method: "PUT",
        body:  JSON.stringify(payload),
      }
    );
  }

  async function getOvernightParkingAvailabilityV2(
    siteId: string,
    userType = "site"
  ) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/overnight-parking-approval-settings2/v1`,
      {
        method: "PATCH",
        body: JSON.stringify({ site: siteId, userType }),
      }
    );
  }

  async function createOrUpdateOvernightParkingAvailabilityV2(
    siteId: string,
    payload: Record<string, any>,
    userType = "site"
  ) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/overnight-parking-approval-settings2/v1`,
      {
        method: "POST",
        body: JSON.stringify({ site: siteId, userType, ...payload }),
      }
    );
  }

  return {
    getSiteById,
    getSiteLevels,
    getSiteUnits,
    updateSite,
    addCamera,
    getAllSiteCameras,
    setSiteGuardPosts,
    updateSiteCamera,
    deleteSiteCameraById,
    updateSitebyId,
    updateSiteInformation,
    getOvernightParkingAvailability,
    updateOvernightParkingAvailability,
    getOvernightParkingAvailabilityV2,
    createOrUpdateOvernightParkingAvailabilityV2,
  };
}
