<script setup lang="ts">
import { ref, nextTick, watch } from 'vue'
import { icons } from '../../../composables/icons'
import type { ChatMessage } from './types'

const props = defineProps<{
  messages: ChatMessage[]
  isThinking: boolean
  error?: string | null
}>()

const emit = defineEmits<{
  send: [text: string]
}>()

const inputText = ref('')
const scrollEl = ref<HTMLElement | null>(null)

function handleSend() {
  const text = inputText.value.trim()
  if (!text) return
  emit('send', text)
  inputText.value = ''
}

function onKeydown(e: KeyboardEvent) {
  if (e.key === 'Enter' && !e.shiftKey) {
    e.preventDefault()
    handleSend()
  }
}

watch(
  () => props.messages.length,
  () => {
    nextTick(() => {
      if (scrollEl.value) scrollEl.value.scrollTop = scrollEl.value.scrollHeight
    })
  }
)
</script>

<template>
  <section class="iris-panel">
    <!-- Header -->
    <div class="iris-head">
      <div class="iris-av" v-html="icons.bot || ''"></div>
      <div>
        <div class="iris-name">Iris</div>
        <div class="iris-sub">Chief of Staff · <span class="online">online</span></div>
      </div>
      <button class="expand-btn" type="button" v-html="icons.expand || ''"></button>
    </div>

    <!-- Chat Scroll -->
    <div ref="scrollEl" class="chat-scroll">
      <div v-if="error" class="chat-msg-info error">⚠ {{ error }}</div>
      <div v-else-if="!messages.length && !isThinking" class="chat-msg-info">Noch keine Nachrichten.</div>

      <div v-for="(msg, i) in messages" :key="i" class="chat-row">
        <template v-if="msg.sender === 'iris'">
          <div class="bubble iris">{{ msg.text }}</div>
          <div v-if="msg.tool" class="tool">
            <span v-html="icons.doc || ''"></span>{{ msg.tool }}
          </div>
        </template>
        <div v-else class="bubble me">{{ msg.text }}</div>
      </div>

      <div v-if="isThinking" class="chat-row">
        <div class="bubble iris"><span class="caret"></span></div>
      </div>
    </div>

    <!-- Input -->
    <div class="chat-in">
      <input
        v-model="inputText"
        type="text"
        placeholder="Nachricht an Iris…"
        @keydown="onKeydown"
      />
      <button class="send" type="button" @click="handleSend" v-html="icons.send || ''"></button>
    </div>
  </section>
</template>

<style scoped>
.iris-panel {
  width: var(--rail-w, 360px);
  flex: 0 0 var(--rail-w, 360px);
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: linear-gradient(180deg, rgba(20,17,48,.6), rgba(12,10,30,.6));
  border: 1px solid var(--line);
  border-radius: var(--r);
  backdrop-filter: blur(12px);
  overflow: hidden;
}

/* ── Header ─────────────────────────────────── */
.iris-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.iris-av {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--grad);
  display: grid;
  place-items: center;
  box-shadow: var(--glow-purple);
  flex: 0 0 auto;
}

.iris-av :deep(svg) {
  width: 18px;
  height: 18px;
  color: #fff;
}

.iris-name {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 600;
  font-size: 14.5px;
  color: var(--tx);
  line-height: 1.2;
}

.iris-sub {
  font-size: 11px;
  color: var(--tx-3);
  margin-top: 1px;
}

.online {
  color: var(--st-work);
}

.expand-btn {
  margin-left: auto;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  border: none;
  background: transparent;
  color: var(--tx-2);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background .15s, color .15s;
  flex: 0 0 auto;
}

.expand-btn:hover {
  background: rgba(124,108,255,.10);
  color: var(--tx);
}

.expand-btn :deep(svg) {
  width: 16px;
  height: 16px;
}

/* ── Messages ────────────────────────────────── */
.chat-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.chat-scroll::-webkit-scrollbar { width: 6px; }
.chat-scroll::-webkit-scrollbar-thumb {
  background: rgba(124,108,255,.22);
  border-radius: 6px;
}
.chat-scroll::-webkit-scrollbar-track { background: transparent; }

.chat-msg-info {
  font-family: 'Manrope', sans-serif;
  font-size: 12px;
  color: var(--tx-3);
  font-style: italic;
  text-align: center;
  padding: 24px 0;
}

.chat-msg-info.error { color: #fda4b0; font-style: normal; }

.chat-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.bubble {
  max-width: 84%;
  padding: 10px 13px;
  border-radius: 14px;
  font-family: 'Manrope', sans-serif;
  font-size: 13px;
  line-height: 1.5;
}

.bubble.iris {
  background: rgba(124,108,255,.12);
  border: 1px solid var(--line-2);
  border-bottom-left-radius: 5px;
  color: var(--tx);
}

.bubble.me {
  background: var(--grad);
  color: #fff;
  border-bottom-right-radius: 5px;
  margin-left: auto;
  box-shadow: var(--glow-purple);
}

.tool {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--st-think);
  padding-left: 4px;
}

.tool :deep(svg) {
  width: 12px;
  height: 12px;
}

.caret::after {
  content: '▍';
  animation: blink 1s steps(1) infinite;
  color: var(--st-think);
}

@keyframes blink { 50% { opacity: 0; } }

@media (max-width: 767px) {
  .iris-panel {
    width: 100%;
    flex: 0 0 auto;
    max-height: 45vh;
  }

  .chat-scroll {
    max-height: 30vh;
  }

  .expand-btn {
    display: none;
  }
}

/* ── Input ───────────────────────────────────── */
.chat-in {
  padding: 12px;
  border-top: 1px solid var(--line);
  display: flex;
  gap: 9px;
  align-items: center;
  flex: 0 0 auto;
}

.chat-in input {
  flex: 1;
  height: 40px;
  border-radius: 11px;
  border: 1px solid var(--line-2);
  background: rgba(124,108,255,.06);
  color: var(--tx);
  padding: 0 14px;
  font-family: 'Manrope', sans-serif;
  font-size: 13px;
  outline: none;
  transition: border-color .15s;
}

.chat-in input::placeholder { color: var(--tx-3); }
.chat-in input:focus { border-color: var(--line-3); }

.send {
  width: 40px;
  height: 40px;
  border-radius: 11px;
  border: none;
  background: var(--grad);
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: var(--glow-purple);
  flex: 0 0 auto;
  transition: filter .15s;
}

.send:hover { filter: brightness(1.1); }

.send :deep(svg) {
  width: 17px;
  height: 17px;
  color: #fff;
}
</style>
