<script setup lang="ts">
import { computed } from 'vue'
import { RouterLink, RouterView, useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const auth = useAuthStore()
const router = useRouter()

interface NavItem { label: string; to: string; icon: string; show: boolean }

const nav = computed<NavItem[]>(() => [
  { label: 'Dashboard', to: '/app', icon: 'M3 3h7v7H3zM14 3h7v7h-7zM14 14h7v7h-7zM3 14h7v7H3z', show: true },
  { label: 'Reseller', to: '/app/resellers', icon: 'M3 21h18M5 21V7l8-4v18M19 21V11l-6-3', show: auth.isPlatformAdmin },
  { label: 'Firmen', to: '/app/companies', icon: 'M3 7h18v13H3zM8 7V5a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2', show: auth.isResellerAdmin || auth.isPlatformAdmin },
  { label: 'Mitarbeiter', to: '/app/employees', icon: 'M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2M9 11a4 4 0 1 0 0-8 4 4 0 0 0 0 8z', show: true },
  { label: 'Produkte', to: '/app/products', icon: 'M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16zM3.27 6.96 12 12.01l8.73-5.05M12 22.08V12', show: auth.isResellerAdmin || auth.isPlatformAdmin },
  { label: 'Editor', to: '/app/card-editor', icon: 'M3 5h18v14H3zM3 10h18M7 15h5', show: auth.isResellerAdmin || auth.isCompanyAdmin },
  { label: 'Bestellungen', to: '/app/orders', icon: 'M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4zM3 6h18M16 10a4 4 0 0 1-8 0', show: auth.isCompanyAdmin || auth.isResellerAdmin || auth.isPlatformAdmin },
  { label: 'Standorte', to: '/app/locations', icon: 'M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0zM12 13a3 3 0 1 0 0-6 3 3 0 0 0 0 6z', show: auth.isCompanyAdmin },
  { label: 'Domains', to: '/app/domains', icon: 'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18zM3 12h18M12 3a15 15 0 0 1 0 18 15 15 0 0 1 0-18z', show: auth.isCompanyAdmin },
  { label: 'Design', to: '/app/design', icon: 'M12 2l7 7a7 7 0 1 1-14 0z', show: auth.isCompanyAdmin },
  { label: 'Einstellungen', to: '/app/settings', icon: 'M4 21v-7M4 10V3M12 21v-9M12 8V3M20 21v-5M20 12V3M1 14h6M9 8h6M17 16h6', show: true },
].filter((i) => i.show))

const contextLabel = computed(() =>
  auth.user?.company?.name ?? auth.user?.reseller?.name ?? 'Plattform',
)

function logout() {
  auth.logout()
  router.push('/login')
}

async function stopImpersonation() {
  await auth.stopImpersonation()
  router.push('/app')
}
</script>

<template>
  <div class="shell">
    <aside class="sidebar">
      <div class="sidebar__brand">
        <span class="brand-logo" style="color:#fff">vcard4<span class="tag">reseller</span></span>
      </div>
      <nav class="sidebar__nav">
        <RouterLink v-for="item in nav" :key="item.to" :to="item.to" class="navlink"
                    :class="{ active: $route.path === item.to }">
          <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2"
               stroke-linecap="round" stroke-linejoin="round"><path :d="item.icon" /></svg>
          <span>{{ item.label }}</span>
        </RouterLink>
      </nav>
    </aside>

    <div class="main">
      <div v-if="auth.isImpersonating" class="imp-banner">
        <span>Du arbeitest als <strong>{{ auth.actingAs?.name }}</strong> ({{ auth.actingAs?.email }})</span>
        <button class="btn btn-sm" @click="stopImpersonation">Beenden</button>
      </div>
      <header class="topbar">
        <div class="topbar__ctx">
          <span class="muted">Mandant</span>
          <strong>{{ contextLabel }}</strong>
        </div>
        <div class="topbar__user">
          <span class="muted">{{ auth.user?.email }}</span>
          <button class="btn btn-ghost btn-sm" @click="logout">Abmelden</button>
        </div>
      </header>
      <main class="content">
        <RouterView />
      </main>
    </div>
  </div>
</template>

<style scoped>
.shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 240px; flex-shrink: 0; background: var(--sidebar); color: #cfcfd2;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.sidebar__brand { padding: 1.4rem 1.3rem; font-size: 1.15rem; }
.sidebar__nav { display: flex; flex-direction: column; gap: 2px; padding: .5rem .7rem; }
.navlink {
  display: flex; align-items: center; gap: .8rem; padding: .7rem .8rem;
  border-radius: var(--radius-sm); color: #c2c2c6; font-size: .92rem; font-weight: 600;
}
.navlink:hover { background: var(--sidebar-hover); color: #fff; text-decoration: none; }
.navlink.active { background: var(--psc-orange); color: #fff; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.imp-banner { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .55rem 1.6rem; background: var(--psc-orange); color: #fff; font-size: .9rem; font-weight: 600; }
.imp-banner .btn { background: #fff; color: var(--psc-orange-dark); }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.6rem; background: #fff; border-bottom: 1px solid var(--line);
}
.topbar__ctx { display: flex; flex-direction: column; line-height: 1.2; }
.topbar__ctx .muted { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; }
.topbar__user { display: flex; align-items: center; gap: 1rem; }
.content { padding: 1.8rem; }
</style>
