<script setup>
import { ref, computed, watch } from 'vue'
import { fetchBausteineUebersicht } from '../api.js'

const props = defineProps({
  topic: { type: String, required: true },
})
const emit = defineEmits(['close'])

const items = ref([])
const loading = ref(true)
const error = ref(null)

// Stufen-Reihenfolge + Anzeige-Label (Farbe via CSS-Klasse st-<key>)
const STUFEN = [
  { key: 'einfach', label: 'Einfach' },
  { key: 'mittel', label: 'Mittel' },
  { key: 'schwer', label: 'Schwer' },
]

watch(() => props.topic, load, { immediate: true })

async function load() {
  loading.value = true
  error.value = null
  items.value = []
  try {
    items.value = await fetchBausteineUebersicht(props.topic)
  } catch (e) {
    error.value = 'Übersicht nicht verfügbar — bitte erst Bausteine erstellen.'
  } finally {
    loading.value = false
  }
}

// Baustein relevant = hat ≥1 relevanten Subbaustein (gleiche Regel wie der Guide).
// Ohne Relevanz-Daten (Alt-Themen) nicht dimmen.
function relevant(b) {
  const mitRelevanz = (b.subbausteine || []).filter((s) => s.relevanz)
  return !mitRelevanz.length || mitRelevanz.some((s) => s.relevanz === 'relevant')
}

// Nur nicht-leere Stufen-Gruppen je Baustein (v-if + v-for nicht auf einem Element)
function gruppen(b) {
  return STUFEN
    .map((st) => ({ ...st, subs: (b.subbausteine || []).filter((s) => s.stufe === st.key) }))
    .filter((g) => g.subs.length)
}

const subTotal = computed(() => items.value.reduce((n, b) => n + (b.subbausteine?.length || 0), 0))
</script>

<template>
  <div class="bk-view">
    <header class="bk-head">
      <h1>{{ topic }}</h1>
      <span class="bk-sub">Bausteine-Übersicht</span>
      <span v-if="items.length" class="bk-count">{{ items.length }} Bausteine · {{ subTotal }} Subbausteine</span>
      <span class="bk-spacer"></span>
      <button class="bk-close" title="Schließen" @click="emit('close')">✕</button>
    </header>

    <div v-if="loading" class="bk-empty-state">Lade…</div>
    <div v-else-if="error" class="bk-empty-state">{{ error }}</div>
    <div v-else-if="!items.length" class="bk-empty-state">Noch keine Bausteine.</div>

    <div v-else class="bk-grid">
      <article
        v-for="b in items"
        :key="b.num"
        class="bk-card"
        :class="{ 'bk-irrelevant': !relevant(b) }"
        :title="relevant(b) ? null : 'Nicht relevant — kommt später in den „Rest“'"
      >
        <h3 class="bk-title"><span class="bk-num">{{ b.num }}</span>{{ b.titel }}</h3>
        <p v-if="b.beschreibung" class="bk-desc">{{ b.beschreibung }}</p>
        <div v-if="b.subbausteine && b.subbausteine.length" class="bk-stufen">
          <div v-for="g in gruppen(b)" :key="g.key" class="bk-stufe" :class="'st-' + g.key">
            <span class="bk-stufe-label">{{ g.label }}</span>
            <ul>
              <li v-for="s in g.subs" :key="s.titel" :class="{ rand: s.relevanz === 'rand' }">
                {{ s.titel }}<span v-if="s.relevanz === 'rand'" class="rand-tag" title="Randthema — kommt später in den „Rest“">Rand</span>
              </li>
            </ul>
          </div>
        </div>
        <p v-else class="bk-no-subs">Keine Subbausteine.</p>
      </article>
    </div>
  </div>
</template>

<style scoped>
.bk-view {
  flex: 1;
  min-width: 0;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg-preview);
}

.bk-head {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  padding: 1.25rem 2rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.bk-head h1 { font-size: 1.5rem; }
.bk-sub { color: var(--text-faint); font-size: 0.9rem; font-weight: 600; }
.bk-count { color: var(--text-muted); font-size: 0.82rem; }
.bk-spacer { flex: 1; }
.bk-close {
  align-self: center;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  width: 2rem;
  height: 2rem;
  cursor: pointer;
}
.bk-close:hover { border-color: var(--accent); }

.bk-empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

.bk-grid {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 2rem 4rem;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1rem;
  align-content: start;
}

.bk-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-top: 3px solid var(--accent-border);
  border-radius: 10px;
  padding: 1rem 1.1rem;
}
/* Nicht relevante Bausteine (kein relevanter Subbaustein) gedimmt */
.bk-card.bk-irrelevant { opacity: 0.5; }

.bk-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1rem;
  margin-bottom: 0.4rem;
}
.bk-num {
  flex: 0 0 auto;
  min-width: 26px;
  height: 26px;
  padding: 0 6px;
  border-radius: 7px;
  background: var(--accent);
  color: var(--on-accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  font-weight: 700;
}
.bk-desc {
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1.45;
  margin-bottom: 0.7rem;
}

.bk-stufen { display: flex; flex-direction: column; gap: 0.55rem; }
.bk-stufe {
  border-left: 3px solid var(--st);
  padding-left: 0.6rem;
}
.bk-stufe.st-einfach { --st: var(--success-border); }
.bk-stufe.st-mittel { --st: var(--warning-border); }
.bk-stufe.st-schwer { --st: var(--danger); }

.bk-stufe-label {
  font-size: 0.66rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--st);
}
.bk-stufe ul {
  list-style: disc;
  margin: 0.25rem 0 0;
  padding-left: 1.15rem;
}
.bk-stufe li {
  font-size: 0.85rem;
  color: var(--text);
  line-height: 1.35;
  margin-bottom: 2px;
}
.bk-stufe li::marker { color: var(--text-faint); }
.bk-stufe li.rand { color: var(--text-faint); }
.rand-tag {
  margin-left: 0.4rem;
  font-size: 0.6rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-faint);
  border: 1px solid var(--border-strong);
  border-radius: 4px;
  padding: 0 4px;
}

.bk-no-subs { color: var(--text-faint); font-size: 0.8rem; font-style: italic; }
</style>
