import { PDFStatus } from '../types';
import { apiClient } from './apiClient';

export const pdfPreviewService = {
  // PDFプレビューURLの取得
  async getPDFUrl(fileId: string): Promise<{ url: string }> {
    const { data } = await apiClient.get<{ url: string }>(`/knowledge-bases/${fileId}/pdf-url`);
    return data;
  },

  // PDFステータスの確認
  async getPDFStatus(fileId: string): Promise<PDFStatus> {
    const { data } = await apiClient.get<PDFStatus>(`/knowledge-bases/${fileId}/pdf-status`);
    return data;
  },

  // PDFのプリロード（変換のトリガー）
  async preloadPDF(fileId: string, force: boolean = false): Promise<void> {
    try {
      const url = `/knowledge-bases/${fileId}/pdf-url` + (force ? '?force=true' : '');
      const response = await apiClient.request(url, {
        method: 'GET',
        signal: AbortSignal.timeout(30000), // Increase timeout for conversion
      });

      if (response.ok) {
        console.log('PDF already exists or conversion completed');
      }
    } catch (error: any) {
      console.log('PDF conversion triggered:', error.message);
    }
  },
};