import type { Like, ReadingProgress } from "@shared/schema";
import { apiRequest } from "@/lib/queryClient";
import type {
	TranslationWithDetails,
	WorkWithAuthor,
	WorkWithDetails,
} from "@/lib/types";

export interface CreateWorkRequest {
	title: string;
	authorId: string;
	originalLanguage: string;
	content?: string;
	description?: string;
	tags?: string[];
}

export interface UpdateWorkRequest extends Partial<CreateWorkRequest> {
	id: string;
}

export interface UpdateReadingProgressRequest {
	userId: string;
	workId: string;
	translationId?: string;
	progress: number;
}

export interface LikeWorkRequest {
	userId: string;
	workId: string;
}

/**
 * API Client for work-related operations
 * Provides type-safe methods for all work API endpoints
 */
export const workApiClient = {
	// Work CRUD operations
	async getWork(workSlug: string): Promise<WorkWithDetails> {
		return apiRequest<WorkWithDetails>("GET", `/api/works/${workSlug}`);
	},

	async getWorks(): Promise<WorkWithAuthor[]> {
		return apiRequest<WorkWithAuthor[]>("GET", "/api/works");
	},

	async createWork(data: CreateWorkRequest): Promise<WorkWithAuthor> {
		return apiRequest<WorkWithAuthor>("POST", "/api/works", data);
	},

	async updateWork(data: UpdateWorkRequest): Promise<WorkWithAuthor> {
		const { id, ...updateData } = data;
		return apiRequest<WorkWithAuthor>("PUT", `/api/works/${id}`, updateData);
	},

	async deleteWork(workId: number): Promise<void> {
		return apiRequest<void>("DELETE", `/api/works/${workId}`);
	},

	// Translation operations
	async getWorkTranslations(
		workSlug: string,
	): Promise<TranslationWithDetails[]> {
		return apiRequest<TranslationWithDetails[]>(
			"GET",
			`/api/works/${workSlug}/translations`,
		);
	},

	// Reading progress operations
	async updateReadingProgress(
		data: UpdateReadingProgressRequest,
	): Promise<ReadingProgress> {
		return apiRequest<ReadingProgress>("POST", "/api/reading-progress", data);
	},

	async getReadingProgress(
		userId: string,
		workId: string,
	): Promise<ReadingProgress | null> {
		return apiRequest<ReadingProgress | null>(
			"GET",
			`/api/reading-progress?userId=${userId}&workId=${workId}`,
		);
	},

	// Like operations
	async likeWork(data: LikeWorkRequest): Promise<Like> {
		return apiRequest<Like>("POST", "/api/likes", {
			userId: data.userId,
			entityType: "work",
			entityId: data.workId,
		});
	},

	async unlikeWork(data: LikeWorkRequest): Promise<void> {
		return apiRequest<void>("DELETE", "/api/likes", {
			userId: data.userId,
			entityType: "work",
			entityId: data.workId,
		});
	},

	// Search and filtering
	async searchWorks(query: string): Promise<WorkWithAuthor[]> {
		return apiRequest<WorkWithAuthor[]>(
			"GET",
			`/api/works/search?q=${encodeURIComponent(query)}`,
		);
	},

	async getWorksByAuthor(authorId: string): Promise<WorkWithAuthor[]> {
		return apiRequest<WorkWithAuthor[]>(
			"GET",
			`/api/works?authorId=${authorId}`,
		);
	},

	async getWorksByTag(tag: string): Promise<WorkWithAuthor[]> {
		return apiRequest<WorkWithAuthor[]>(
			"GET",
			`/api/works?tag=${encodeURIComponent(tag)}`,
		);
	},
};
