import type {
	CreateConcept,
	CreateEmotion,
	CreateReadabilityScore,
	CreateTopicCluster,
	CreateWritingStyle,
} from "@shared/schema";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { AnalyticsParams } from "@/api/analytics-api-client";
import { analyticsApiClient } from "@/api/analytics-api-client";

// Query hooks
export function useWorkStats(workId: string) {
	return useQuery({
		queryKey: ["work-stats", workId],
		queryFn: () => analyticsApiClient.getWorkStats(workId),
		enabled: !!workId,
	});
}

export function useWorkAnalytics(workId: string) {
	return useQuery({
		queryKey: ["work-analytics", workId],
		queryFn: () => analyticsApiClient.getWorkAnalytics(workId),
		enabled: !!workId,
	});
}

export function useReadabilityScore(workId: string) {
	return useQuery({
		queryKey: ["readability-score", workId],
		queryFn: () => analyticsApiClient.getReadabilityScore(workId),
		enabled: !!workId,
	});
}

export function useWritingStyle(workId: string) {
	return useQuery({
		queryKey: ["writing-style", workId],
		queryFn: () => analyticsApiClient.getWritingStyle(workId),
		enabled: !!workId,
	});
}

export function useWorkEmotions(workId: string) {
	return useQuery({
		queryKey: ["work-emotions", workId],
		queryFn: () => analyticsApiClient.getWorkEmotions(workId),
		enabled: !!workId,
	});
}

export function useWorkConcepts(workId: string) {
	return useQuery({
		queryKey: ["work-concepts", workId],
		queryFn: () => analyticsApiClient.getWorkConcepts(workId),
		enabled: !!workId,
	});
}

export function useWorkTopicClusters(workId: string) {
	return useQuery({
		queryKey: ["work-topic-clusters", workId],
		queryFn: () => analyticsApiClient.getWorkTopicClusters(workId),
		enabled: !!workId,
	});
}

export function useUserStats(userId: string) {
	return useQuery({
		queryKey: ["user-stats", userId],
		queryFn: () => analyticsApiClient.getUserStats(userId),
		enabled: !!userId,
	});
}

export function useUserActivity(userId: string, params?: AnalyticsParams) {
	return useQuery({
		queryKey: ["user-activity", userId, params],
		queryFn: () => analyticsApiClient.getUserActivity(userId, params),
		enabled: !!userId,
	});
}

export function useAnalysisResults(workId: string) {
	return useQuery({
		queryKey: ["analysis-results", workId],
		queryFn: () => analyticsApiClient.getAnalysisResults(workId),
		enabled: !!workId,
	});
}

export function usePlatformStats() {
	return useQuery({
		queryKey: ["platform-stats"],
		queryFn: () => analyticsApiClient.getPlatformStats(),
	});
}

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

	return useMutation({
		mutationFn: (data: CreateReadabilityScore) =>
			analyticsApiClient.analyzeReadability(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["readability-score", variables.workId],
			});
			queryClient.invalidateQueries({
				queryKey: ["work-analytics", variables.workId],
			});
		},
	});
}

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

	return useMutation({
		mutationFn: (data: CreateWritingStyle) =>
			analyticsApiClient.analyzeWritingStyle(data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["writing-style", variables.workId],
			});
			queryClient.invalidateQueries({
				queryKey: ["work-analytics", variables.workId],
			});
		},
	});
}

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

	return useMutation({
		mutationFn: (data: CreateEmotion) =>
			analyticsApiClient.analyzeEmotions(data),
		onSuccess: (_, variables) => {
			if (variables.workId) {
				queryClient.invalidateQueries({
					queryKey: ["work-emotions", variables.workId],
				});
				queryClient.invalidateQueries({
					queryKey: ["work-analytics", variables.workId],
				});
			}
		},
	});
}

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

	return useMutation({
		mutationFn: (data: CreateConcept) =>
			analyticsApiClient.extractConcepts(data),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["work-concepts"] });
			queryClient.invalidateQueries({ queryKey: ["work-analytics"] });
		},
	});
}

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

	return useMutation({
		mutationFn: (data: CreateTopicCluster) =>
			analyticsApiClient.analyzeTopicClusters(data),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["work-topic-clusters"] });
			queryClient.invalidateQueries({ queryKey: ["work-analytics"] });
		},
	});
}

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

	return useMutation({
		mutationFn: ({
			workId,
			type,
			data,
		}: {
			workId: string;
			type: string;
			data: Record<string, unknown>;
		}) => analyticsApiClient.createAnalysis(workId, type, data),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["analysis-results", variables.workId],
			});
		},
	});
}

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

	return useMutation({
		mutationFn: (workId: string) =>
			analyticsApiClient.performLinguisticAnalysis(workId),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["analysis-results", variables],
			});
		},
	});
}

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

	return useMutation({
		mutationFn: (workId: string) =>
			analyticsApiClient.performPoeticAnalysis(workId),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["analysis-results", variables],
			});
		},
	});
}

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

	return useMutation({
		mutationFn: (workId: string) =>
			analyticsApiClient.performSentimentAnalysis(workId),
		onSuccess: (_, variables) => {
			queryClient.invalidateQueries({
				queryKey: ["analysis-results", variables],
			});
		},
	});
}
