export interface RagSearchResult {
  content: string;
  fileName: string;
  score: number;
  chunkIndex: number;
}

export interface RagResponse {
  searchResults: RagSearchResult[];
  sources: string[];
  ragPrompt: string;
  hasRelevantContent: boolean;
}

export const ragService = {
  /**
   * RAG サービス - RAG 検索結果の直接取得を担当（チャットインターフェースではなく、デバッグや検証用）
   */
  async search(query: string, settings: any, authToken: string): Promise<RagResponse> {
    try {
      const response = await fetch('/api/knowledge-bases/rag-search', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${authToken}`,
        },
        body: JSON.stringify({ query, settings }),
      });

      if (!response.ok) {
        const errorData = await response.text();
        console.error('RAG search error:', response.status, errorData);
        throw new Error(`RAG search failed: ${response.status} - ${errorData}`);
      }

      const result = await response.json();
      return result;
    } catch (error) {
      console.error('RAG service error:', error);
      throw error;
    }
  },
};