<template>
  <div class="min-h-screen bg-gray-950 text-gray-100 p-8">
    <div class="max-w-2xl mx-auto">
      <h1 class="text-2xl font-bold mb-2 text-white">RAG Status - Admin</h1>
      <p class="text-gray-400 text-sm mb-8">Coexistence V1 (keyword NocoDB) + V2 (embeddings Mistral)</p>

      <!-- Bloc V2 -->
      <div class="bg-gray-900 rounded-lg p-6 mb-4 border" :class="info?.v2_ready ? 'border-green-700' : 'border-yellow-700'">
        <div class="flex items-center gap-3 mb-4">
          <span class="text-lg font-semibold">RAG V2 - Embeddings vectoriels</span>
          <span
            class="px-2 py-0.5 rounded text-xs font-mono"
            :class="info?.v2_ready ? 'bg-green-900 text-green-300' : 'bg-yellow-900 text-yellow-300'"
          >
            {{ info?.v2_ready ? 'ACTIF' : 'EN ATTENTE' }}
          </span>
        </div>

        <div v-if="info" class="space-y-2 text-sm text-gray-300">
          <div class="flex justify-between">
            <span class="text-gray-500">Fiches vectorisées</span>
            <span class="font-mono">{{ info.v2_embeddings_count }} / 120</span>
          </div>
          <div class="flex justify-between">
            <span class="text-gray-500">Modèle embed</span>
            <span class="font-mono">{{ info.v2_model }}</span>
          </div>
          <div class="flex justify-between">
            <span class="text-gray-500">Modèle chat</span>
            <span class="font-mono">{{ info.model_chat }}</span>
          </div>
          <div class="flex justify-between">
            <span class="text-gray-500">Généré le</span>
            <span class="font-mono">{{ info.v2_generated_date ? info.v2_generated_date.slice(0, 10) : 'jamais' }}</span>
          </div>
        </div>

        <div v-if="!info?.v2_ready" class="mt-4 bg-gray-800 rounded p-3 text-xs font-mono text-yellow-300">
          {{ info?.setup_command ?? 'MISTRAL_API_KEY=xxx node scripts/vectorize-v2.js' }}
        </div>
      </div>

      <!-- Bloc V1 -->
      <div class="bg-gray-900 rounded-lg p-6 mb-6 border" :class="info?.v1_enabled ? 'border-blue-700' : 'border-gray-700'">
        <div class="flex items-center gap-3 mb-4">
          <span class="text-lg font-semibold">RAG V1 - Keyword matching (NocoDB)</span>
          <span
            class="px-2 py-0.5 rounded text-xs font-mono"
            :class="info?.v1_enabled ? 'bg-blue-900 text-blue-300' : 'bg-gray-800 text-gray-500'"
          >
            {{ info?.v1_enabled ? 'ACTIF' : 'DÉSACTIVÉ' }}
          </span>
        </div>

        <div v-if="info" class="space-y-2 text-sm text-gray-300">
          <div class="flex justify-between">
            <span class="text-gray-500">RAG_V1_ENABLED</span>
            <span class="font-mono">{{ info.v1_enabled }}</span>
          </div>
          <div class="flex justify-between">
            <span class="text-gray-500">Déprecation prévue</span>
            <span class="font-mono">{{ info.v1_deprecation_date }}</span>
          </div>
        </div>
      </div>

      <!-- Testeur chatbot-v2 -->
      <div class="bg-gray-900 rounded-lg p-6 border border-gray-700">
        <h2 class="text-base font-semibold mb-4">Tester /api/chatbot-v2</h2>

        <div class="flex gap-2 mb-4">
          <input
            v-model="testQuestion"
            type="text"
            placeholder="Ex : structures de réemploi en Belgique"
            class="flex-1 bg-gray-800 border border-gray-600 rounded px-3 py-2 text-sm text-white placeholder-gray-500 focus:outline-none focus:border-blue-500"
            @keydown.enter="runTest"
          />
          <button
            class="px-4 py-2 bg-blue-700 hover:bg-blue-600 rounded text-sm font-medium transition-colors disabled:opacity-50"
            :disabled="testLoading || !testQuestion.trim()"
            @click="runTest"
          >
            {{ testLoading ? 'En cours...' : 'Tester' }}
          </button>
        </div>

        <div v-if="testError" class="bg-red-900/50 border border-red-700 rounded p-3 text-sm text-red-300 mb-3">
          {{ testError }}
        </div>

        <div v-if="testResult" class="space-y-3">
          <div class="bg-gray-800 rounded p-3 text-sm">
            <div class="text-gray-400 text-xs mb-1 font-mono">reponse_texte</div>
            <p class="text-gray-100 leading-relaxed">{{ testResult.reponse_texte }}</p>
          </div>

          <div v-if="testResult.fiches_recommandees?.length" class="bg-gray-800 rounded p-3 text-sm">
            <div class="text-gray-400 text-xs mb-2 font-mono">fiches_recommandees</div>
            <div v-for="f in testResult.fiches_recommandees" :key="f.fiche_id" class="mb-2">
              <span class="font-mono text-blue-300 text-xs">{{ f.fiche_id }}</span>
              <span class="text-white ml-2">{{ f.nom }}</span>
              <p class="text-gray-400 text-xs mt-0.5">{{ f.explication }}</p>
            </div>
          </div>

          <div v-if="testResult.sources?.length" class="bg-gray-800 rounded p-3 text-sm">
            <div class="text-gray-400 text-xs mb-2 font-mono">sources (top-5 cosine)</div>
            <div v-for="s in testResult.sources" :key="s.fiche_id" class="flex justify-between text-xs mb-1">
              <span class="text-gray-300 font-mono">{{ s.fiche_id }}</span>
              <span class="text-blue-400 font-mono">{{ s.score?.toFixed(4) }}</span>
            </div>
          </div>

          <div class="text-xs font-mono px-2" :class="testResult.v2_ready ? 'text-green-400' : 'text-yellow-400'">
            v2_ready: {{ testResult.v2_ready }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
const { data: info } = await useFetch('/api/admin/rag-info')

const testQuestion = ref('')
const testResult = ref<any>(null)
const testError = ref<string | null>(null)
const testLoading = ref(false)

async function runTest() {
  if (!testQuestion.value.trim()) return
  testLoading.value = true
  testError.value = null
  testResult.value = null

  try {
    const res = await $fetch('/api/chatbot-v2', {
      method: 'POST',
      body: { question: testQuestion.value }
    })
    testResult.value = res
  } catch (e: any) {
    testError.value = e?.data?.statusMessage ?? e?.message ?? 'Erreur inconnue'
  } finally {
    testLoading.value = false
  }
}
</script>
