<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Admin — Dealspace</title>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <script src="https://cdn.tailwindcss.com"></script>
  <style>* { font-family: 'Inter', sans-serif; } body { background: #0a1628; }
    .sidebar-link.active { background: rgba(201,168,76,0.1); color: #c9a84c; border-left: 3px solid #c9a84c; }
    .sidebar-link:hover:not(.active) { background: rgba(255,255,255,0.04); }
  </style>
</head>
<body>
  <header class="bg-[#0d1f3c] border-b border-white/[0.08] px-6 py-3 flex items-center justify-between sticky top-0 z-50">
    <a href="/app/tasks" class="text-xl font-bold text-white tracking-tight"><span class="text-[#c9a84c]">Deal</span>space</a>
    <div class="flex items-center gap-4">
      <span class="text-xs px-2 py-0.5 bg-[#c9a84c]/20 text-[#c9a84c] rounded-full font-medium">Super Admin</span>
      <span id="userName" class="text-sm text-[#b0bec5]"></span>
      <button onclick="logout()" class="text-sm text-[#b0bec5] hover:text-white transition">Logout</button>
    </div>
  </header>
  <div class="flex">
    <nav class="w-56 bg-[#0d1f3c] border-r border-white/[0.08] min-h-[calc(100vh-49px)] sticky top-[49px] shrink-0">
      <div class="p-3 space-y-0.5">
        <a href="/app/tasks" class="sidebar-link flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-[#b0bec5] transition">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/></svg>
          App Home</a>
        <a href="/admin" class="sidebar-link active flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-white transition">
          <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.066 2.573c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.573 1.066c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.066-2.573c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
          Dashboard</a>
      </div>
    </nav>
    <main class="flex-1 p-8 max-w-6xl">
      <h1 class="text-2xl font-bold text-white mb-2">Admin Dashboard</h1>
      <p class="text-[#b0bec5] text-sm mb-8">Platform overview and management.</p>

      <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-8">
        <div class="bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-5">
          <div class="text-[#b0bec5] text-xs uppercase tracking-wider mb-1">Users</div>
          <div id="statUsers" class="text-3xl font-bold text-white">—</div>
        </div>
        <div class="bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-5">
          <div class="text-[#b0bec5] text-xs uppercase tracking-wider mb-1">Projects</div>
          <div id="statProjects" class="text-3xl font-bold text-white">—</div>
        </div>
        <div class="bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-5">
          <div class="text-[#b0bec5] text-xs uppercase tracking-wider mb-1">Organizations</div>
          <div id="statOrgs" class="text-3xl font-bold text-white">—</div>
        </div>
        <div class="bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-5">
          <div class="text-[#b0bec5] text-xs uppercase tracking-wider mb-1">Active Sessions</div>
          <div id="statSessions" class="text-3xl font-bold text-white">—</div>
        </div>
      </div>

      <div class="bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-6">
        <div class="flex items-center justify-between mb-4">
          <h2 class="text-sm font-semibold text-white">All Users</h2>
        </div>
        <div id="userList" class="space-y-2"><div class="text-[#b0bec5] text-sm">Loading...</div></div>
      </div>
    </main>
  </div>

  <script>
    const token = localStorage.getItem('ds_token');
    if (!token) window.location.href = '/app/login';
    const user = JSON.parse(localStorage.getItem('ds_user') || '{}');
    if (!user.is_super_admin) window.location.href = '/app/tasks';
    document.getElementById('userName').textContent = user.name || user.email || '';

    function fetchAPI(path, opts = {}) {
      opts.headers = { ...opts.headers, 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' };
      return fetch(path, opts).then(r => { if (r.status === 401) { localStorage.removeItem('ds_token'); window.location.href = '/app/login'; } return r; });
    }
    function logout() { fetchAPI('/api/auth/logout', { method: 'POST' }).finally(() => { localStorage.clear(); window.location.href = '/app/login'; }); }
    function escHtml(s) { const d = document.createElement('div'); d.textContent = s; return d.innerHTML; }

    async function loadStats() {
      try {
        const [usersRes, projectsRes, orgsRes] = await Promise.all([
          fetchAPI('/api/admin/users'), fetchAPI('/api/projects'), fetchAPI('/api/orgs')
        ]);
        const users = await usersRes.json();
        const projects = await projectsRes.json();
        const orgs = await orgsRes.json();
        document.getElementById('statUsers').textContent = Array.isArray(users) ? users.length : '?';
        document.getElementById('statProjects').textContent = Array.isArray(projects) ? projects.length : '?';
        document.getElementById('statOrgs').textContent = Array.isArray(orgs) ? orgs.length : '?';
        document.getElementById('statSessions').textContent = '—';

        if (Array.isArray(users) && users.length > 0) {
          document.getElementById('userList').innerHTML = users.map(u => `
            <div class="flex items-center gap-4 px-4 py-3 rounded-lg bg-[#0a1628] border border-white/[0.05]">
              <div class="w-8 h-8 rounded-full bg-[#c9a84c]/20 flex items-center justify-center text-[#c9a84c] text-sm font-semibold">${(u.name||u.email||'?')[0].toUpperCase()}</div>
              <div class="flex-1">
                <div class="text-white text-sm font-medium">${escHtml(u.name || u.email)}</div>
                ${u.name ? `<div class="text-[#8899a6] text-xs">${escHtml(u.email)}</div>` : ''}
              </div>
              ${u.is_super_admin ? '<span class="text-xs px-2 py-0.5 bg-[#c9a84c]/20 text-[#c9a84c] rounded-full">super admin</span>' : ''}
              <span class="text-xs text-[#8899a6]">${new Date(u.created_at).toLocaleDateString()}</span>
            </div>`).join('');
        } else {
          document.getElementById('userList').innerHTML = '<div class="text-[#b0bec5] text-sm">No users found.</div>';
        }
      } catch(e) {}
    }
    loadStats();
  </script>
</body>
</html>
