<template>
  <div class="account-layout">
    <SiteHeader />
    <div class="account-shell container">
      <aside class="account-nav" aria-label="منوی حساب کاربری">
        <div class="account-user">
          <div class="avatar" aria-hidden="true">{{ initials }}</div>
          <div>
            <p class="user-name">{{ auth.fullName || 'کاربر' }}</p>
            <p class="user-meta">{{ auth.user?.username }}</p>
          </div>
        </div>
        <nav>
          <RouterLink :to="{ name: 'account' }" class="nav-link">خلاصه</RouterLink>
          <RouterLink :to="{ name: 'my-classes' }" class="nav-link">کلاس‌های من</RouterLink>
          <RouterLink :to="{ name: 'my-certificates' }" class="nav-link">گواهی‌نامه‌ها</RouterLink>
        </nav>
        <button type="button" class="logout-btn" @click="handleLogout">خروج از حساب</button>
      </aside>
      <section class="account-content">
        <RouterView />
      </section>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { RouterLink, RouterView, useRouter } from 'vue-router'
import SiteHeader from '@/components/SiteHeader.vue'
import { useAuthStore } from '@/stores/authStore'

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

const initials = computed(() => {
  const name = auth.user?.name || ''
  const surname = auth.user?.surname || ''
  return `${name.charAt(0)}${surname.charAt(0)}` || 'گ'
})

async function handleLogout() {
  await auth.logout()
  router.push({ name: 'landing' })
}
</script>

<style scoped>
.account-layout {
  min-height: 100vh;
  background:
    radial-gradient(circle at 10% 0%, rgba(232, 163, 23, 0.12), transparent 40%),
    linear-gradient(180deg, var(--canvas) 0%, var(--canvas-deep) 100%);
}

.account-shell {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 1.5rem;
  padding-block: 6.5rem 3rem;
}

.account-nav {
  position: sticky;
  top: 5.5rem;
  align-self: start;
  padding: 1.25rem;
  border-radius: 1.25rem;
  background: var(--chalk);
  border: 1px solid rgba(20, 53, 43, 0.08);
  box-shadow: var(--shadow);
}

.account-user {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1.25rem;
  padding-bottom: 1.1rem;
  border-bottom: 1px solid rgba(20, 53, 43, 0.08);
}

.avatar {
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--ink);
  color: var(--saffron);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.1rem;
}

.user-name {
  font-weight: 700;
}

.user-meta {
  font-size: 0.8rem;
  color: var(--ink-soft);
}

nav {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.nav-link {
  padding: 0.7rem 0.85rem;
  border-radius: 0.75rem;
  font-weight: 600;
  color: var(--ink-soft);
  transition: background 0.2s ease, color 0.2s ease;
}

.nav-link:hover,
.nav-link.router-link-active {
  background: rgba(45, 106, 79, 0.1);
  color: var(--leaf);
}

.logout-btn {
  margin-top: 1.25rem;
  width: 100%;
  border: none;
  background: transparent;
  color: var(--danger);
  font-weight: 700;
  padding: 0.7rem;
  border-radius: 0.75rem;
}

.logout-btn:hover {
  background: rgba(180, 35, 24, 0.08);
}

.account-content {
  min-width: 0;
}

@media (max-width: 860px) {
  .account-shell {
    grid-template-columns: 1fr;
    padding-block: 5.5rem 2rem;
  }

  .account-nav {
    position: static;
  }

  nav {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .logout-btn {
    margin-top: 0.75rem;
  }
}
</style>
