/**
 * Subscriptions Hooks
 * React hooks for subscription functionality using shared types
 */

import { useQuery } from "@tanstack/react-query";
import { apiClient, queryKeys, getDataOrThrow } from "@/lib/api";
import { useAuthSession } from "@/features/auth/services";
import type { InvoiceList } from "@customer-portal/domain/billing";
import {
  subscriptionStatsSchema,
  type Subscription,
  type SubscriptionList,
  type SubscriptionStats,
} from "@customer-portal/domain/subscriptions";

interface UseSubscriptionsOptions {
  status?: string;
}

/**
 * Hook to fetch all subscriptions
 */
export function useSubscriptions(options: UseSubscriptionsOptions = {}) {
  const { status } = options;
  const { isAuthenticated } = useAuthSession();

  return useQuery<SubscriptionList>({
    queryKey: queryKeys.subscriptions.list(status ? { status } : undefined),
    queryFn: async () => {
      const response = await apiClient.GET<SubscriptionList>(
        "/api/subscriptions",
        status ? { params: { query: { status } } } : undefined
      );
      return getDataOrThrow<SubscriptionList>(response, "Failed to load subscriptions");
    },
    staleTime: 5 * 60 * 1000,
    gcTime: 10 * 60 * 1000,
    enabled: isAuthenticated,
  });
}

/**
 * Hook to fetch active subscriptions only
 */
export function useActiveSubscriptions() {
  const { isAuthenticated } = useAuthSession();

  return useQuery<Subscription[]>({
    queryKey: queryKeys.subscriptions.active(),
    queryFn: async () => {
      const response = await apiClient.GET<Subscription[]>("/api/subscriptions/active");
      return getDataOrThrow<Subscription[]>(response, "Failed to load active subscriptions");
    },
    staleTime: 5 * 60 * 1000,
    gcTime: 10 * 60 * 1000,
    enabled: isAuthenticated,
  });
}

/**
 * Hook to fetch subscription statistics
 */
export function useSubscriptionStats() {
  const { isAuthenticated } = useAuthSession();

  return useQuery<SubscriptionStats>({
    queryKey: queryKeys.subscriptions.stats(),
    queryFn: async () => {
      const response = await apiClient.GET<SubscriptionStats>("/api/subscriptions/stats");
      if (!response.data) {
        throw new Error("Subscription statistics response was empty");
      }
      return subscriptionStatsSchema.parse(response.data);
    },
    staleTime: 5 * 60 * 1000,
    gcTime: 10 * 60 * 1000,
    enabled: isAuthenticated,
  });
}

/**
 * Hook to fetch a specific subscription
 */
export function useSubscription(subscriptionId: number) {
  const { isAuthenticated } = useAuthSession();

  return useQuery<Subscription>({
    queryKey: queryKeys.subscriptions.detail(String(subscriptionId)),
    queryFn: async () => {
      const response = await apiClient.GET<Subscription>("/api/subscriptions/{id}", {
        params: { path: { id: subscriptionId } },
      });
      return getDataOrThrow<Subscription>(response, "Failed to load subscription details");
    },
    staleTime: 5 * 60 * 1000,
    gcTime: 10 * 60 * 1000,
    enabled: isAuthenticated && subscriptionId > 0,
  });
}

/**
 * Hook to fetch subscription invoices
 */
export function useSubscriptionInvoices(
  subscriptionId: number,
  options: { page?: number; limit?: number } = {}
) {
  const { page = 1, limit = 10 } = options;
  const { isAuthenticated } = useAuthSession();

  return useQuery<InvoiceList>({
    queryKey: queryKeys.subscriptions.invoices(subscriptionId, { page, limit }),
    queryFn: async () => {
      const response = await apiClient.GET<InvoiceList>("/api/subscriptions/{id}/invoices", {
        params: {
          path: { id: subscriptionId },
          query: { page, limit },
        },
      });
      return getDataOrThrow<InvoiceList>(response, "Failed to load subscription invoices");
    },
    staleTime: 60 * 1000,
    gcTime: 5 * 60 * 1000,
    enabled: isAuthenticated && subscriptionId > 0,
  });
}
