<?php
require_once __DIR__ . '/includes/config.php';

// Only allow this script to run from command line or direct access
if (php_sapi_name() !== 'cli' && !isset($_GET['direct'])) {
    die('Access denied');
}

// Check if superadmin already exists
$result = $conn->query("SELECT id FROM admin_users WHERE role_id = 1");
$superadminExists = $result->num_rows > 0;


if ($superadminExists) {
    die("Superadmin account already exists.\n");
}

// Create superadmin account
$username = 'superadmin';
$email = 'superadmin@reliablemabati.co.ke';
$password = 'StrongPassword123!'; // Change this to a strong password
$fullName = 'System Superadmin';

// Hash password
$passwordHash = password_hash($password, PASSWORD_DEFAULT);

try {
    $stmt = $conn->prepare("
    INSERT INTO admin_users (username, email, password_hash, full_name, role_id)
    VALUES (?, ?, ?, ?, 1)
");
$stmt->bind_param("ssss", $username, $email, $passwordHash, $fullName);
$stmt->execute();
    
    echo "Superadmin account created successfully!\n";
    echo "Username: $username\n";
    echo "Password: $password\n";
    echo "IMPORTANT: Change this password immediately after login!\n";
    
} catch (PDOException $e) {
    die("Error creating superadmin account: " . $e->getMessage() . "\n");
}