/**
 * GET /api/admin/rag-info
 *
 * Retourne le statut du système RAG (v1 + v2) pour la page /admin/rag-status
 */

import { existsSync, readFileSync } from 'fs'
import { resolve } from 'path'

export default defineEventHandler(async (_event) => {
  // Statut V2 : compter les embeddings
  let v2Count = 0
  let v2Date: string | null = null
  let v2Model: string | null = null

  try {
    // Chercher depuis process.cwd() (racine du projet Nuxt)
    const embPath = resolve(process.cwd(), 'server', 'data', 'embeddings-v2.json')
    if (existsSync(embPath)) {
      const data = JSON.parse(readFileSync(embPath, 'utf-8'))
      v2Count = data.embeddings?.length ?? 0
      v2Date = data.meta?.date ?? null
      v2Model = data.meta?.model ?? null
    }
  } catch (e: any) {
    console.warn('[rag-info] Erreur lecture embeddings-v2.json :', e?.message ?? e)
  }

  return {
    v2_embeddings_count: v2Count,
    v2_ready: v2Count > 0,
    v2_model: v2Model ?? 'mistral-embed',
    v2_generated_date: v2Date ?? null,
    v1_enabled: process.env.RAG_V1_ENABLED !== 'false',
    v1_deprecation_date: process.env.RAG_V1_DEPRECATION_DATE ?? 'non défini',
    model_chat: 'mistral-small-latest',
    setup_command: 'MISTRAL_API_KEY=xxx node scripts/vectorize-v2.js'
  }
})
