export default function () {
  type TVisitorSelection = {
    label: string;
    value: TVisitorType;
  };

  const visitorSelection: TVisitorSelection[] = [
    { label: "Drive-in", value: "guest" },
    { label: "Contractor", value: "contractor" },
    { label: "Walk-In", value: "walk-in" },
    { label: "Delivery", value: "delivery" },
    { label: "Pick-Up", value: "pick-up" },
    { label: "Drop-Off", value: "drop-off" },
  ];

  const typeFieldMap: Record<
    TVisitorType,
    (keyof TVisitorPayload | "delivery-company")[]
  > = {
    guest: [
      "name",
      "nric",
      "contact",
      "block",
      "plateNumber",
      "level",
      "unit",
      "unitName",
      "remarks",
    ],
    contractor: [
      "contractorType",
      "name",
      "nric",
      "company",
      "contact",
      "plateNumber",
      "block",
      "level",
      "unit",
      "unitName",
      "remarks",
    ],
    "walk-in": [
      "name",
      "company",
      "nric",
      "contact",
      "block",
      "level",
      "unit",
      "unitName",
      "remarks",
    ],
    delivery: [
      "attachments",
      "name",
      "deliveryType",
      "delivery-company",
      "nric",
      "contact",
      "plateNumber",
      "block",
      "level",
      "unit",
      "unitName",
      "remarks",
    ],
    "pick-up": ["plateNumber", "block", "remarks"],
    "drop-off": ["plateNumber", "block", "remarks"],
  };

  const contractorTypes = [
    { title: "Estate Contractor", value: "estate-contractor" },
    { title: "Home Contractor", value: "home-contractor" },
    { title: "Property Agent", value: "property-agent" },
    { title: "House Mover", value: "house-mover" },
  ];

  type GetVisitorsParams = {
    page?: number;
    limit?: number;
    order?: "asc" | "desc";
    search?: string;
    org?: string;
    site?: string;
    dateTo?: string;
    dateFrom?: string;
    type?: string;
    status?: string;
    checkedOut?: boolean;
    plateNumber?: string;
    tab?: string;
  };

  async function getVisitors({
    page = 1,
    limit = 10,
    order = "desc",
    search = "",
    org = "",
    site = "",
    dateTo = "",
    dateFrom = "",
    type = "",
    status = "",
    // status = "registered"
    plateNumber = "",
    checkedOut,
    tab,
  }: GetVisitorsParams = {}) {
    return await useNuxtApp().$api<Record<string, any>>(
      "/api/visitor-transactions",
      {
        method: "GET",
        query: {
          page,
          limit,
          search,
          org,
          site,
          dateTo,
          dateFrom,
          type,
          status,
          checkedOut,
          plateNumber,
          order,
          tab,
        },
      }
    );
  }

  async function createVisitor(payload: Partial<TVisitorPayload>) {
    return await useNuxtApp().$api<Record<string, any>>(
      "/api/visitor-transactions",
      {
        method: "POST",
        body: payload,
      }
    );
  }

  async function updateVisitor(_id: string, payload: Partial<TVisitorPayload>) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/visitor-transactions/id/${_id}`,
      {
        method: "PUT",
        body: payload,
      }
    );
  }

  async function deleteVisitor(_id: string) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/visitor-transactions/id/${_id}`,
      {
        method: "DELETE",
      }
    );
  }

  function validateVisitorQrCode(site: string, id: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/visitors/visitor-qr-code/${id}?site=${site}`
    );
  }

  async function updateVisitorPassKey(_id: string, payload: TPassKeyPayload[]) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/visitor-transactions/replace-keys/id/${_id}`,
      {
        method: "PUT",
        body: payload,
      }
    );
  }

  function visitorDataFromScannedQRCodeCheckIn(payload: any) {
    return useNuxtApp().$api<Record<string, any>>("/api/visitors/update-one", {
      method: "PUT",
      body: payload,
    });
  }

  return {
    typeFieldMap,
    visitorSelection,
    contractorTypes,
    createVisitor,
    getVisitors,
    updateVisitor,
    deleteVisitor,
    validateVisitorQrCode,
    updateVisitorPassKey,
    visitorDataFromScannedQRCodeCheckIn,
  };
}
