export default function useCustomerSite() {
  function add(payload: TCustomerSite) {
    return useNuxtApp().$api<Record<string, any>>("/api/customer-sites", {
      method: "POST",
      body: payload,
    });
  }

  function addViaInvite(invite: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/customer-sites/invite/${invite}`,
      {
        method: "POST",
      }
    );
  }


  async function getAll({
    page = 1,
    search = "",
    limit = 10,
    org = "",
    status = "active",
    sort = "",
    order = "asc",
  }: {
    page?: number;
    search?: string;
    limit?: number;
    org?: string;
    status?: string;
    sort?: string;
    order?: string;
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>("/api/customer-sites", {
      method: "GET",
      query: { page, search, limit, sort, order, status, org },
    });
  }

  function getBySiteAsServiceProvider(site: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/customer-sites/service-provider/${site}`,
      {
        method: "GET",
      }
    );
  }

  function getCustomerSites (org: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/customer-sites?page=1&search=&limit=10&sort=&order=asc&status=active&org=${org}`,
      {
        method: 'GET'
      }
    )
  }
  async function getById(id: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/customer-sites/${id}`,
      {
        method: "GET",
      }
    );
  }
  async function updateById(
  id: string,
  payload: Partial<TCustomerSite>,
) {
  return useNuxtApp().$api<Record<string, any>>(
    `/api/customer-sites/${id}`,
    {
      method: "PUT",
      body: payload,
    }
  );
}

async function deleteById(id: string) {
  return useNuxtApp().$api<Record<string, any>>(
    `/api/customer-sites/${id}`,
    {
      method: "DELETE",
    }
  );
}

  return {
    add,
    getAll,
    addViaInvite,
    getBySiteAsServiceProvider,
    getCustomerSites,
    getById,
    updateById,
    deleteById,
  };
}
