import type {
	AnalysisResult,
	Concept,
	CreateConcept,
	CreateEmotion,
	CreateReadabilityScore,
	CreateTopicCluster,
	CreateWritingStyle,
	Emotion,
	ReadabilityScore,
	TopicCluster,
	UserStats,
	WorkStats,
	WritingStyle,
} from "@shared/schema";
import { apiRequest } from "@/lib/queryClient";

export interface AnalyticsParams {
	workId?: string;
	userId?: string;
	language?: string;
	dateFrom?: string;
	dateTo?: string;
}

/**
 * API Client for analytics and AI-powered features
 * Provides type-safe methods for all analytics API endpoints
 */
export const analyticsApiClient = {
	// Work Analytics
	async getWorkStats(workId: string): Promise<WorkStats> {
		return apiRequest<WorkStats>("GET", `/api/works/${workId}/stats`);
	},

	async getWorkAnalytics(workId: string): Promise<{
		readabilityScore?: ReadabilityScore;
		writingStyle?: WritingStyle;
		emotions: Emotion[];
		concepts: Concept[];
		topicClusters: TopicCluster[];
	}> {
		return apiRequest("GET", `/api/works/${workId}/analytics`);
	},

	// Readability Analysis
	async getReadabilityScore(workId: string): Promise<ReadabilityScore | null> {
		return apiRequest<ReadabilityScore | null>(
			"GET",
			`/api/works/${workId}/readability`,
		);
	},

	async analyzeReadability(
		data: CreateReadabilityScore,
	): Promise<ReadabilityScore> {
		return apiRequest<ReadabilityScore>(
			"POST",
			"/api/analytics/readability",
			data,
		);
	},

	// Writing Style Analysis
	async getWritingStyle(workId: string): Promise<WritingStyle | null> {
		return apiRequest<WritingStyle | null>(
			"GET",
			`/api/works/${workId}/writing-style`,
		);
	},

	async analyzeWritingStyle(data: CreateWritingStyle): Promise<WritingStyle> {
		return apiRequest<WritingStyle>(
			"POST",
			"/api/analytics/writing-style",
			data,
		);
	},

	// Emotion Analysis
	async getWorkEmotions(workId: string): Promise<Emotion[]> {
		return apiRequest<Emotion[]>("GET", `/api/works/${workId}/emotions`);
	},

	async analyzeEmotions(data: CreateEmotion): Promise<Emotion> {
		return apiRequest<Emotion>("POST", "/api/analytics/emotions", data);
	},

	// Concept Extraction
	async getWorkConcepts(workId: string): Promise<Concept[]> {
		return apiRequest<Concept[]>("GET", `/api/works/${workId}/concepts`);
	},

	async extractConcepts(data: CreateConcept): Promise<Concept> {
		return apiRequest<Concept>("POST", "/api/analytics/concepts", data);
	},

	// Topic Clustering
	async getWorkTopicClusters(workId: string): Promise<TopicCluster[]> {
		return apiRequest<TopicCluster[]>(
			"GET",
			`/api/works/${workId}/topic-clusters`,
		);
	},

	async analyzeTopicClusters(data: CreateTopicCluster): Promise<TopicCluster> {
		return apiRequest<TopicCluster>(
			"POST",
			"/api/analytics/topic-clusters",
			data,
		);
	},

	// User Analytics
	async getUserStats(userId: string): Promise<UserStats> {
		return apiRequest<UserStats>("GET", `/api/users/${userId}/stats`);
	},

	async getUserActivity(
		userId: string,
		params?: AnalyticsParams,
	): Promise<{
		readingProgress: number;
		contributionsCount: number;
		likesGiven: number;
		commentsCount: number;
		collectionsCount: number;
	}> {
		const queryParams = new URLSearchParams();
		if (params?.dateFrom) queryParams.append("dateFrom", params.dateFrom);
		if (params?.dateTo) queryParams.append("dateTo", params.dateTo);

		return apiRequest(
			"GET",
			`/api/users/${userId}/activity?${queryParams.toString()}`,
		);
	},

	// General Analysis
	async getAnalysisResults(workId: string): Promise<AnalysisResult[]> {
		return apiRequest<AnalysisResult[]>("GET", `/api/works/${workId}/analysis`);
	},

	async createAnalysis(
		workId: string,
		type: string,
		data: Record<string, unknown>,
	): Promise<AnalysisResult> {
		return apiRequest<AnalysisResult>("POST", "/api/analytics/analyze", {
			workId,
			type,
			data,
		});
	},

	// Linguistic Analysis
	async performLinguisticAnalysis(workId: string): Promise<AnalysisResult> {
		return apiRequest<AnalysisResult>("POST", `/api/analytics/linguistic`, {
			workId,
		});
	},

	async performPoeticAnalysis(workId: string): Promise<AnalysisResult> {
		return apiRequest<AnalysisResult>("POST", `/api/analytics/poetic`, {
			workId,
		});
	},

	async performSentimentAnalysis(workId: string): Promise<AnalysisResult> {
		return apiRequest<AnalysisResult>("POST", `/api/analytics/sentiment`, {
			workId,
		});
	},

	// Platform Analytics
	async getPlatformStats(): Promise<{
		totalWorks: number;
		totalAuthors: number;
		totalUsers: number;
		totalTranslations: number;
		popularLanguages: { language: string; count: number }[];
		recentActivity: number;
	}> {
		return apiRequest("GET", "/api/analytics/platform");
	},
};
