import type { CreateTag, UpdateTag } from "@shared/schema";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { TagSearchParams } from "@/api/tag-api-client";
import { tagApiClient } from "@/api/tag-api-client";

// Query hooks
export function useTag(tagId: string) {
	return useQuery({
		queryKey: ["tag", tagId],
		queryFn: () => tagApiClient.getTag(tagId),
		enabled: !!tagId,
	});
}

export function useTagByName(name: string) {
	return useQuery({
		queryKey: ["tag-by-name", name],
		queryFn: () => tagApiClient.getTagByName(name),
		enabled: !!name,
	});
}

export function useTags(params?: TagSearchParams) {
	return useQuery({
		queryKey: ["tags", params],
		queryFn: () => tagApiClient.getTags(params),
	});
}

export function useTagWorks(tagId: string) {
	return useQuery({
		queryKey: ["tag-works", tagId],
		queryFn: () => tagApiClient.getTagWorks(tagId),
		enabled: !!tagId,
	});
}

export function useWorksByTagName(tagName: string) {
	return useQuery({
		queryKey: ["works-by-tag-name", tagName],
		queryFn: () => tagApiClient.getWorksByTagName(tagName),
		enabled: !!tagName,
	});
}

export function usePopularTags(limit = 20) {
	return useQuery({
		queryKey: ["popular-tags", limit],
		queryFn: () => tagApiClient.getPopularTags(limit),
	});
}

export function useTagsByType(type: string) {
	return useQuery({
		queryKey: ["tags-by-type", type],
		queryFn: () => tagApiClient.getTagsByType(type),
		enabled: !!type,
	});
}

export function useSearchTags(query: string) {
	return useQuery({
		queryKey: ["search-tags", query],
		queryFn: () => tagApiClient.searchTags(query),
		enabled: !!query && query.length > 1,
	});
}

export function useWorkTags(workId: string) {
	return useQuery({
		queryKey: ["work-tags", workId],
		queryFn: () => tagApiClient.getWorkTags(workId),
		enabled: !!workId,
	});
}

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

	return useMutation({
		mutationFn: (data: CreateTag) => tagApiClient.createTag(data),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["tags"] });
			queryClient.invalidateQueries({ queryKey: ["popular-tags"] });
		},
	});
}

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

	return useMutation({
		mutationFn: ({ tagId, data }: { tagId: string; data: UpdateTag }) =>
			tagApiClient.updateTag(tagId, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["tag", variables.tagId] });
			queryClient.invalidateQueries({ queryKey: ["tags"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (tagId: string) => tagApiClient.deleteTag(tagId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["tags"] });
			queryClient.invalidateQueries({ queryKey: ["popular-tags"] });
		},
	});
}

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

	return useMutation({
		mutationFn: ({ workId, tagId }: { workId: string; tagId: string }) =>
			tagApiClient.addTagToWork(workId, tagId),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["work-tags", variables.workId],
			});
			queryClient.invalidateQueries({
				queryKey: ["tag-works", variables.tagId],
			});
		},
	});
}

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

	return useMutation({
		mutationFn: ({ workId, tagId }: { workId: string; tagId: string }) =>
			tagApiClient.removeTagFromWork(workId, tagId),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["work-tags", variables.workId],
			});
			queryClient.invalidateQueries({
				queryKey: ["tag-works", variables.tagId],
			});
		},
	});
}
