<?php
/**
 * Setup Super Admin User
 * Run this file to check or create the superadmin user
 */

require_once __DIR__ . '/config/app.php';
require_once __DIR__ . '/core/Database.php';
require_once __DIR__ . '/core/Security.php';

echo "=== Super Admin Setup ===\n\n";

// Check if superadmin user exists
$stmt = db()->prepare("SELECT id, name, email, is_active FROM superadmins WHERE email = ?");
$stmt->execute(['superadmin@tellerpro.com']);
$admin = $stmt->fetch();

if ($admin) {
    echo "Superadmin user found:\n";
    echo "ID: " . $admin['id'] . "\n";
    echo "Name: " . $admin['name'] . "\n";
    echo "Email: " . $admin['email'] . "\n";
    echo "Active: " . ($admin['is_active'] ? 'Yes' : 'No') . "\n\n";
    
    if (!$admin['is_active']) {
        echo "Activating superadmin user...\n";
        $stmt = db()->prepare("UPDATE superadmins SET is_active = 1 WHERE id = ?");
        $stmt->execute([$admin['id']]);
        echo "Superadmin user activated.\n\n";
    }
} else {
    echo "Superadmin user not found. Creating...\n";
    
    $email = 'superadmin@tellerpro.com';
    $password = 'SuperAdmin@123';
    $name = 'Platform Admin';
    $hash = Security::hashPassword($password);
    
    $stmt = db()->prepare("INSERT INTO superadmins (name, email, password, is_active) VALUES (?, ?, ?, 1)");
    $stmt->execute([$name, $email, $hash]);
    
    echo "Superadmin user created successfully.\n";
    echo "Email: $email\n";
    echo "Password: $password\n\n";
}

echo "=== Setup Complete ===\n";
echo "You can now login at: " . APP_URL . "/superadmin/login\n";
echo "Email: superadmin@tellerpro.com\n";
echo "Password: SuperAdmin@123\n";
