import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type {
	CreateWorkRequest,
	LikeWorkRequest,
	UpdateReadingProgressRequest,
	UpdateWorkRequest,
} from "@/api/work-api-client";
import { workApiClient } from "@/api/work-api-client";

// TODO: Move to environment configuration or user context
const DEFAULT_USER_ID = "1";

// Query hooks
export function useWork(workSlug: string) {
	return useQuery({
		queryKey: ["work", workSlug],
		queryFn: () => workApiClient.getWork(workSlug),
		enabled: !!workSlug,
	});
}

export function useWorks() {
	return useQuery({
		queryKey: ["works"],
		queryFn: () => workApiClient.getWorks(),
	});
}

export function useWorkTranslations(workSlug: string) {
	return useQuery({
		queryKey: ["work-translations", workSlug],
		queryFn: () => workApiClient.getWorkTranslations(workSlug),
		enabled: !!workSlug,
	});
}

export function useReadingProgress(
	workId: string,
	userId: string = DEFAULT_USER_ID,
) {
	return useQuery({
		queryKey: ["reading-progress", userId, workId],
		queryFn: () => workApiClient.getReadingProgress(userId, workId),
		enabled: !!workId && !!userId,
	});
}

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

export function useWorksByAuthor(authorId: number) {
	return useQuery({
		queryKey: ["works-by-author", authorId],
		queryFn: () => workApiClient.getWorksByAuthor(authorId),
		enabled: !!authorId,
	});
}

export function useWorksByTag(tag: string) {
	return useQuery({
		queryKey: ["works-by-tag", tag],
		queryFn: () => workApiClient.getWorksByTag(tag),
		enabled: !!tag,
	});
}

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

	return useMutation({
		mutationFn: (data: CreateWorkRequest) => workApiClient.createWork(data),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["works"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (data: UpdateWorkRequest) => workApiClient.updateWork(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["work", variables.id] });
			queryClient.invalidateQueries({ queryKey: ["works"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (workId: number) => workApiClient.deleteWork(workId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["works"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (data: UpdateReadingProgressRequest) =>
			workApiClient.updateReadingProgress(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["reading-progress", variables.userId, variables.workId],
			});
		},
	});
}

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

	return useMutation({
		mutationFn: (data: LikeWorkRequest) => workApiClient.likeWork(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["work", variables.workId] });
			queryClient.invalidateQueries({ queryKey: ["works"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (data: LikeWorkRequest) => workApiClient.unlikeWork(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["work", variables.workId] });
			queryClient.invalidateQueries({ queryKey: ["works"] });
		},
	});
}
