<?php
require_once __DIR__ . '/api/lib.php';

// Check if already logged in
$authUser = hw_get_auth_user();
if ($authUser && in_array('superadmin', $authUser['roles'] ?? [])) {
    header('Location: /atasteofafricanmarketplace/superadmin/dashboard.php');
    exit;
}

$error = '';
if ($_POST) {
    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if ($email && $password) {
        $db = hw_db();
        $stmt = $db->prepare('SELECT u.*, GROUP_CONCAT(r.name) as roles FROM users u LEFT JOIN user_roles ur ON u.id = ur.user_id LEFT JOIN roles r ON ur.role_id = r.id WHERE u.email = ? GROUP BY u.id');
        $stmt->execute([$email]);
        $user = $stmt->fetch();
        
        if ($user && hw_verify_password($password, $user['password_hash'])) {
            $roles = explode(',', $user['roles'] ?? '');
            if (in_array('superadmin', $roles)) {
                $token = hw_jwt_encode(['sub' => $user['id'], 'email' => $user['email'], 'roles' => $roles]);
                hw_set_auth_cookie($token);
                header('Location: /atasteofafricanmarketplace/superadmin/dashboard.php');
                exit;
            } else {
                $error = 'Access denied. Superadmin privileges required.';
            }
        } else {
            $error = 'Invalid credentials';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Superadmin Login | Herbal Wellness</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center">
    <div class="bg-white p-8 rounded-lg shadow-md w-full max-w-md">
        <div class="text-center mb-8">
            <h1 class="text-2xl font-bold text-gray-900">Superadmin Login</h1>
            <p class="text-gray-600 mt-2">Website Management Portal</p>
        </div>
        
        <?php if ($error): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
                <?= htmlspecialchars($error) ?>
            </div>
        <?php endif; ?>
        
        <form method="POST" class="space-y-6">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Email</label>
                <input type="email" name="email" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald-500">
            </div>
            
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Password</label>
                <input type="password" name="password" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-emerald-500">
            </div>
            
            <button type="submit" class="w-full bg-emerald-600 text-white py-2 px-4 rounded-md hover:bg-emerald-700 focus:outline-none focus:ring-2 focus:ring-emerald-500">
                Login
            </button>
        </form>
        
        <div class="mt-6 text-center">
            <a href="/atasteofafricanmarketplace/index.php" class="text-emerald-600 hover:text-emerald-700 text-sm">← Back to Website</a>
        </div>
    </div>
</body>
</html>