import { api } from './api';
import type { BookStackRecentResponse, BookStackSearchResponse, BookStackPageDetail } from '../types/bookstack.types';

interface ApiResponse<T> {
  success: boolean;
  data: T;
  configured: boolean;
}

export const bookstackApi = {
  getRecent(): Promise<BookStackRecentResponse> {
    return api
      .get<ApiResponse<BookStackRecentResponse['data']>>('/api/bookstack/recent')
      .then((r) => ({ configured: r.data.configured, data: r.data.data }));
  },

  getPage(id: number): Promise<{ configured: boolean; data: BookStackPageDetail | null }> {
    return api
      .get<ApiResponse<BookStackPageDetail | null>>(`/api/bookstack/pages/${id}`)
      .then((r) => ({ configured: r.data.configured, data: r.data.data }));
  },

  search(query: string): Promise<BookStackSearchResponse> {
    return api
      .get<ApiResponse<BookStackSearchResponse['data']>>('/api/bookstack/search', {
        params: { query },
      })
      .then((r) => ({ configured: r.data.configured, data: r.data.data }));
  },
};
