import type {
	Contribution,
	CreateContribution,
	UpdateContribution,
} from "@shared/schema";
import { apiRequest } from "@/lib/queryClient";

export interface ContributionSearchParams {
	userId?: string;
	workId?: string;
	translationId?: string;
	status?: string;
	limit?: number;
	offset?: number;
}

export interface ReviewContributionRequest {
	status: "approved" | "rejected";
	feedback?: string;
}

/**
 * API Client for contribution-related operations
 * Provides type-safe methods for all contribution API endpoints
 */
export const contributionApiClient = {
	// Contribution CRUD operations
	async getContribution(contributionId: string): Promise<Contribution> {
		return apiRequest<Contribution>(
			"GET",
			`/api/contributions/${contributionId}`,
		);
	},

	async getContributions(
		params?: ContributionSearchParams,
	): Promise<Contribution[]> {
		const queryParams = new URLSearchParams();
		if (params?.userId) queryParams.append("userId", params.userId);
		if (params?.workId) queryParams.append("workId", params.workId);
		if (params?.translationId)
			queryParams.append("translationId", params.translationId);
		if (params?.status) queryParams.append("status", params.status);
		if (params?.limit) queryParams.append("limit", params.limit.toString());
		if (params?.offset) queryParams.append("offset", params.offset.toString());

		return apiRequest<Contribution[]>(
			"GET",
			`/api/contributions?${queryParams.toString()}`,
		);
	},

	async createContribution(data: CreateContribution): Promise<Contribution> {
		return apiRequest<Contribution>("POST", "/api/contributions", data);
	},

	async updateContribution(
		contributionId: string,
		data: UpdateContribution,
	): Promise<Contribution> {
		return apiRequest<Contribution>(
			"PUT",
			`/api/contributions/${contributionId}`,
			data,
		);
	},

	async deleteContribution(contributionId: string): Promise<void> {
		return apiRequest<void>("DELETE", `/api/contributions/${contributionId}`);
	},

	// Contribution Review operations
	async reviewContribution(
		contributionId: string,
		data: ReviewContributionRequest,
	): Promise<Contribution> {
		return apiRequest<Contribution>(
			"POST",
			`/api/contributions/${contributionId}/review`,
			data,
		);
	},

	async submitContribution(contributionId: string): Promise<Contribution> {
		return apiRequest<Contribution>(
			"POST",
			`/api/contributions/${contributionId}/submit`,
		);
	},

	// User Contributions operations
	async getUserContributions(userId: string): Promise<Contribution[]> {
		return apiRequest<Contribution[]>(
			"GET",
			`/api/users/${userId}/contributions`,
		);
	},

	async getUserContributionsByStatus(
		userId: string,
		status: string,
	): Promise<Contribution[]> {
		return apiRequest<Contribution[]>(
			"GET",
			`/api/users/${userId}/contributions?status=${status}`,
		);
	},

	// Work Contributions operations
	async getWorkContributions(workId: string): Promise<Contribution[]> {
		return apiRequest<Contribution[]>(
			"GET",
			`/api/works/${workId}/contributions`,
		);
	},

	// Review Queue operations
	async getPendingContributions(limit?: number): Promise<Contribution[]> {
		const queryParams = new URLSearchParams();
		queryParams.append("status", "submitted");
		if (limit) queryParams.append("limit", limit.toString());

		return apiRequest<Contribution[]>(
			"GET",
			`/api/contributions?${queryParams.toString()}`,
		);
	},

	async getContributionsUnderReview(limit?: number): Promise<Contribution[]> {
		const queryParams = new URLSearchParams();
		queryParams.append("status", "under_review");
		if (limit) queryParams.append("limit", limit.toString());

		return apiRequest<Contribution[]>(
			"GET",
			`/api/contributions?${queryParams.toString()}`,
		);
	},
};
