import type { CreateContribution, UpdateContribution } from "@shared/schema";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type {
	ContributionSearchParams,
	ReviewContributionRequest,
} from "@/api/contribution-api-client";
import { contributionApiClient } from "@/api/contribution-api-client";

// Query hooks
export function useContribution(contributionId: string) {
	return useQuery({
		queryKey: ["contribution", contributionId],
		queryFn: () => contributionApiClient.getContribution(contributionId),
		enabled: !!contributionId,
	});
}

export function useContributions(params?: ContributionSearchParams) {
	return useQuery({
		queryKey: ["contributions", params],
		queryFn: () => contributionApiClient.getContributions(params),
	});
}

export function useUserContributions(userId: string) {
	return useQuery({
		queryKey: ["user-contributions", userId],
		queryFn: () => contributionApiClient.getUserContributions(userId),
		enabled: !!userId,
	});
}

export function usePendingContributions() {
	return useQuery({
		queryKey: ["pending-contributions"],
		queryFn: () => contributionApiClient.getPendingContributions(),
	});
}

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

	return useMutation({
		mutationFn: (data: CreateContribution) =>
			contributionApiClient.createContribution(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["contributions"] });
			if (variables.userId) {
				queryClient.invalidateQueries({
					queryKey: ["user-contributions", variables.userId],
				});
			}
		},
	});
}

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

	return useMutation({
		mutationFn: ({
			contributionId,
			data,
		}: {
			contributionId: string;
			data: UpdateContribution;
		}) => contributionApiClient.updateContribution(contributionId, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["contribution", variables.contributionId],
			});
			queryClient.invalidateQueries({ queryKey: ["contributions"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (contributionId: string) =>
			contributionApiClient.deleteContribution(contributionId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["contributions"] });
			queryClient.invalidateQueries({ queryKey: ["user-contributions"] });
			queryClient.invalidateQueries({ queryKey: ["pending-contributions"] });
		},
	});
}

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

	return useMutation({
		mutationFn: ({
			contributionId,
			data,
		}: {
			contributionId: string;
			data: ReviewContributionRequest;
		}) => contributionApiClient.reviewContribution(contributionId, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["contribution", variables.contributionId],
			});
			queryClient.invalidateQueries({ queryKey: ["contributions"] });
			queryClient.invalidateQueries({ queryKey: ["pending-contributions"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (contributionId: string) =>
			contributionApiClient.submitContribution(contributionId),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({ queryKey: ["contribution", variables] });
			queryClient.invalidateQueries({ queryKey: ["contributions"] });
			queryClient.invalidateQueries({ queryKey: ["pending-contributions"] });
		},
	});
}
