/**
 * Catalog Hooks
 * React hooks for catalog functionality
 */

import { useQuery } from "@tanstack/react-query";
import { queryKeys } from "@/lib/api";
import { catalogService } from "../services";

/**
 * Internet catalog composite hook
 * Fetches plans and installations together
 */
export function useInternetCatalog() {
  return useQuery({
    queryKey: queryKeys.catalog.internet.combined(),
    queryFn: () => catalogService.getInternetCatalog(),
    staleTime: 5 * 60 * 1000,
    gcTime: 10 * 60 * 1000,
  });
}

/**
 * SIM catalog composite hook
 * Fetches plans, activation fees, and addons together
 */
export function useSimCatalog() {
  return useQuery({
    queryKey: queryKeys.catalog.sim.combined(),
    queryFn: () => catalogService.getSimCatalog(),
    staleTime: 5 * 60 * 1000,
    gcTime: 10 * 60 * 1000,
  });
}

/**
 * VPN catalog hook
 * Fetches VPN plans and activation fees
 */
export function useVpnCatalog() {
  return useQuery({
    queryKey: queryKeys.catalog.vpn.combined(),
    queryFn: () => catalogService.getVpnCatalog(),
    staleTime: 5 * 60 * 1000,
    gcTime: 10 * 60 * 1000,
  });
}

/**
 * Lookup helpers by SKU
 */
export function useInternetPlan(sku?: string) {
  const { data, ...rest } = useInternetCatalog();
  const plan = (data?.plans || []).find(p => p.sku === sku);
  return { plan, ...rest } as const;
}

export function useSimPlan(sku?: string) {
  const { data, ...rest } = useSimCatalog();
  const plan = (data?.plans || []).find(p => p.sku === sku);
  return { plan, ...rest } as const;
}

export function useVpnPlan(sku?: string) {
  const { data, ...rest } = useVpnCatalog();
  const plan = (data?.plans || []).find(p => p.sku === sku);
  return { plan, ...rest } as const;
}

/**
 * Addon/installation lookup helpers by SKU
 */
export function useInternetInstallation(sku?: string) {
  const { data, ...rest } = useInternetCatalog();
  const installation = (data?.installations || []).find(i => i.sku === sku);
  return { installation, ...rest } as const;
}

export function useInternetAddon(sku?: string) {
  const { data, ...rest } = useInternetCatalog();
  const addon = (data?.addons || []).find(a => a.sku === sku);
  return { addon, ...rest } as const;
}

export function useSimAddon(sku?: string) {
  const { data, ...rest } = useSimCatalog();
  const addon = (data?.addons || []).find(a => a.sku === sku);
  return { addon, ...rest } as const;
}
