/**
 * Services Hooks
 * React hooks for services functionality
 */

import { useQuery } from "@tanstack/react-query";
import { queryKeys } from "@/core/api";
import { servicesService } from "../api";
import { useAuthSession } from "@/features/auth/stores/auth.store";

type ServicesCatalogScope = "public" | "account";

function withScope<T extends readonly unknown[]>(key: T, scope: ServicesCatalogScope) {
  return [...key, scope] as const;
}

/**
 * Internet catalog (public vs account)
 */
export function usePublicInternetCatalog() {
  return useQuery({
    queryKey: withScope(queryKeys.services.internet.combined(), "public"),
    queryFn: () => servicesService.getPublicInternetCatalog(),
  });
}

export function useAccountInternetCatalog() {
  const { isAuthenticated } = useAuthSession();
  return useQuery({
    queryKey: withScope(queryKeys.services.internet.combined(), "account"),
    enabled: isAuthenticated,
    queryFn: () => servicesService.getAccountInternetCatalog(),
  });
}

/**
 * SIM catalog (public vs account)
 */
export function usePublicSimCatalog() {
  return useQuery({
    queryKey: withScope(queryKeys.services.sim.combined(), "public"),
    queryFn: () => servicesService.getPublicSimCatalog(),
  });
}

export function useAccountSimCatalog() {
  const { isAuthenticated } = useAuthSession();
  return useQuery({
    queryKey: withScope(queryKeys.services.sim.combined(), "account"),
    enabled: isAuthenticated,
    queryFn: () => servicesService.getAccountSimCatalog(),
  });
}

/**
 * VPN catalog (public vs account)
 */
export function usePublicVpnCatalog() {
  return useQuery({
    queryKey: withScope(queryKeys.services.vpn.combined(), "public"),
    queryFn: () => servicesService.getPublicVpnCatalog(),
  });
}

export function useAccountVpnCatalog() {
  const { isAuthenticated } = useAuthSession();
  return useQuery({
    queryKey: withScope(queryKeys.services.vpn.combined(), "account"),
    enabled: isAuthenticated,
    queryFn: () => servicesService.getAccountVpnCatalog(),
  });
}

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

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

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

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

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

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

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

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

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