import type { CreateCollection, UpdateCollection } from "@shared/schema";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { CollectionSearchParams } from "@/api/collection-api-client";
import { collectionApiClient } from "@/api/collection-api-client";

// Query hooks
export function useCollection(collectionId: string) {
	return useQuery({
		queryKey: ["collection", collectionId],
		queryFn: () => collectionApiClient.getCollection(collectionId),
		enabled: !!collectionId,
	});
}

export function useCollectionBySlug(slug: string) {
	return useQuery({
		queryKey: ["collection-by-slug", slug],
		queryFn: () => collectionApiClient.getCollectionBySlug(slug),
		enabled: !!slug,
	});
}

export function useCollections(params?: CollectionSearchParams) {
	return useQuery({
		queryKey: ["collections", params],
		queryFn: () => collectionApiClient.getCollections(params),
	});
}

export function useUserCollections(userId: string) {
	return useQuery({
		queryKey: ["user-collections", userId],
		queryFn: () => collectionApiClient.getUserCollections(userId),
		enabled: !!userId,
	});
}

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

	return useMutation({
		mutationFn: (data: CreateCollection) =>
			collectionApiClient.createCollection(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["collections"] });
			if (variables.userId) {
				queryClient.invalidateQueries({
					queryKey: ["user-collections", variables.userId],
				});
			}
		},
	});
}

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

	return useMutation({
		mutationFn: ({
			collectionId,
			data,
		}: {
			collectionId: string;
			data: UpdateCollection;
		}) => collectionApiClient.updateCollection(collectionId, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["collection", variables.collectionId],
			});
			queryClient.invalidateQueries({ queryKey: ["collections"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (collectionId: string) =>
			collectionApiClient.deleteCollection(collectionId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["collections"] });
			queryClient.invalidateQueries({ queryKey: ["user-collections"] });
		},
	});
}

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

	return useMutation({
		mutationFn: ({
			collectionId,
			workId,
			order,
		}: {
			collectionId: string;
			workId: string;
			order?: number;
		}) => collectionApiClient.addWorkToCollection(collectionId, workId, order),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["collection", variables.collectionId],
			});
		},
	});
}

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

	return useMutation({
		mutationFn: ({
			collectionId,
			workId,
		}: {
			collectionId: string;
			workId: string;
		}) => collectionApiClient.removeWorkFromCollection(collectionId, workId),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["collection", variables.collectionId],
			});
		},
	});
}

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

	return useMutation({
		mutationFn: ({
			collectionId,
			items,
		}: {
			collectionId: string;
			items: { id: string; order: number }[];
		}) => collectionApiClient.reorderCollectionItems(collectionId, items),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["collection", variables.collectionId],
			});
		},
	});
}
