import { useQuery } from "@tanstack/react-query";
import type { SearchFilters, SearchParams } from "@/api/search-api-client";
import { searchApiClient } from "@/api/search-api-client";

// Query hooks
export function useSearch(params: SearchParams) {
	return useQuery({
		queryKey: ["search", params],
		queryFn: () => searchApiClient.search(params),
		enabled: !!params.query && params.query.length > 2,
	});
}

export function useAdvancedSearch(params: SearchParams) {
	return useQuery({
		queryKey: ["advanced-search", params],
		queryFn: () => searchApiClient.advancedSearch(params),
		enabled: !!params.query && params.query.length > 2,
	});
}

export function useQuickSearch(query: string, limit = 5) {
	return useQuery({
		queryKey: ["quick-search", query, limit],
		queryFn: () => searchApiClient.quickSearch(query, limit),
		enabled: !!query && query.length > 1,
	});
}

export function useSearchSuggestions(query: string) {
	return useQuery({
		queryKey: ["search-suggestions", query],
		queryFn: () => searchApiClient.getSearchSuggestions(query),
		enabled: !!query && query.length > 1,
	});
}

export function useSearchWorks(query: string, filters?: SearchFilters) {
	return useQuery({
		queryKey: ["search-works", query, filters],
		queryFn: () => searchApiClient.searchWorks(query, filters),
		enabled: !!query && query.length > 2,
	});
}

export function useSearchAuthors(query: string, filters?: SearchFilters) {
	return useQuery({
		queryKey: ["search-authors", query, filters],
		queryFn: () => searchApiClient.searchAuthors(query, filters),
		enabled: !!query && query.length > 2,
	});
}

export function useSearchTranslations(query: string, filters?: SearchFilters) {
	return useQuery({
		queryKey: ["search-translations", query, filters],
		queryFn: () => searchApiClient.searchTranslations(query, filters),
		enabled: !!query && query.length > 2,
	});
}

export function useBrowseByLanguage(language: string, limit?: number) {
	return useQuery({
		queryKey: ["browse-language", language, limit],
		queryFn: () => searchApiClient.browseByLanguage(language, limit),
		enabled: !!language,
	});
}

export function useBrowseByTag(tag: string, limit?: number) {
	return useQuery({
		queryKey: ["browse-tag", tag, limit],
		queryFn: () => searchApiClient.browseByTag(tag, limit),
		enabled: !!tag,
	});
}

export function useBrowseByAuthor(authorId: string, limit?: number) {
	return useQuery({
		queryKey: ["browse-author", authorId, limit],
		queryFn: () => searchApiClient.browseByAuthor(authorId, limit),
		enabled: !!authorId,
	});
}
