import type { Author, Tag, Translation, Work } from "@shared/schema";
import { apiRequest } from "@/lib/queryClient";

export interface SearchFilters {
	languages?: string[];
	categories?: string[];
	tags?: string[];
	authors?: string[];
	workTypes?: string[];
	dateFrom?: string;
	dateTo?: string;
	yearStart?: number;
	yearEnd?: number;
}

export interface SearchParams {
	query: string;
	limit?: number;
	offset?: number;
	filters?: SearchFilters;
}

export interface SearchResults {
	works: Work[];
	translations: Translation[];
	authors: Author[];
	total: number;
}

export interface AdvancedSearchResults {
	works: Work[];
	authors: Author[];
	translations: Translation[];
	tags: Tag[];
	totalResults: number;
	facets: {
		languages: { [key: string]: number };
		types: { [key: string]: number };
		authors: { [key: string]: number };
		tags: { [key: string]: number };
	};
}

/**
 * API Client for search-related operations
 * Provides type-safe methods for all search API endpoints
 */
export const searchApiClient = {
	// Basic search
	async search(params: SearchParams): Promise<SearchResults> {
		const { query, limit, offset, filters } = params;
		const requestBody = {
			query,
			limit,
			offset,
			filters,
		};

		return apiRequest<SearchResults>("POST", "/api/search", requestBody);
	},

	// Advanced search with facets
	async advancedSearch(params: SearchParams): Promise<AdvancedSearchResults> {
		const { query, limit, offset, filters } = params;
		const requestBody = {
			query,
			limit,
			offset,
			filters,
		};

		return apiRequest<AdvancedSearchResults>(
			"POST",
			"/api/search/advanced",
			requestBody,
		);
	},

	// Quick search for autocomplete
	async quickSearch(
		query: string,
		limit = 5,
	): Promise<{
		works: Work[];
		authors: Author[];
	}> {
		return apiRequest<{
			works: Work[];
			authors: Author[];
		}>(
			"GET",
			`/api/search/quick?q=${encodeURIComponent(query)}&limit=${limit}`,
		);
	},

	// Search suggestions
	async getSearchSuggestions(query: string): Promise<string[]> {
		return apiRequest<string[]>(
			"GET",
			`/api/search/suggestions?q=${encodeURIComponent(query)}`,
		);
	},

	// Search by entity type
	async searchWorks(query: string, filters?: SearchFilters): Promise<Work[]> {
		return apiRequest<Work[]>("POST", "/api/search/works", { query, filters });
	},

	async searchAuthors(
		query: string,
		filters?: SearchFilters,
	): Promise<Author[]> {
		return apiRequest<Author[]>("POST", "/api/search/authors", {
			query,
			filters,
		});
	},

	async searchTranslations(
		query: string,
		filters?: SearchFilters,
	): Promise<Translation[]> {
		return apiRequest<Translation[]>("POST", "/api/search/translations", {
			query,
			filters,
		});
	},

	// Browse operations
	async browseByLanguage(language: string, limit?: number): Promise<Work[]> {
		const queryParams = new URLSearchParams();
		queryParams.append("language", language);
		if (limit) queryParams.append("limit", limit.toString());

		return apiRequest<Work[]>(
			"GET",
			`/api/browse/language?${queryParams.toString()}`,
		);
	},

	async browseByTag(tag: string, limit?: number): Promise<Work[]> {
		const queryParams = new URLSearchParams();
		queryParams.append("tag", tag);
		if (limit) queryParams.append("limit", limit.toString());

		return apiRequest<Work[]>(
			"GET",
			`/api/browse/tag?${queryParams.toString()}`,
		);
	},

	async browseByAuthor(authorId: string, limit?: number): Promise<Work[]> {
		const queryParams = new URLSearchParams();
		if (limit) queryParams.append("limit", limit.toString());

		return apiRequest<Work[]>(
			"GET",
			`/api/authors/${authorId}/works?${queryParams.toString()}`,
		);
	},
};
