import type { CreateTranslation, UpdateTranslation } from "@shared/schema";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { TranslationSearchParams } from "@/api/translation-api-client";
import { translationApiClient } from "@/api/translation-api-client";

// Query hooks
export function useTranslation(translationId: string) {
	return useQuery({
		queryKey: ["translation", translationId],
		queryFn: () => translationApiClient.getTranslation(translationId),
		enabled: !!translationId,
	});
}

export function useTranslations(params?: TranslationSearchParams) {
	return useQuery({
		queryKey: ["translations", params],
		queryFn: () => translationApiClient.getTranslations(params),
	});
}

export function useWorkTranslations(
	workId: string,
	params?: { language?: string },
) {
	return useQuery({
		queryKey: ["work-translations", workId, params],
		queryFn: () => translationApiClient.getWorkTranslations(workId, params),
		enabled: !!workId,
	});
}

export function useTranslatorTranslations(translatorId: string) {
	return useQuery({
		queryKey: ["translator-translations", translatorId],
		queryFn: () => translationApiClient.getTranslatorTranslations(translatorId),
		enabled: !!translatorId,
	});
}

// Mutation hooks
export function useCreateTranslation() {
	const queryClient = useQueryClient();

	return useMutation({
		mutationFn: (data: CreateTranslation) =>
			translationApiClient.createTranslation(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["translations"] });
			if (variables.workId) {
				queryClient.invalidateQueries({
					queryKey: ["work-translations", variables.workId],
				});
			}
		},
	});
}

export function useCreateWorkTranslation() {
	const queryClient = useQueryClient();

	return useMutation({
		mutationFn: ({
			workId,
			data,
		}: {
			workId: string;
			data: Omit<CreateTranslation, "workId">;
		}) => translationApiClient.createWorkTranslation(workId, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["work-translations", variables.workId],
			});
			queryClient.invalidateQueries({ queryKey: ["translations"] });
		},
	});
}

export function useUpdateTranslation() {
	const queryClient = useQueryClient();

	return useMutation({
		mutationFn: ({
			translationId,
			data,
		}: {
			translationId: string;
			data: UpdateTranslation;
		}) => translationApiClient.updateTranslation(translationId, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["translation", variables.translationId],
			});
			queryClient.invalidateQueries({ queryKey: ["translations"] });
			queryClient.invalidateQueries({ queryKey: ["work-translations"] });
		},
	});
}

export function useDeleteTranslation() {
	const queryClient = useQueryClient();

	return useMutation({
		mutationFn: (translationId: string) =>
			translationApiClient.deleteTranslation(translationId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["translations"] });
			queryClient.invalidateQueries({ queryKey: ["work-translations"] });
		},
	});
}
