/** Core domain types for Cloud Reader — shared across web and mobile */

// ---- User & Auth ----

export interface User {
  id: number;
  email: string;
  username: string;
}

export interface TokenResponse {
  access: string;
  refresh: string;
}

export interface LoginPayload {
  email: string;
  password: string;
}

export interface RegisterPayload {
  email: string;
  username: string;
  password: string;
  password2: string;
}

// ---- Books ----

export interface Book {
  id: string;
  title: string;
  author: string;
  total_pages: number;
  cover_image: string;
  created_at: string;
  updated_at: string;
}

export interface BookSummary {
  id: string;
  title: string;
  author: string;
  total_pages: number;
  cover_image: string;
}

export interface BookListItem {
  id: number;
  title: string;
  author: string;
  genre: string;
  reading_status: string;
  reading_status_display: string;
  cover_image: string | null;
}

export interface BookDetail {
  id: number;
  title: string;
  author: string;
  genre: string;
  description: string;
  reading_status: string;
  reading_status_display: string;
  cover_image: string | null;
  total_pages: number;
  created_at: string;
  updated_at: string;
}

export interface BookSearchParams {
  q?: string;
  genre?: string;
  author?: string;
  reading_status?: string;
  ordering?: string;
  page?: number;
  page_size?: number;
}

export const READING_STATUS_OPTIONS = [
  "to_read",
  "reading",
  "finished",
  "dnf",
] as const;

// ---- E-Books ----

export interface EBookListItem {
  id: number;
  title: string;
  author: string;
  filename: string;
  format: string;
  page_count: number;
  cover_image: string | null;
  created_at: string;
  progress: number | null;
}

export interface EBookDetail {
  id: number;
  title: string;
  author: string;
  filename: string;
  file_url: string;
  format: string;
  page_count: number;
  file_size: number;
  metadata_json: Record<string, unknown>;
  cover_image: string | null;
  created_at: string;
  updated_at: string;
  progress: ReadingProgress | null;
}

export interface ReadingProgress {
  current_position: number;
  last_page: number;
}

export interface ReadingSettings {
  font_size: number;
  font_style: "sans-serif" | "serif" | "monospace";
  background_color: string;
}

export interface BookChapter {
  id: number;
  title: string;
  index: number;
  href: string;
  children: BookChapter[];
}

export interface TocResponse {
  chapters: BookChapter[];
  format: string;
  page_count: number;
}

export interface ContentResponse {
  page: number;
  total_pages: number;
  content: string;
  chapter_title: string;
  format: string;
}

// ---- Bookmarks & Notes ----

export interface Bookmark {
  id: string;
  book: string;
  book_title: string;
  page: number;
  location_text: string;
  created_at: string;
  updated_at: string;
}

export interface Note {
  id: string;
  book: string;
  book_title: string;
  page: number;
  location_text: string;
  content: string;
  created_at: string;
  updated_at: string;
}

export interface CreateBookmarkPayload {
  book: string;
  page: number;
  location_text?: string;
}

export interface CreateNotePayload {
  book: string;
  page: number;
  location_text?: string;
  content: string;
}

export interface UpdateNotePayload {
  content: string;
}

export type AnnotationKind = "bookmark" | "note";

export interface AnnotationEntry {
  id: string;
  kind: AnnotationKind;
  book_title: string;
  book_id: string;
  page: number;
  location_text: string;
  content?: string;
  created_at: string;
  updated_at: string;
}

// ---- Generic API shapes ----

export interface PaginatedResponse<T> {
  count: number;
  next: string | null;
  previous: string | null;
  results: T[];
}

export interface ApiError {
  detail?: string;
  [key: string]: unknown;
}