import type { CreateComment, UpdateComment } from "@shared/schema";
import { apiRequest } from "@/lib/queryClient";
import type { CommentWithUser } from "@/lib/types";

export interface CommentSearchParams {
	workId?: string;
	translationId?: string;
	userId?: string;
	entityType?: string;
	entityId?: string;
	limit?: number;
	offset?: number;
}

/**
 * API Client for comment-related operations
 * Provides type-safe methods for all comment API endpoints
 */
export const commentApiClient = {
	// Comment CRUD operations
	async getComment(commentId: string): Promise<CommentWithUser> {
		return apiRequest<CommentWithUser>("GET", `/api/comments/${commentId}`);
	},

	async getComments(params?: CommentSearchParams): Promise<CommentWithUser[]> {
		const queryParams = new URLSearchParams();
		if (params?.workId) queryParams.append("workId", params.workId);
		if (params?.translationId)
			queryParams.append("translationId", params.translationId);
		if (params?.userId) queryParams.append("userId", params.userId);
		if (params?.entityType) queryParams.append("entityType", params.entityType);
		if (params?.entityId) queryParams.append("entityId", params.entityId);
		if (params?.limit) queryParams.append("limit", params.limit.toString());
		if (params?.offset) queryParams.append("offset", params.offset.toString());

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

	async createComment(data: CreateComment): Promise<CommentWithUser> {
		return apiRequest<CommentWithUser>("POST", "/api/comments", data);
	},

	async updateComment(
		commentId: string,
		data: UpdateComment,
	): Promise<CommentWithUser> {
		return apiRequest<CommentWithUser>(
			"PUT",
			`/api/comments/${commentId}`,
			data,
		);
	},

	async deleteComment(commentId: string): Promise<void> {
		return apiRequest<void>("DELETE", `/api/comments/${commentId}`);
	},

	// Work Comments operations
	async getWorkComments(workId: string): Promise<CommentWithUser[]> {
		return apiRequest<CommentWithUser[]>(
			"GET",
			`/api/works/${workId}/comments`,
		);
	},

	async createWorkComment(
		workId: string,
		data: Omit<CreateComment, "workId">,
	): Promise<CommentWithUser> {
		return apiRequest<CommentWithUser>(
			"POST",
			`/api/works/${workId}/comments`,
			data,
		);
	},

	// Translation Comments operations
	async getTranslationComments(
		translationId: string,
	): Promise<CommentWithUser[]> {
		return apiRequest<CommentWithUser[]>(
			"GET",
			`/api/translations/${translationId}/comments`,
		);
	},

	async createTranslationComment(
		translationId: string,
		data: Omit<CreateComment, "translationId">,
	): Promise<CommentWithUser> {
		return apiRequest<CommentWithUser>(
			"POST",
			`/api/translations/${translationId}/comments`,
			data,
		);
	},

	// Comment Replies operations
	async getCommentReplies(commentId: string): Promise<CommentWithUser[]> {
		return apiRequest<CommentWithUser[]>(
			"GET",
			`/api/comments/${commentId}/replies`,
		);
	},

	async createCommentReply(
		parentCommentId: string,
		data: Omit<CreateComment, "parentId">,
	): Promise<CommentWithUser> {
		return apiRequest<CommentWithUser>(
			"POST",
			`/api/comments/${parentCommentId}/replies`,
			data,
		);
	},
};
