export default function useAccessManagement() {
  function getDoorAccessLevels(acmUrl?: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/door-access-levels`,
      {
        method: "GET",
        query: {
          ...(acmUrl ? { acm_url: acmUrl } : {}),
        },
      }
    );
  }

  function getLiftAccessLevels(acmUrl?: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/lift-access-levels`,
      {
        method: "GET",
        query: {
          ...(acmUrl ? { acm_url: acmUrl } : {}),
        },
      }
    );
  }

  function getAccessGroups(acmUrl?: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/access-groups`,
      {
        method: "GET",
        query: {
          ...(acmUrl ? { acm_url: acmUrl } : {}),
        },
      }
    );
  }

  function addPhysicalCard(payload: Record<string, any>) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/physical-card`,
      {
        method: "POST",
        body: payload,
      }
    );
  }

  function addNonPhysicalCard(payload: Record<string, any>) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/non-physical-card`,
      {
        method: "POST",
        body: payload,
      }
    );
  }

  function getAllAccessCardsCounts(params: { site: string; userType: string }) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/all-access-cards-counts`,
      {
        method: "GET",
        query: {
          site: params.site,
          userType: params.userType,
        },
      }
    );
  }

  function getUserTypeAccessCards(
    params: {
      page?: number;
      limit?: number;
      search?: string;
      organization?: string;
      userType?: string;
      site?: string;
    } = {}
  ) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/user-type-access-cards`,
      {
        method: "GET",
        query: {
          ...(params.page ? { page: params.page } : {}),
          ...(params.limit ? { limit: params.limit } : {}),
          ...(params.search ? { search: params.search } : {}),
          ...(params.organization ? { organization: params.organization } : {}),
          ...(params.userType ? { userType: params.userType } : {}),
          ...(params.site ? { site: params.site } : {}),
        },
      }
    );
  }

  function bulkPhysicalAccessCard(params: { site: string; file: File }) {
    const formData = new FormData();
    formData.append("file", params.file);
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/bulk-upload`,
      {
        method: "POST",
        query: { site: params.site },
        body: formData,
      }
    );
  }

  function getAvailableAccessCards(params: {
    site: string;
    userType: string;
    type: string;
    accessLevel: string | null;
    liftAccessLevel: string | null;
  }) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/access-and-lift-cards`,
      {
        method: "GET",
        query: {
          site: params.site,
          userType: params.userType,
          type: params.type,
          ...(params.accessLevel != null ? { accessLevel: params.accessLevel } : {}),
          ...(params.liftAccessLevel != null ? { liftAccessLevel: params.liftAccessLevel } : {}),
        },
      }
    );
  }

  function assignAccessCard(payload: {
    units: string[];
    quantity: number;
    type: string;
    site: string;
    userType: string;
    accessLevel: string | null;
    liftAccessLevel: string | null;
  }) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/assign-access-card`,
      {
        method: "POST",
        body: payload,
      }
    );
  }

  function deleteCard(payload: { cardId: string; remarks: string }) {
    return useNuxtApp().$api(`/api/access-management/delete-card`, {
      method: "PATCH",
      body: payload,
    });
  }

  function getCardHistory(cardId: string) {
    return useNuxtApp().$api<Record<string, any>[]>(
      `/api/access-management/card-history/${cardId}`,
      { method: "GET" }
    );
  }

  function getCardDetails(params: { siteId: string; cardId: string }) {
    return useNuxtApp().$api<{ message: string; data: Record<string, any> }>(
      `/api/access-management/card-details`,
      {
        method: "GET",
        query: { siteId: params.siteId, cardId: params.cardId },
      }
    );
  }

  function saveVisitorAccessCardQrTag(
    siteId: string,
    payloads: Array<{
      _id: string;
      cardNo: string;
      qrTag: string;
      qrTagCardNo: string;
    }>
  ) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/qr-tag/${siteId}`,
      {
        method: "POST",
        body: payloads,
      }
    );
  }

  function getAllVisitorAccessCardsQrTags(site: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/qr-tag`,
      {
        method: "GET",
        query: { site },
      }
    );
  }

  function getVisitorAccessCards(
    params: {
      page?: number;
      limit?: number;
      site?: string;
      search?: string;
    } = {}
  ) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/visitor-access-cards`,
      {
        method: "GET",
        query: {
          type: "Visitor/Resident",
          ...(params.page ? { page: params.page } : {}),
          ...(params.limit ? { limit: params.limit } : {}),
          ...(params.site ? { site: params.site } : {}),
          ...(params.search ? { search: params.search } : {}),
        },
      }
    );
  }

  function getAvailableContractorCards(params: {
    type: "NFC" | "QRCODE";
    siteId: string;
    unitId: string;
    page?: number;
    limit?: number;
  }) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/available-card-contractor/${params.type}`,
      {
        method: "GET",
        query: {
          siteId: params.siteId,
          unitId: params.unitId,
          page: params.page ?? 1,
          limit: params.limit ?? 100,
        },
      }
    );
  }

  function generateQrVms(params: {
    site: string;
    unitId: string;
    quantity: number;
  }) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/generate-qr-vms`,
      {
        method: "POST",
        body: {
          site: params.site,
          unitId: params.unitId,
          quantity: params.quantity,
        },
      }
    );
  }

  function createVisitorPass(payload: {
    site: string;
    unitId: string;
    quantity: number;
    type: string;
    nfcCards: { _id: string; cardNo: string }[];
    acm_url: string;
    visitorId: string;
  }) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/visitor`,
      {
        method: "POST",
        body: payload,
      }
    );
  }

  function getBlockLevelUnitList(siteId: string) {
    return useNuxtApp().$api<{ message: string; data: any[] }>(
      `/api/access-management/block-level-unit-list`,
      {
        method: "GET",
        query: { site: siteId },
      }
    );
  }

  function signQr(payload: { cardId: string; purpose: string }) {
    return useNuxtApp().$api<{ message: string; data: string }>(
      `/api/access-management/sign-qr`,
      {
        method: "POST",
        body: payload,
      }
    );
  }

  function getResidents(params: { orgId: string; siteId: string; unitId: string }) {
    return useNuxtApp().$api<{ data: Record<string, any>[] }>(
      `/api/access-management/residents`,
      { method: "GET", query: params }
    );
  }

  function assignUser(payload: { assignees: string[]; unit: string; type: string; acm_url: string }) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/access-management/assign-user`,
      { method: "POST", body: payload }
    );
  }

  return {
    getResidents,
    assignUser,
    getDoorAccessLevels,
    getLiftAccessLevels,
    getAccessGroups,
    addPhysicalCard,
    addNonPhysicalCard,
    getUserTypeAccessCards,
    getAllAccessCardsCounts,
    bulkPhysicalAccessCard,
    assignAccessCard,
    getAvailableAccessCards,
    deleteCard,
    getCardHistory,
    getCardDetails,
    getVisitorAccessCards,
    saveVisitorAccessCardQrTag,
    getAllVisitorAccessCardsQrTags,
    getAvailableContractorCards,
    generateQrVms,
    createVisitorPass,
    signQr,
    getBlockLevelUnitList,
  };
}
