<script setup lang="ts">
import { nextTick, ref, watch } from 'vue'
import { Menu, Search, Sparkles } from '@lucide/vue'
import { RouterLink } from 'vue-router'

const props = withDefaults(defineProps<{
  connected?: boolean
  statusLabel?: string
  irisChatOpen?: boolean
  commandOpen?: boolean
  canOpenIris?: boolean
}>(), {
  connected: false,
  statusLabel: undefined,
  irisChatOpen: false,
  commandOpen: false,
  canOpenIris: false,
})

defineEmits<{
  'toggle-sidebar': []
  'open-command': []
  'open-iris': []
}>()

const irisChatButton = ref<HTMLButtonElement | null>(null)
const commandButton = ref<HTMLButtonElement | null>(null)

watch(
  () => props.irisChatOpen,
  async (isOpen, wasOpen) => {
    if (!isOpen && wasOpen) {
      await nextTick()
      if (!document.activeElement || document.activeElement === document.body) {
        irisChatButton.value?.focus()
      }
    }
  },
)

watch(
  () => props.commandOpen,
  async (isOpen, wasOpen) => {
    if (!isOpen && wasOpen) {
      await nextTick()
      if (!document.activeElement || document.activeElement === document.body) {
        commandButton.value?.focus()
      }
    }
  },
)
</script>

<template>
  <header class="topbar">
    <!-- Hamburger (mobile only) -->
    <button
      class="hamburger"
      type="button"
      aria-label="Open navigation"
      @click="$emit('toggle-sidebar')"
    >
      <Menu :size="20" aria-hidden="true" />
    </button>

    <!-- Search -->
    <button
      ref="commandButton"
      class="search"
      type="button"
      aria-label="Mission Control durchsuchen"
      aria-haspopup="dialog"
      aria-controls="mission-command-dialog"
      :aria-expanded="commandOpen"
      @click="$emit('open-command')"
    >
      <Search class="search-icon" :size="16" aria-hidden="true" />
      <span class="search-placeholder">Operationen, Agents oder Tasks suchen…</span>
      <kbd>Ctrl K</kbd>
    </button>

    <!-- Spacer -->
    <div class="spacer"></div>

    <!-- Status Pill -->
    <RouterLink
      to="/runs"
      :class="['pill', connected ? 'live' : 'preview']"
      :aria-label="connected ? 'OpenClaw connected, open Run Control' : 'OpenClaw disconnected, open Run Control'"
    >
      <span class="status-dot" :class="connected ? 'on' : 'off'"></span>
      {{ connected ? (statusLabel || 'OpenClaw verbunden') : 'OpenClaw offline' }}
    </RouterLink>

    <!-- Ask Iris Button -->
    <button
      ref="irisChatButton"
      class="btn btn-primary ask-iris-btn"
      type="button"
      :disabled="!canOpenIris"
      :aria-label="canOpenIris ? 'Iris Chat öffnen' : 'Iris Chat ist nur für Owner verfügbar'"
      :aria-haspopup="canOpenIris ? 'dialog' : undefined"
      :aria-controls="canOpenIris ? 'iris-chat-dialog' : undefined"
      :aria-expanded="canOpenIris ? irisChatOpen : false"
      :title="canOpenIris ? undefined : 'Nur Owner dürfen Iris verwenden'"
      @click="$emit('open-iris')"
    >
      <span class="btn-icon"><Sparkles :size="15" aria-hidden="true" /></span>
      <span class="ask-label">Iris Chat</span>
    </button>
  </header>
</template>

<style scoped>
.topbar {
  height: 62px;
  flex: 0 0 62px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(8,6,20,.5);
  backdrop-filter: blur(14px);
}

.search {
  flex: 1;
  max-width: 560px;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 38px;
  padding: 0 14px;
  border-radius: 11px;
  background: rgba(124,108,255,.06);
  border: 1px solid var(--line);
  color: var(--tx-3);
  font-size: 13.5px;
  font-family: 'Manrope', sans-serif;
  text-align: left;
  cursor: pointer;
}

.search-icon {
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
}

.search-placeholder {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.spacer {
  flex: 1;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 11px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 600;
  font-family: 'Manrope', sans-serif;
  border: 1px solid var(--line-2);
  background: rgba(124,108,255,.07);
  color: var(--tx-2);
  text-decoration: none;
}

.search kbd {
  margin-left: auto;
  padding: 2px 6px;
  border: 1px solid var(--line-2);
  border-radius: 6px;
  background: color-mix(in srgb, var(--space-3) 64%, transparent);
  color: var(--tx-3);
  font-family: var(--font-mono-v2);
  font-size: 10px;
}

.search:hover {
  border-color: var(--line-2);
  background: rgba(124,108,255,.1);
}

.search:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: 0 0 auto;
}

.status-dot.on {
  background: var(--st-work);
  box-shadow: 0 0 0 0 rgba(61,220,151,.5);
  animation: pulse-work 1.8s infinite;
}

.status-dot.off {
  background: var(--st-idle);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  padding: 0 14px;
  border-radius: 10px;
  font-family: 'Manrope', sans-serif;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  border: none;
  transition: filter .16s;
}

.btn-primary {
  background: var(--grad);
  color: #fff;
  box-shadow: var(--glow-purple);
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
}

.btn:disabled {
  cursor: not-allowed;
  opacity: .48;
  box-shadow: none;
}

.btn-icon :deep(svg) {
  width: 15px;
  height: 15px;
}

.hamburger {
  display: none;
}

@media (max-width: 900px) {
  .topbar {
    padding: 0 14px;
  }

  .search {
    flex: 1;
    max-width: none;
  }

  .search kbd {
    display: none;
  }

  .hamburger {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 9px;
    border: none;
    background: transparent;
    color: var(--tx-2);
    cursor: pointer;
    flex: 0 0 auto;
  }

  .hamburger:hover {
    background: rgba(124,108,255,.1);
    color: var(--tx);
  }

  .hamburger :deep(svg) {
    width: 20px;
    height: 20px;
  }

  .pill {
    display: none;
  }

  .ask-iris-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    display: grid;
    place-items: center;
    border-radius: 9px;
    flex: 0 0 auto;
  }

  .ask-label {
    display: none;
  }

  .ask-iris-btn .btn-icon {
    display: flex;
  }
}

</style>
