/**
 * API client for the reader module — reading settings, chapters, and progress.
 * Uses the shared axios client so JWT auth is attached automatically.
 */

import api from "./client";
import type {
  ChapterDetail,
  ChapterSummary,
  ReadingProgress,
  ReadingSettings,
} from "../types/reader";

interface TocChapter {
  id: number;
  title: string;
  index: number;
  href?: string;
}

export async function getReadingSettings(): Promise<ReadingSettings> {
  const { data } = await api.get<ReadingSettings>("/reader/settings/");
  return data;
}

export async function updateReadingSettings(
  settings: Partial<ReadingSettings>,
): Promise<ReadingSettings> {
  const { data } = await api.patch<ReadingSettings>("/reader/settings/", settings);
  return data;
}

export async function getChapters(bookId: number): Promise<ChapterSummary[]> {
  const { data } = await api.get<{ chapters: TocChapter[] }>(
    `/books/ebooks/${bookId}/toc/`,
  );
  const chapters = data.chapters ?? [];
  return chapters.map((ch) => ({
    id: ch.id,
    book: bookId,
    title: ch.title,
    number: ch.index + 1,
  }));
}

export async function getChapterContent(
  bookId: number,
  chapterNumber: number,
): Promise<ChapterDetail> {
  const { data } = await api.get<{
    page: number;
    chapter_title: string;
    content: string;
  }>(`/books/ebooks/${bookId}/content/`, { params: { page: chapterNumber } });
  return {
    id: chapterNumber,
    book: bookId,
    title: data.chapter_title ?? "",
    number: data.page ?? chapterNumber,
    content: data.content ?? "",
    created_at: "",
    updated_at: "",
  };
}

export async function getReadingProgress(
  bookId: number,
): Promise<ReadingProgress> {
  const { data } = await api.get<{
    current_position: number;
    last_page: number;
    epub_location?: string;
    updated_at?: string;
  }>(`/books/ebooks/${bookId}/progress/`);
  return {
    id: bookId,
    book: bookId,
    current_chapter: data.last_page || 1,
    current_position: data.current_position ?? 0,
    percentage: data.current_position ?? 0,
    epub_location: data.epub_location ?? "",
    updated_at: data.updated_at ?? "",
  };
}

export async function updateReadingProgress(
  bookId: number,
  progress: Partial<ReadingProgress>,
): Promise<ReadingProgress> {
  const body: Record<string, string | number> = {};
  if (progress.percentage !== undefined || progress.current_position !== undefined) {
    body.current_position = progress.percentage ?? progress.current_position ?? 0;
  }
  if (progress.current_chapter !== undefined) {
    body.last_page = progress.current_chapter;
  }
  if (progress.epub_location !== undefined) {
    body.epub_location = progress.epub_location;
  }
  const { data } = await api.patch<{
    current_position: number;
    last_page: number;
    epub_location?: string;
    updated_at?: string;
  }>(`/books/ebooks/${bookId}/progress/`, body);
  return {
    id: bookId,
    book: bookId,
    current_chapter: data.last_page || 1,
    current_position: data.current_position ?? 0,
    percentage: data.current_position ?? 0,
    epub_location: data.epub_location ?? "",
    updated_at: data.updated_at ?? "",
  };
}
