import { apiClient } from "./client";
import type {
  EBookListItem,
  EBookDetail,
  ReadingProgress,
  ReadingSettings,
  TocResponse,
  ContentResponse,
  PaginatedResponse,
} from "@cloud-reader/shared";

export const ebooksApi = {
  /** List uploaded e-books */
  list() {
    return apiClient.get<PaginatedResponse<EBookListItem>>("/api/ebooks/");
  },

  /** Get e-book detail */
  get(id: number) {
    return apiClient.get<EBookDetail>(`/api/ebooks/${id}/`);
  },

  /** Get table of contents */
  getToc(id: number) {
    return apiClient.get<TocResponse>(`/api/ebooks/${id}/toc/`);
  },

  /** Get page content */
  getContent(id: number, page: number) {
    return apiClient.get<ContentResponse>(
      `/api/ebooks/${id}/content/?page=${page}`,
    );
  },

  /** Update reading progress */
  updateProgress(id: number, data: Partial<ReadingProgress>) {
    return apiClient.patch<ReadingProgress>(
      `/api/ebooks/${id}/progress/`,
      data,
    );
  },

  /** Get or update reading settings */
  getSettings(id: number) {
    return apiClient.get<ReadingSettings>(`/api/ebooks/${id}/settings/`);
  },

  updateSettings(id: number, data: Partial<ReadingSettings>) {
    return apiClient.patch<ReadingSettings>(
      `/api/ebooks/${id}/settings/`,
      data,
    );
  },
};