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

export interface DiscoverySetup {
	id: number;
	name: string;
	createdAt: string;
	itemCount: number;
	creatorName: string | null;
}

export interface DiscoveryCategory {
	name: string;
	itemCount: number;
}

interface GlobalItem {
	id: number;
	brand: string;
	model: string;
	category: string | null;
	weightGrams: number | null;
	priceCents: number | null;
	imageUrl: string | null;
	description: string | null;
	sourceUrl: string | null;
	imageCredit: string | null;
	imageSourceUrl: string | null;
	createdAt: string;
}

interface CursorPage<T> {
	items: T[];
	nextCursor: string | null;
	hasMore: boolean;
}

export function useDiscoverySetups(limit = 6) {
	return useQuery({
		queryKey: ["discovery", "setups", limit],
		queryFn: () =>
			apiGet<CursorPage<DiscoverySetup>>(
				`/api/discovery/setups?limit=${limit}`,
			),
		staleTime: 2 * 60 * 1000,
	});
}

export function useDiscoveryItems(limit = 8) {
	return useQuery({
		queryKey: ["discovery", "items", limit],
		queryFn: () =>
			apiGet<CursorPage<GlobalItem>>(`/api/discovery/items?limit=${limit}`),
		staleTime: 2 * 60 * 1000,
	});
}

export function useDiscoveryCategories(limit = 12) {
	return useQuery({
		queryKey: ["discovery", "categories", limit],
		queryFn: () =>
			apiGet<DiscoveryCategory[]>(`/api/discovery/categories?limit=${limit}`),
		staleTime: 5 * 60 * 1000,
	});
}
