<script setup>
import { ref } from 'vue'
import { suggestTopics } from '../api.js'

const emit = defineEmits(['close', 'selectTopic'])

const problem = ref('')
const loading = ref(false)
const submitted = ref(false)
const suggestions = ref([])
const error = ref(false)

async function submit() {
  const text = problem.value.trim()
  if (!text || loading.value) return
  loading.value = true
  submitted.value = true
  error.value = false
  suggestions.value = []
  try {
    const result = await suggestTopics(text)
    suggestions.value = Array.isArray(result) ? result : []
  } catch (e) {
    error.value = true
  } finally {
    loading.value = false
  }
}

function pick(title) {
  emit('selectTopic', title)
}
</script>

<template>
  <main class="help-chat">
    <header class="help-header">
      <h2>Passendes Thema finden</h2>
      <button class="close-btn" title="Schließen" @click="emit('close')">×</button>
    </header>

    <div class="help-body">
      <p class="hint">Beschreibe dein Problem oder Lernziel – die KI schlägt dir passende Themen-Namen vor, zu denen du einen Guide generieren kannst.</p>

      <div class="input-row">
        <textarea
          v-model="problem"
          placeholder="Womit hast du Probleme? Was möchtest du lernen?"
          :disabled="loading"
          @keyup.enter.exact.prevent="submit"
        ></textarea>
        <button
          class="send-btn"
          :disabled="!problem.trim() || loading"
          @click="submit"
        >{{ loading ? '…' : 'Themen finden' }}</button>
      </div>

      <div v-if="loading" class="status">Suche Themen…</div>
      <div v-else-if="error" class="status error">Fehler bei der Anfrage. Bitte erneut versuchen.</div>
      <div v-else-if="submitted && !suggestions.length" class="status">Keine Vorschläge gefunden.</div>

      <ul v-else class="suggestions">
        <li v-for="(s, i) in suggestions" :key="i" class="suggestion" @click="pick(s.title)">
          <span class="suggestion-title">{{ s.title }}</span>
          <span class="suggestion-reason" v-if="s.reason">{{ s.reason }}</span>
        </li>
      </ul>
    </div>
  </main>
</template>

<style scoped>
.help-chat {
  flex: 1;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f8f9fb;
  overflow: hidden;
}

.help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  background: #fff;
  border-bottom: 1px solid #e2e5e9;
}

.help-header h2 {
  font-size: 1.1rem;
  font-weight: 700;
  color: #1a1a1a;
}

.close-btn {
  border: none;
  background: none;
  font-size: 1.6rem;
  line-height: 1;
  color: #4b5563;
  cursor: pointer;
  padding: 0 6px;
}

.close-btn:hover {
  color: #6366f1;
}

.help-body {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}

.hint {
  color: #5a6470;
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.input-row textarea {
  flex: 1;
  min-height: 70px;
  resize: vertical;
  padding: 8px 10px;
  border: 1px solid #d8dde3;
  border-radius: 6px;
  font-size: 0.9rem;
  font-family: inherit;
  outline: none;
}

.input-row textarea:focus {
  border-color: #6366f1;
}

.input-row textarea:disabled {
  background: #f3f4f6;
}

.send-btn {
  padding: 8px 14px;
  border: none;
  background: #6366f1;
  color: #fff;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.status {
  margin-top: 1.25rem;
  color: #5a6470;
  font-size: 0.9rem;
}

.status.error {
  color: #991b1b;
}

.suggestions {
  list-style: none;
  margin-top: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.suggestion {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 14px;
  background: #d1fae5;
  border: 1px solid #34d399;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}

.suggestion:hover {
  background: #a7f3d0;
  border-color: #059669;
}

.suggestion-title {
  font-weight: 700;
  color: #065f46;
}

.suggestion-reason {
  font-size: 0.82rem;
  color: #047857;
}
</style>
