import { useQuery } from "@tanstack/react-query";
import { apiGet } from "../../../lib/api/methods";
import { PACKAGES_ENDPOINTS } from "./endpoints";

export type Package = {
  uuid: string;
  name: string;
  description: string;
  credits: number;
  price: number;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
};

type PackageResponse = Package[];

export default function usePackagesFetch({
  limit,
}: {
  limit?: number;
} = {}) {
  const { data, isFetching, isError, error } = useQuery({
    queryKey: ["packages", limit],
    queryFn: async () => {
      const res = await apiGet<PackageResponse>(PACKAGES_ENDPOINTS.PACKAGES, {
        limit,
      });
      return res.data;
    },
    staleTime: 10 * 60 * 1000,
    gcTime: 30 * 60 * 1000,
    retry: false,
  });

  return { packages: data, loading: isFetching, error, isError };
}
