import type { CreateBookmark, UpdateBookmark } from "@shared/schema";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { BookmarkSearchParams } from "@/api/bookmark-api-client";
import { bookmarkApiClient } from "@/api/bookmark-api-client";

// Query hooks
export function useBookmark(bookmarkId: string) {
	return useQuery({
		queryKey: ["bookmark", bookmarkId],
		queryFn: () => bookmarkApiClient.getBookmark(bookmarkId),
		enabled: !!bookmarkId,
	});
}

export function useBookmarks(params?: BookmarkSearchParams) {
	return useQuery({
		queryKey: ["bookmarks", params],
		queryFn: () => bookmarkApiClient.getBookmarks(params),
	});
}

export function useUserBookmarks(userId: string) {
	return useQuery({
		queryKey: ["user-bookmarks", userId],
		queryFn: () => bookmarkApiClient.getUserBookmarks(userId),
		enabled: !!userId,
	});
}

export function useIsWorkBookmarked(userId: string, workId: string) {
	return useQuery({
		queryKey: ["is-work-bookmarked", userId, workId],
		queryFn: () => bookmarkApiClient.isWorkBookmarked(userId, workId),
		enabled: !!userId && !!workId,
	});
}

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

	return useMutation({
		mutationFn: (data: CreateBookmark) =>
			bookmarkApiClient.createBookmark(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["bookmarks"] });
			if (variables.userId) {
				queryClient.invalidateQueries({
					queryKey: ["user-bookmarks", variables.userId],
				});
				queryClient.invalidateQueries({
					queryKey: ["is-work-bookmarked", variables.userId, variables.workId],
				});
			}
		},
	});
}

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

	return useMutation({
		mutationFn: ({
			userId,
			workId,
			note,
		}: {
			userId: string;
			workId: string;
			note?: string;
		}) => bookmarkApiClient.bookmarkWork(userId, workId, note),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["user-bookmarks", variables.userId],
			});
			queryClient.invalidateQueries({
				queryKey: ["is-work-bookmarked", variables.userId, variables.workId],
			});
			queryClient.invalidateQueries({ queryKey: ["bookmarks"] });
		},
	});
}

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

	return useMutation({
		mutationFn: ({ userId, workId }: { userId: string; workId: string }) =>
			bookmarkApiClient.unbookmarkWork(userId, workId),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["user-bookmarks", variables.userId],
			});
			queryClient.invalidateQueries({
				queryKey: ["is-work-bookmarked", variables.userId, variables.workId],
			});
			queryClient.invalidateQueries({ queryKey: ["bookmarks"] });
		},
	});
}

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

	return useMutation({
		mutationFn: ({
			bookmarkId,
			data,
		}: {
			bookmarkId: string;
			data: UpdateBookmark;
		}) => bookmarkApiClient.updateBookmark(bookmarkId, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["bookmark", variables.bookmarkId],
			});
			queryClient.invalidateQueries({ queryKey: ["bookmarks"] });
			queryClient.invalidateQueries({ queryKey: ["user-bookmarks"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (bookmarkId: string) =>
			bookmarkApiClient.deleteBookmark(bookmarkId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["bookmarks"] });
			queryClient.invalidateQueries({ queryKey: ["user-bookmarks"] });
			queryClient.invalidateQueries({ queryKey: ["is-work-bookmarked"] });
		},
	});
}
