<?php
/**
 * Create or Reset Superadmin Account - Browser Version
 * 
 * This script creates a new superadmin account or resets an existing one.
 * Access via browser at /create_superadmin_browser.php
 */

require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/core/Security.php';

$message = '';
$messageType = '';
$credentials = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        $db = db();
        
        // Default credentials
        $defaultName = 'Platform Admin';
        $defaultEmail = 'superadmin@tellerpro.com';
        $defaultPassword = 'SuperAdmin@123';
        
        // Check if superadmin already exists
        $stmt = $db->prepare("SELECT * FROM superadmins WHERE email = ? LIMIT 1");
        $stmt->execute([$defaultEmail]);
        $existing = $stmt->fetch();
        
        if ($existing) {
            // Update password
            $hashedPassword = Security::hashPassword($defaultPassword);
            $stmt = $db->prepare("UPDATE superadmins SET password = ?, is_active = 1 WHERE email = ?");
            $stmt->execute([$hashedPassword, $defaultEmail]);
            
            $message = 'Superadmin password updated successfully.';
            $messageType = 'success';
        } else {
            // Create new superadmin
            $hashedPassword = Security::hashPassword($defaultPassword);
            $stmt = $db->prepare("INSERT INTO superadmins (name, email, password, is_active) VALUES (?, ?, ?, 1)");
            $stmt->execute([$defaultName, $defaultEmail, $hashedPassword]);
            
            $message = 'Superadmin account created successfully.';
            $messageType = 'success';
        }
        
        $credentials = [
            'email' => $defaultEmail,
            'password' => $defaultPassword,
            'login_url' => APP_URL . '/superadmin/login'
        ];
        
    } catch (PDOException $e) {
        $message = 'Database error: ' . $e->getMessage();
        $messageType = 'error';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Superadmin - TellerPro</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        
        .container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            max-width: 500px;
            width: 100%;
            padding: 40px;
        }
        
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .header h1 {
            color: #333;
            font-size: 24px;
            margin-bottom: 10px;
        }
        
        .header .subtitle {
            color: #666;
            font-size: 14px;
        }
        
        .alert {
            padding: 16px 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        
        .alert-success {
            background: #d4edda;
            border: 1px solid #28a745;
            color: #155724;
        }
        
        .alert-error {
            background: #f8d7da;
            border: 1px solid #dc3545;
            color: #721c24;
        }
        
        .credentials-box {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
        }
        
        .credentials-box h3 {
            color: #333;
            font-size: 16px;
            margin-bottom: 15px;
        }
        
        .credential-item {
            margin-bottom: 12px;
        }
        
        .credential-item label {
            display: block;
            color: #666;
            font-size: 12px;
            margin-bottom: 4px;
        }
        
        .credential-item .value {
            color: #333;
            font-size: 14px;
            font-weight: 600;
            font-family: 'Courier New', monospace;
        }
        
        .btn {
            width: 100%;
            padding: 14px 24px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            text-decoration: none;
            display: block;
            text-align: center;
        }
        
        .btn-primary {
            background: #667eea;
            color: white;
        }
        
        .btn-primary:hover {
            background: #5568d3;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        }
        
        .btn-secondary {
            background: #6c757d;
            color: white;
            margin-top: 10px;
        }
        
        .btn-secondary:hover {
            background: #5a6268;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>👤 Superadmin Account</h1>
            <p class="subtitle">Create or Reset Superadmin Login</p>
        </div>
        
        <?php if ($message): ?>
            <div class="alert alert-<?php echo $messageType; ?>">
                <?php echo htmlspecialchars($message); ?>
            </div>
        <?php endif; ?>
        
        <?php if ($credentials): ?>
            <div class="credentials-box">
                <h3>🔑 Login Credentials</h3>
                <div class="credential-item">
                    <label>Email:</label>
                    <div class="value"><?php echo htmlspecialchars($credentials['email']); ?></div>
                </div>
                <div class="credential-item">
                    <label>Password:</label>
                    <div class="value"><?php echo htmlspecialchars($credentials['password']); ?></div>
                </div>
                <div class="credential-item">
                    <label>Login URL:</label>
                    <div class="value"><?php echo htmlspecialchars($credentials['login_url']); ?></div>
                </div>
            </div>
            
            <a href="<?php echo $credentials['login_url']; ?>" class="btn btn-primary">
                Go to Login Page
            </a>
        <?php else: ?>
            <form method="POST">
                <p style="color: #666; margin-bottom: 20px; font-size: 14px;">
                    This will create or reset the superadmin account with default credentials.
                </p>
                
                <button type="submit" class="btn btn-primary">
                    Create/Reset Superadmin
                </button>
            </form>
        <?php endif; ?>
    </div>
</body>
</html>
