export default function useVehicle() {
  async function getVehicles({
    search = "",
    page = 1,
    limit = 10,
    order = "desc",
    type = "",
    category = "",
    site = "",
  } = {}) {
    return await useNuxtApp().$api<Record<string, any>>("/api/vehicles", {
      method: "GET",
      query: { search, page, limit, order, type, category, site },
    });
  }

  async function getVehicleById(id: string) {
    return await useNuxtApp().$api<Record<string, any>>(`/api/vehicles/${id}`, {
      method: "GET",
    });
  }

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

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

  async function getCustomSeasonPassTypes(site: string) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/vehicles/site/${site}`,
      {
        method: "GET",
      }
    );
  }

  async function searchMultipleVehiclesByUnitId({unitId, page = 1, limit = 10}: {unitId: string, page?: number, limit?: number}) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/vehicles/unit/${unitId}`,
      {
        method: "GET",
        query: { page, limit },
      }
    );
  }
  
  interface IDeleteVehicleParams {
    site: string;
    id: string;
    recno?: string;
    type: "whitelist" | "blocklist";
  }

  async function deleteVehicle({ site, id, recno, type }: IDeleteVehicleParams) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/vehicles/${id}`,
      {
        method: "PUT",
        body: {
          site,
          recno,
          type,
        },
      }
    );
  }

  async function approveVehicle({ site, org, id }: { site: string, org: string, id: string}){
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/vehicles/approve/${id}`,
      {
        method: "PUT",
        body: {
          site,
          org,
        },
      }
    );
  }

  async function getVehicleByNRIC(search: string) {
    return await useNuxtApp().$api<Record<string, any>>(
      `/api/vehicles/nric`,
      {
        method: "GET",
        query: {
          search,
        },
      }
    );
  }


  function formatVehicleStatus(status: TVehicleStatus) {
    let label = ""
    let color = ""
    switch (status) {
        case 'pending':
            label = "Pending"
            color = "orange"
            break;
        case 'active':
            label = "Active"
            color = "green"
            break;
        case 'rejected':
            label = "Rejected"
            color = "red"
            break;
        case 'deleted':
            label = "Deleted"
            color = "grey"
            break;
        case 'inactive':
            label = "Deleted"
            color = "grey"
            break;
        default:
            label = status?.charAt(0).toUpperCase() + status?.slice(1) || ""
            color = "grey"
    }

    return { label, color }
}

// fetch specific plateNumber Vehicle data

function getSpecificVehicleById(id: string){
  return useNuxtApp().$api<Record<string, any>>(
    `/api/vehicles/specific/${id}`,
    {
      method: "GET",
    }
  );
}


  return {
    getVehicles,
    addVehicle,
    getCustomSeasonPassTypes,
    getVehicleById,
    updateVehicle,
    deleteVehicle,
    getVehicleByNRIC,
    formatVehicleStatus,
    approveVehicle,
    searchMultipleVehiclesByUnitId,
    getSpecificVehicleById
  };
}
