<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
import { RouterView, RouterLink, useRoute, useRouter } from 'vue-router';
import { useAuthStore } from '../stores/auth';
import { useAdminLocale } from '../composables/useAdminLocale';
import AdminLocaleSwitcher from '../components/AdminLocaleSwitcher.vue';
import { resolveAdminBreadcrumb } from '../utils/admin-breadcrumb';

const route = useRoute();
const router = useRouter();
const auth = useAuthStore();
const { t } = useAdminLocale();

const sidebarOpen = ref(false);
const isMobileNav = ref(false);

const adminMenus = computed(() => [
  { path: '/', label: t('nav.dashboard'), matchPrefix: true },
  { path: '/matches', label: t('nav.matches'), matchPrefix: true },
  { path: '/users', label: t('nav.agents_players') },
  { path: '/finance-logs', label: t('nav.finance_logs') },
  { path: '/cashback', label: t('nav.cashback') },
  { path: '/bets', label: t('nav.bets') },
  { path: '/contents', label: t('nav.contents') },
  { path: '/media', label: t('nav.media') },
  { path: '/audit', label: t('nav.audit') },
  { path: '/smoke-tests', label: t('nav.smoke_tests') },
]);

const agentMenus = computed(() => [
  { path: '/', label: t('nav.dashboard') },
  { path: '/my-players', label: t('nav.agents_players') },
  { path: '/finance-logs', label: t('nav.finance_logs') },
  { path: '/my-bets', label: t('nav.myBets') },
]);

const menus = computed(() => (auth.isAdmin.value ? adminMenus.value : agentMenus.value));

function isMatchesSectionPath(path: string) {
  return (
    path === '/matches' ||
    path.startsWith('/matches/') ||
    path.startsWith('/outrights/') ||
    path.startsWith('/settlement/')
  );
}

function isDashboardSectionPath(path: string) {
  return path === '/' || path === '/dashboard/players';
}

const currentLabel = computed(() => {
  const hit = menus.value.find((m) => {
    if ('matchPrefix' in m && m.matchPrefix) {
      if (m.path === '/') return isDashboardSectionPath(route.path);
      return isMatchesSectionPath(route.path);
    }
    return route.path === m.path;
  });
  return hit?.label ?? '';
});

const topbarCrumbs = computed(() => resolveAdminBreadcrumb(route.path, t));

const roleLabel = computed(() => {
  if (auth.isAdmin.value) return t('role.admin');
  const level = auth.user.value?.agentLevel;
  if (auth.isAgent.value && level != null && level > 0) {
    return t('role.agent_level', { n: level });
  }
  return t('role.agent');
});

const currentUser = computed(() => auth.user.value);
const isAdminPortal = computed(() => auth.isAdmin.value);

const userInitial = computed(() =>
  (currentUser.value?.username ?? '').charAt(0).toUpperCase()
);

function syncMobileNav() {
  isMobileNav.value = window.matchMedia('(max-width: 1023px)').matches;
  if (!isMobileNav.value) {
    sidebarOpen.value = false;
  }
}

function toggleSidebar() {
  sidebarOpen.value = !sidebarOpen.value;
}

function closeSidebar() {
  sidebarOpen.value = false;
}

function onNavClick() {
  if (isMobileNav.value) {
    closeSidebar();
  }
}

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

onMounted(() => {
  syncMobileNav();
  window.addEventListener('resize', syncMobileNav);
});

onUnmounted(() => {
  window.removeEventListener('resize', syncMobileNav);
});

watch(() => route.path, () => {
  if (isMobileNav.value) {
    closeSidebar();
  }
});
</script>

<template>
  <div class="shell" :class="{ 'shell--nav-open': sidebarOpen && isMobileNav }">
    <button
      v-if="isMobileNav && sidebarOpen"
      type="button"
      class="sidebar-backdrop"
      :aria-label="t('nav.close_menu')"
      @click="closeSidebar"
    />

    <!-- ── Sidebar ── -->
    <aside class="sidebar" :class="{ open: sidebarOpen }">
      <div class="brand">
        <img src="/logo.png" alt="TheBet365" class="brand-logo" />
      </div>

      <nav class="nav">
        <RouterLink
          v-for="m in menus" :key="m.path" :to="m.path"
          class="nav-item"
          :class="{
            active:
              route.path === m.path ||
              ('matchPrefix' in m &&
                m.matchPrefix &&
                (m.path === '/'
                  ? isDashboardSectionPath(route.path)
                  : isMatchesSectionPath(route.path))),
          }"
          @click="onNavClick"
        >
          {{ m.label }}
        </RouterLink>
      </nav>

      <div class="sidebar-foot">TheBet365 &copy; 2026</div>
    </aside>

    <!-- ── Main ── -->
    <div class="main">
      <header class="topbar">
        <div class="topbar-left">
          <button
            v-if="isMobileNav"
            type="button"
            class="btn-menu"
            :aria-label="sidebarOpen ? t('nav.close_menu') : t('nav.open_menu')"
            @click="toggleSidebar"
          >
            <span class="menu-icon" aria-hidden="true" />
          </button>
          <div class="topbar-title">
            <span v-if="!topbarCrumbs" class="topbar-accent" />
            <nav v-if="topbarCrumbs" class="topbar-crumbs" aria-label="Breadcrumb">
              <template v-for="(item, i) in topbarCrumbs" :key="`${item.label}-${i}`">
                <RouterLink
                  v-if="item.to && i < topbarCrumbs.length - 1"
                  :to="item.to"
                  class="crumb-link"
                >
                  {{ item.label }}
                </RouterLink>
                <span v-else class="crumb-current">{{ item.label }}</span>
                <span v-if="i < topbarCrumbs.length - 1" class="crumb-sep" aria-hidden="true">/</span>
              </template>
            </nav>
            <span v-else class="topbar-page-label">{{ currentLabel }}</span>
          </div>
          <div id="topbar-page-actions" class="topbar-page-actions" />
        </div>
        <div class="topbar-right">
          <div class="user-chip">
            <div class="avatar">{{ userInitial }}</div>
            <div class="user-info">
              <span class="user-name">{{ currentUser?.username }}</span>
              <span class="user-role">{{ roleLabel }}</span>
            </div>
          </div>
          <AdminLocaleSwitcher />
          <div class="portal-tag">{{ isAdminPortal ? t('portal.admin') : t('portal.agent') }}</div>
          <button class="btn-logout" @click="logout">{{ t('logout') }}</button>
        </div>
      </header>
      <main class="page-main">
        <RouterView />
      </main>
    </div>
  </div>
</template>

<style scoped>
.shell {
  --sidebar-width: 176px;
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  background: rgba(6, 6, 6, 0.98);
  border-right: 1px solid #1c1c1c;
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform 0.2s ease;
}

.brand {
  height: 56px;
  min-height: 56px;
  padding: 0 10px;
  border-bottom: 1px solid #1a1a1a;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-sizing: border-box;
}
.brand-logo {
  max-width: 118px;
  max-height: 34px;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
}

.nav {
  flex: 1;
  padding: 8px 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 9px 10px;
  border-radius: 6px;
  color: #aaa;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s;
  border-left: 2px solid transparent;
  letter-spacing: 0.02em;
}
.nav-item:hover {
  background: rgba(255, 255, 255, 0.05);
  color: #fff;
}
.nav-item.active {
  background: linear-gradient(90deg, rgba(36, 143, 84, 0.22), rgba(36, 143, 84, 0.04));
  color: var(--green-text);
  font-weight: 700;
  border-left-color: var(--green-bright);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.sidebar-foot {
  padding: 10px 10px;
  font-size: 10px;
  color: #282828;
  border-top: 1px solid #161616;
  letter-spacing: 0.04em;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 90;
  border: none;
  padding: 0;
  margin: 0;
  background: rgba(0, 0, 0, 0.55);
  cursor: pointer;
}

/* ── Main ── */
.main {
  margin-left: var(--sidebar-width);
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.topbar {
  position: sticky; top: 0; z-index: 80;
  height: 56px;
  min-height: 56px;
  box-sizing: border-box;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 28px;
  background: rgba(6, 6, 6, 0.98);
  border-bottom: 1px solid #1a1a1a;
  backdrop-filter: blur(12px);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.btn-menu {
  display: none;
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.03);
  color: #ccc;
  transition: border-color 0.15s, background 0.15s;
}
.btn-menu:hover {
  border-color: #444;
  background: rgba(255, 255, 255, 0.06);
}
.menu-icon {
  display: block;
  width: 16px;
  height: 2px;
  background: currentColor;
  border-radius: 1px;
  box-shadow: 0 -5px 0 currentColor, 0 5px 0 currentColor;
}

.topbar-title {
  display: flex; align-items: center; gap: 12px;
  font-size: 15px; font-weight: 700;
  color: #e8e8e8;
  letter-spacing: 0.04em;
  min-width: 0;
}
.topbar-page-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-crumbs {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px 8px;
  font-size: 14px;
  font-weight: 600;
  min-width: 0;
}
.crumb-link {
  color: var(--green-text);
  transition: color 0.15s;
}
.crumb-link:hover {
  color: #d4fde5;
}
.crumb-current {
  color: #e8e8e8;
}
.crumb-sep {
  color: #444;
  font-weight: 400;
  user-select: none;
}
.topbar-accent {
  width: 3px; height: 16px;
  background: linear-gradient(180deg, var(--green-glow), var(--green-deep));
  border-radius: 2px;
  flex-shrink: 0;
  box-shadow: 0 0 8px rgba(47, 181, 106, 0.45);
}

.topbar-page-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  margin-left: 8px;
}
.topbar-page-actions:empty {
  display: none;
}

.topbar-right {
  display: flex; align-items: center; gap: 12px;
  flex-shrink: 0;
}

.user-chip {
  display: flex; align-items: center; gap: 8px;
}
.avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--primary-grad);
  border: 1px solid var(--green-border);
  box-shadow: var(--primary-shadow);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 800; color: #fff;
  flex-shrink: 0;
}
.user-info {
  display: flex; flex-direction: column; gap: 1px;
}
.user-name {
  font-size: 13px; font-weight: 600; color: #e0e0e0;
  line-height: 1;
}
.user-role {
  font-size: 10px; color: #555;
  line-height: 1;
}

.portal-tag {
  padding: 3px 8px;
  border: 1px solid var(--green-border);
  border-radius: 4px;
  font-size: 11px;
  color: var(--green-text);
  background: var(--green-surface);
  font-weight: 600;
  letter-spacing: 0.04em;
}

.btn-logout {
  padding: 5px 14px;
  background: transparent;
  border: 1px solid #2a2a2a;
  border-radius: 6px;
  color: #888;
  font-size: 12px;
  font-family: inherit;
  transition: all 0.15s;
}
.btn-logout:hover { border-color: #444; color: #ccc; }

.page-main {
  flex: 1;
  min-height: 0;
  padding: 20px 28px 28px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.page-main > * {
  flex: 1;
  min-height: 0;
}

@media (max-width: 1023px) {
  .sidebar {
    transform: translateX(-100%);
    box-shadow: none;
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.45);
  }
  .main {
    margin-left: 0;
  }
  .btn-menu {
    display: inline-flex;
  }
  .page-main {
    padding: 16px;
  }
  .topbar {
    padding: 0 14px;
  }
  .user-info,
  .portal-tag {
    display: none;
  }
}

@media (max-width: 640px) {
  .topbar-right {
    gap: 8px;
  }
}
</style>
