---
import Layout from '../layouts/Layout.astro';
import { verifyAuthSimple } from '../lib/simple-auth';

export const prerender = false;

let auth = null;
let authError = null;

try {
  auth = await verifyAuthSimple(Astro.request);
} catch (error) {
  authError = error.message;
}
---

<Layout title="Auth Test">
  <div class="min-h-screen bg-gray-100 p-8">
    <div class="max-w-4xl mx-auto">
      <h1 class="text-3xl font-bold mb-8">Authentication Test</h1>
      
      <div class="bg-white rounded-lg shadow-lg p-6 mb-6">
        <h2 class="text-xl font-semibold mb-4">Authentication Status</h2>
        
        {auth ? (
          <div class="space-y-4">
            <div class="p-4 bg-green-100 border border-green-400 rounded">
              <h3 class="font-semibold text-green-800">✅ Authenticated</h3>
              <p><strong>User ID:</strong> {auth.user.id}</p>
              <p><strong>Email:</strong> {auth.user.email}</p>
              <p><strong>Is Admin:</strong> {auth.isAdmin ? 'Yes' : 'No'}</p>
              <p><strong>Organization ID:</strong> {auth.organizationId || 'None'}</p>
            </div>
            
            {auth.isAdmin ? (
              <div class="p-4 bg-blue-100 border border-blue-400 rounded">
                <h3 class="font-semibold text-blue-800">🔑 Admin Access Granted</h3>
                <p>You should be able to access admin dashboards.</p>
                <div class="mt-4 space-x-4">
                  <a href="/admin/dashboard" class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">Admin Dashboard</a>
                  <a href="/admin/super-dashboard" class="bg-purple-500 text-white px-4 py-2 rounded hover:bg-purple-600">Super Dashboard</a>
                </div>
              </div>
            ) : (
              <div class="p-4 bg-yellow-100 border border-yellow-400 rounded">
                <h3 class="font-semibold text-yellow-800">⚠️ Not an Admin</h3>
                <p>Your account does not have admin privileges. You can only access the regular dashboard.</p>
                <div class="mt-4">
                  <a href="/dashboard" class="bg-green-500 text-white px-4 py-2 rounded hover:bg-green-600">User Dashboard</a>
                </div>
              </div>
            )}
          </div>
        ) : (
          <div class="space-y-4">
            <div class="p-4 bg-red-100 border border-red-400 rounded">
              <h3 class="font-semibold text-red-800">❌ Not Authenticated</h3>
              <p>You are not logged in or your session has expired.</p>
              {authError && <p><strong>Error:</strong> {authError}</p>}
            </div>
            
            <div class="mt-4">
              <a href="/login" class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600">Login</a>
            </div>
          </div>
        )}
      </div>
      
      <div class="bg-white rounded-lg shadow-lg p-6">
        <h2 class="text-xl font-semibold mb-4">Quick Actions</h2>
        <div class="space-y-2">
          <p><a href="/dashboard" class="text-blue-600 hover:underline">→ User Dashboard</a></p>
          <p><a href="/admin/dashboard" class="text-blue-600 hover:underline">→ Admin Dashboard (requires admin)</a></p>
          <p><a href="/admin/super-dashboard" class="text-blue-600 hover:underline">→ Super Dashboard (requires admin)</a></p>
          <p><a href="/login" class="text-blue-600 hover:underline">→ Login Page</a></p>
        </div>
      </div>
    </div>
  </div>
</Layout>