import api from "./client";
import type {
  Bookmark,
  Note,
  CreateBookmarkPayload,
  CreateNotePayload,
  PaginatedResponse,
} from "@cloud-reader/shared";

export function fetchBookmarks(
  bookId?: string,
): Promise<PaginatedResponse<Bookmark>> {
  const params = bookId ? { book: bookId } : {};
  return api
    .get<PaginatedResponse<Bookmark>>("/api/annotations/bookmarks/", { params })
    .then((res) => res.data);
}

export function createBookmark(
  payload: CreateBookmarkPayload,
): Promise<Bookmark> {
  return api
    .post<Bookmark>("/api/annotations/bookmarks/", payload)
    .then((res) => res.data);
}

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

export function fetchNotes(bookId?: string): Promise<PaginatedResponse<Note>> {
  const params = bookId ? { book: bookId } : {};
  return api
    .get<PaginatedResponse<Note>>("/api/annotations/notes/", { params })
    .then((res) => res.data);
}

export function createNote(payload: CreateNotePayload): Promise<Note> {
  return api
    .post<Note>("/api/annotations/notes/", payload)
    .then((res) => res.data);
}

export function updateNote(
  id: string,
  content: string,
): Promise<Note> {
  return api
    .patch<Note>(`/api/annotations/notes/${id}/`, { content })
    .then((res) => res.data);
}

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