<script setup>
// Präsentationaler Chat (Nachrichtenliste + Typing + Eingabe). Die Mechanik (send/cancel/
// scroll/focus) lebt in useChat; hier wird nur das `chat`-Objekt gerendert. Ersetzt die
// zeichengleichen Chat-Blöcke in BlockPanel und TopicDetail.
import { renderMarkdown } from '../markdown.js'

const props = defineProps({
  chat: { type: Object, required: true },       // Rückgabe von useChat()
  hint: { type: String, default: '' },          // Platzhaltertext bei leerer Historie
  placeholder: { type: String, default: '' },
  rows: { type: Number, default: 2 },
  autoGrow: { type: Boolean, default: false },
  maxHeight: { type: String, default: '320px' },
})

function onInput() {
  if (props.autoGrow) props.chat.autoGrow()
}
</script>

<template>
  <div :ref="chat.messagesEl" class="ct-messages" :style="{ maxHeight }" @scroll="chat.onScroll">
    <p v-if="!chat.messages.value.length" class="ct-hint">{{ hint }}</p>
    <template v-for="(m, i) in chat.messages.value" :key="i">
      <div v-if="m.role === 'assistant'" class="ct-msg assistant markdown" v-html="renderMarkdown(m.content)"></div>
      <div v-else class="ct-msg user">{{ m.content }}</div>
    </template>
    <div v-if="chat.loading.value" class="ct-msg assistant ct-typing">Denkt nach…</div>
  </div>
  <div class="ct-input">
    <textarea
      :ref="chat.inputEl"
      v-model="chat.input.value"
      :rows="rows"
      :placeholder="placeholder"
      @input="onInput"
      @keydown.enter.exact.prevent="chat.send"
    ></textarea>
    <button
      :disabled="!chat.input.value.trim() && !chat.loading.value"
      :class="{ cancel: chat.loading.value }"
      :title="chat.loading.value ? 'Abbrechen' : 'Senden'"
      @click="chat.send"
    >{{ chat.loading.value ? '✕' : '➤' }}</button>
  </div>
</template>

<style scoped>
/* flex:1 füllt ein Panel mit fester Höhe (TopicDetail); maxHeight begrenzt sonst (BlockPanel-Tab). */
.ct-messages { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 0.4rem; overflow-y: auto; padding: 0.2rem; }
.ct-hint { font-size: 0.85rem; color: var(--text-muted); margin: 0 0 0.5rem; }
.ct-msg {
  max-width: 85%;
  padding: 0.4rem 0.6rem;
  border-radius: 8px;
  font-size: 0.85rem;
  line-height: 1.4;
  word-break: break-word;
}
.ct-msg.user { align-self: flex-end; background: var(--accent); color: var(--on-accent); white-space: pre-wrap; }
.ct-msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--border); }
.ct-typing { color: var(--text-faint); font-style: italic; }
.ct-input { display: flex; gap: 0.4rem; margin-top: 0.55rem; align-items: flex-end; }
.ct-input textarea {
  flex: 1;
  resize: none;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg, var(--panel));
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
}
.ct-input textarea:focus { outline: none; border-color: var(--accent); }
.ct-input button {
  flex: 0 0 auto;
  width: 38px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
  font-size: 1rem;
}
.ct-input button:disabled { opacity: 0.5; cursor: default; }
.ct-input button.cancel { background: var(--danger); }
</style>
