import api from "./client";
import type { Book, PaginatedResponse } from "@cloud-reader/shared";

export function fetchBooks(
  page = 1,
  pageSize = 20,
): Promise<PaginatedResponse<Book>> {
  return api
    .get<PaginatedResponse<Book>>("/api/books/", {
      params: { page, page_size: pageSize },
    })
    .then((res) => res.data);
}

export function fetchBook(id: string): Promise<Book> {
  return api.get<Book>(`/api/books/${id}/`).then((res) => res.data);
}

export function searchBooks(
  query: string,
): Promise<PaginatedResponse<Book>> {
  return api
    .get<PaginatedResponse<Book>>("/api/books/search/", {
      params: { q: query },
    })
    .then((res) => res.data);
}

export function deleteBook(id: string): Promise<void> {
  return api.delete(`/api/books/${id}/`).then(() => {});
}