<template>
  <Teleport to="body">
    <Transition name="fade">
      <div v-if="open" class="fixed inset-0 z-50 flex items-start justify-end bg-black/30" @click.self="$emit('close')">
        <div class="mt-0 h-full w-full max-w-md bg-white shadow-xl sm:mt-0 flex flex-col">
          <div class="flex items-center justify-between border-b border-gray-200 px-5 py-4">
            <h3 class="text-base font-semibold text-gray-900">AI-ассистент по материалам</h3>
            <button @click="$emit('close')" class="rounded-md p-1 text-gray-400 hover:bg-gray-100 hover:text-gray-600">
              <svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
              </svg>
            </button>
          </div>

          <div class="flex-1 overflow-y-auto p-5">
            <p class="mb-4 text-sm text-gray-600">
              Опишите вашу задачу, и AI порекомендует оптимальный материал.
            </p>

            <textarea
              v-model="taskDescription"
              rows="4"
              class="input-field mb-4"
              placeholder="Например: Корпус для уличного датчика температуры, диапазон -30..+50, водонепроницаемый"
            ></textarea>

            <button @click="getRecommendation" :disabled="!taskDescription.trim() || loading" class="btn-primary w-full mb-5">
              <svg v-if="loading" class="mr-2 h-4 w-4 animate-spin" fill="none" viewBox="0 0 24 24">
                <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"></path>
              </svg>
              {{ loading ? 'Анализирую...' : 'Получить рекомендацию' }}
            </button>

            <div v-if="error" class="mb-4 rounded-lg bg-red-50 p-3 text-sm text-red-700">{{ error }}</div>

            <div v-if="recommendation">
              <!-- Main recommendation -->
              <div class="mb-4 rounded-lg border-2 border-primary-200 bg-primary-50 p-4">
                <div class="flex items-center justify-between mb-2">
                  <span class="text-xs font-semibold uppercase tracking-wider text-primary-600">Рекомендация</span>
                  <button @click="selectMaterial(recommendation.recommended_material_id)" class="btn-primary !py-1 !px-3 !text-xs">
                    Выбрать
                  </button>
                </div>
                <p class="text-base font-bold text-gray-900 mb-1.5">{{ recommendation.recommended_material_name }}</p>
                <p class="text-sm text-gray-700 leading-relaxed">{{ recommendation.reasoning }}</p>
              </div>

              <!-- Alternatives -->
              <div v-if="recommendation.alternatives?.length" class="space-y-2.5">
                <p class="text-xs font-semibold uppercase tracking-wider text-gray-500">Альтернативы</p>
                <div v-for="alt in recommendation.alternatives" :key="alt.material_id" class="flex items-start justify-between rounded-lg border border-gray-200 p-3">
                  <div class="flex-1">
                    <p class="text-sm font-semibold text-gray-900">{{ alt.name }}</p>
                    <p class="text-xs text-gray-500 mt-0.5">{{ alt.why }}</p>
                  </div>
                  <button @click="selectMaterial(alt.material_id)" class="btn-secondary !py-1 !px-2.5 !text-xs ml-3 shrink-0">
                    Выбрать
                  </button>
                </div>
              </div>

              <!-- Questions -->
              <div v-if="recommendation.questions?.length" class="mt-4 rounded-lg bg-amber-50 p-3">
                <p class="text-xs font-semibold text-amber-700 mb-1.5">Уточняющие вопросы:</p>
                <ul class="list-disc pl-4 text-sm text-amber-700 space-y-1">
                  <li v-for="q in recommendation.questions" :key="q">{{ q }}</li>
                </ul>
              </div>
            </div>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { ref } from 'vue'
import api from '../api/client'
import { useCalculatorStore } from '../stores/calculator'

defineProps({ open: Boolean })
const emit = defineEmits(['close'])

const store = useCalculatorStore()
const taskDescription = ref('')
const loading = ref(false)
const error = ref('')
const recommendation = ref(null)

async function getRecommendation() {
  loading.value = true
  error.value = ''
  try {
    const payload = {
      task_description: taskDescription.value,
      budget_preference: 'optimal',
    }
    if (store.result?.file_info) {
      payload.file_info = {
        volume_cm3: store.result.file_info.volume_cm3,
        bounding_box_mm: store.result.file_info.bounding_box_mm,
      }
    }
    const { data } = await api.post('/advisor', payload)
    recommendation.value = data
  } catch (e) {
    error.value = e.response?.data?.detail || 'Ошибка получения рекомендации'
  } finally {
    loading.value = false
  }
}

function selectMaterial(id) {
  store.materialId = id
  store.result = null
  emit('close')
}
</script>

<style scoped>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
