import type {
	CreateTranslation,
	Translation,
	UpdateTranslation,
} from "@shared/schema";
import { apiRequest } from "@/lib/queryClient";
import type { TranslationWithDetails } from "@/lib/types";

export interface TranslationSearchParams {
	workId?: string;
	language?: string;
	translatorId?: string;
	limit?: number;
	offset?: number;
}

/**
 * API Client for translation-related operations
 * Provides type-safe methods for all translation API endpoints
 */
export const translationApiClient = {
	// Translation CRUD operations
	async getTranslation(translationId: string): Promise<TranslationWithDetails> {
		return apiRequest<TranslationWithDetails>(
			"GET",
			`/api/translations/${translationId}`,
		);
	},

	async getTranslations(
		params?: TranslationSearchParams,
	): Promise<Translation[]> {
		const queryParams = new URLSearchParams();
		if (params?.workId) queryParams.append("workId", params.workId);
		if (params?.language) queryParams.append("language", params.language);
		if (params?.translatorId)
			queryParams.append("translatorId", params.translatorId);
		if (params?.limit) queryParams.append("limit", params.limit.toString());
		if (params?.offset) queryParams.append("offset", params.offset.toString());

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

	async createTranslation(data: CreateTranslation): Promise<Translation> {
		return apiRequest<Translation>("POST", "/api/translations", data);
	},

	async updateTranslation(
		translationId: string,
		data: UpdateTranslation,
	): Promise<Translation> {
		return apiRequest<Translation>(
			"PUT",
			`/api/translations/${translationId}`,
			data,
		);
	},

	async deleteTranslation(translationId: string): Promise<void> {
		return apiRequest<void>("DELETE", `/api/translations/${translationId}`);
	},

	// Work Translations operations
	async getWorkTranslations(
		workId: string,
		params?: { language?: string },
	): Promise<TranslationWithDetails[]> {
		const queryParams = new URLSearchParams();
		if (params?.language) queryParams.append("language", params.language);

		return apiRequest<TranslationWithDetails[]>(
			"GET",
			`/api/works/${workId}/translations?${queryParams.toString()}`,
		);
	},

	async createWorkTranslation(
		workId: string,
		data: Omit<CreateTranslation, "workId">,
	): Promise<Translation> {
		return apiRequest<Translation>(
			"POST",
			`/api/works/${workId}/translations`,
			data,
		);
	},

	// Translator operations
	async getTranslatorTranslations(
		translatorId: string,
	): Promise<Translation[]> {
		return apiRequest<Translation[]>(
			"GET",
			`/api/users/${translatorId}/translations`,
		);
	},
};
