"use client";

import { useQuery } from "@tanstack/react-query";
import { useAuthSession } from "@/features/auth/services";
import { queryKeys } from "@/lib/api";
import { ordersService } from "@/features/orders/services/orders.service";
import type { OrderSummary } from "@customer-portal/domain/orders";

const STALE_TIME_MS = 2 * 60 * 1000;
const GC_TIME_MS = 10 * 60 * 1000;

export function useOrdersList() {
  const { isAuthenticated } = useAuthSession();

  return useQuery<OrderSummary[]>({
    queryKey: queryKeys.orders.list(),
    queryFn: () => ordersService.getMyOrders(),
    enabled: isAuthenticated,
    staleTime: STALE_TIME_MS,
    gcTime: GC_TIME_MS,
    refetchOnWindowFocus: false,
    refetchOnReconnect: false,
  });
}

export type UseOrdersListResult = ReturnType<typeof useOrdersList>;
