<script setup lang="ts">
import { computed } from "vue";
import { RouterLink, RouterView, useRoute } from "vue-router";

import { useDesktopRuntime } from "../composables/useDesktopRuntime";
import { useDesktopSession } from "../composables/useDesktopSession";
import { desktopMonitoringMediaCatalog, desktopPublishMediaCatalog } from "../lib/media-catalog";

const route = useRoute();
const { snapshot, error, refreshAccounts } = useDesktopRuntime();
const { session, logout } = useDesktopSession();

const accountAwareRoutes = new Set(["/media-platforms", "/ai-platforms"]);

function handleNavClick(target: string) {
  if (accountAwareRoutes.has(target)) {
    void refreshAccounts();
  }
}

const navItems = computed(() => {
  const data = snapshot.value;
  const accounts = data?.accounts ?? [];
  const publishIDs = new Set(desktopPublishMediaCatalog.map((item) => item.id));
  const monitoringIDs = new Set(desktopMonitoringMediaCatalog.map((item) => item.id));

  return [
    {
      to: "/",
      title: "控制台",
      description: "运行总览、活动流和总体健康度",
      count: data?.summary.issuesOpen ?? 0,
    },
    {
      to: "/publish-management",
      title: "发布管理",
      description: "查看待发布队列、历史发送结果，并对文章再次发送",
      count: data?.summary.queuedTasks ?? 0,
    },
    {
      to: "/media-platforms",
      title: "媒体平台",
      description: "发布媒体授权、多个账号绑定与表格化管理",
      count: accounts.filter((item) => publishIDs.has(item.platform)).length,
    },
    {
      to: "/ai-platforms",
      title: "AI 平台",
      description: "AI 平台一平台一绑定，按卡片查看状态与归属",
      count: accounts.filter((item) => monitoringIDs.has(item.platform)).length,
    },
    {
      to: "/diagnostics",
      title: "诊断",
      description: "vault、scheduler、transport 与原始快照",
      count: data?.clients.length ?? 0,
    },
  ];
});


const operatorName = computed(() => session.value?.user?.name ?? "未命名操作员");
const operatorEmail = computed(() => session.value?.user?.email ?? "");
const clientLabel = computed(
  () => session.value?.desktopClient?.device_name ?? "当前设备尚未注册 client",
);
</script>

<template>
  <div class="app-shell">
    <aside class="sidebar">
      <div class="brand-block">
        <div class="brand-mark">
          <span class="brand-ring"></span>
          <span class="brand-core"></span>
        </div>
        <div>
          <p class="eyebrow">Workspace Cockpit</p>
          <h1>GEO Rankly Desktop</h1>
        </div>
      </div>

      <nav class="nav">
        <RouterLink
          v-for="item in navItems"
            :key="item.to"
            :to="item.to"
            class="nav-link"
            :class="{ active: route.path === item.to }"
            @click="handleNavClick(item.to)"
          >
            <div class="nav-copy">
              <strong>{{ item.title }}</strong>
            </div>
            <span class="nav-count">{{ item.count }}</span>
          </RouterLink>
        </nav>

      <footer class="sidebar-footer">
        <section class="user-profile-panel">
          <div class="user-info-row">
            <div class="user-avatar">{{ operatorName.charAt(0).toUpperCase() }}</div>
            <div class="user-details">
              <strong>{{ operatorName }}</strong>
              <p>{{ operatorEmail }}</p>
            </div>
          </div>
          <div class="user-actions-row">
            <span class="device-tag">{{ clientLabel }}</span>
            <button type="button" class="logout-link" @click="logout">退出</button>
          </div>
        </section>


        <div class="footer-row">
          <span>Version</span>
          <strong>{{ snapshot?.app.version ?? "0.1.0" }}</strong>
        </div>
        <p v-if="error" class="error-text">{{ error }}</p>
      </footer>
    </aside>

    <main class="content">
      <RouterView />
    </main>
  </div>
</template>

<style scoped>
.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
  background: #f0f2f5;
  color: #1a1a1a;
}

.sidebar {
  width: 320px;
  height: 100vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  padding: 24px;
  border-right: 1px solid #e6edf5;
  background: #ffffff;
  flex-shrink: 0;
}

.brand-block {
  display: flex;
  gap: 16px;
  align-items: center;
}

.brand-mark {
  position: relative;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
}

.brand-ring,
.brand-core {
  position: absolute;
  border-radius: 50%;
}

.brand-ring {
  inset: 0;
  border: 1px solid #91caff;
  background: #e6f4ff;
}

.brand-core {
  width: 16px;
  height: 16px;
  background: #1677ff;
  box-shadow: 0 2px 6px rgba(22, 119, 255, 0.4);
}

.eyebrow {
  margin: 0 0 4px;
  color: #8c8c8c;
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  color: #1a1a1a;
  line-height: 1.2;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: auto 0;
}

.nav-link {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-radius: 12px;
  text-decoration: none;
  background: #ffffff;
  border: 1px solid #e6edf5;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.nav-link:hover {
  border-color: #91caff;
  box-shadow: 0 4px 12px rgba(22, 119, 255, 0.08);
  transform: translateY(-1px);
}

.nav-link.active {
  background: #f0f7ff;
  border-color: #1677ff;
  box-shadow: 0 4px 12px rgba(22, 119, 255, 0.1);
}

.nav-copy {
  display: grid;
  gap: 4px;
}

.nav-copy strong {
  font-size: 14px;
  font-weight: 600;
  color: #1a1a1a;
}

.nav-link.active .nav-copy strong {
  color: #1677ff;
}

.nav-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 28px;
  padding: 0 8px;
  border-radius: 14px;
  background: #f0f0f0;
  color: #595959;
  font-size: 13px;
  font-weight: 600;
}

.nav-link.active .nav-count {
  background: #1677ff;
  color: #fff;
}

.sidebar-footer {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-top: 16px;
}

.user-profile-panel {
  padding: 14px;
  border-radius: 10px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
}

.user-info-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #e0e7ff;
  color: #4f46e5;
  font-weight: 700;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.user-details {
  min-width: 0;
}

.user-details strong {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: #0f172a;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-details p {
  margin: 2px 0 0;
  font-size: 12px;
  color: #64748b;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-actions-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid #e2e8f0;
}

.device-tag {
  font-size: 11px;
  font-weight: 500;
  color: #475569;
  background: #f1f5f9;
  padding: 4px 8px;
  border-radius: 4px;
  border: 1px solid #e2e8f0;
}

.logout-link {
  font-size: 12px;
  font-weight: 500;
  color: #ef4444;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.2s;
}

.logout-link:hover {
  background: #fef2f2;
}

.footer-row span {
  display: block;
  color: #595959;
  font-weight: 600;
  font-size: 12px;
}

.footer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 12px;
  border-top: 1px solid #f0f0f0;
}

.footer-row strong {
  font-size: 13px;
  color: #1a1a1a;
}

.error-text {
  margin: 0;
  color: #ff4d4f;
  font-size: 13px;
}

.content {
  flex: 1;
  height: 100vh;
  padding: 24px 32px;
  overflow-y: auto;
}

@media (max-width: 1080px) {
  .sidebar {
    width: 260px;
  }
}
</style>
