<?php
define('ADMIN_ACCESS', true);
require_once 'config.php';

echo "<h3>Create Separate Sub-Admin for Localhost</h3>";

$db = new Database();
$pdo = $db->getConnection();

// Create sub-admin for localhost (site ID 1)
$localhostUsername = 'admin_localhost';
$localhostPassword = 'password123';
$localhostEmail = 'admin@localhost.com';
$localhostFullName = 'Admin Localhost';
$localhostSiteId = 1; // MAIN site

// Create sub-admin for newsite.localhost (site ID 8) 
$newsiteUsername = 'admin_newsite';
$newsitePassword = 'password123';
$newsiteEmail = 'admin@newsite.localhost';
$newsiteFullName = 'Admin New Site';
$newsiteSiteId = 8; // NEW SITE

echo "<h4>Creating Sub-Admins:</h4>";
echo "<pre>";

try {
    // Hash passwords
    $localhostHashedPassword = password_hash($localhostPassword, PASSWORD_DEFAULT);
    $newsiteHashedPassword = password_hash($newsitePassword, PASSWORD_DEFAULT);
    
    // Create localhost admin
    $stmt1 = $pdo->prepare("
        INSERT INTO site_admins (username, email, full_name, password, site_id, created_by, created_at, is_active) 
        VALUES (?, ?, ?, ?, ?, 1, NOW(), 1)
        ON DUPLICATE KEY UPDATE password = VALUES(password), site_id = VALUES(site_id)
    ");
    $result1 = $stmt1->execute([$localhostUsername, $localhostEmail, $localhostFullName, $localhostHashedPassword, $localhostSiteId]);
    
    if ($result1) {
        echo "✅ Created/Updated admin for localhost:\n";
        echo "   Username: $localhostUsername\n";
        echo "   Password: $localhostPassword\n";
        echo "   Site ID: $localhostSiteId (for localhost)\n";
        echo "   Login at: http://localhost/autoprediksi/admin/login.php\n\n";
    }
    
    // Create newsite admin
    $stmt2 = $pdo->prepare("
        INSERT INTO site_admins (username, email, full_name, password, site_id, created_by, created_at, is_active) 
        VALUES (?, ?, ?, ?, ?, 1, NOW(), 1)
        ON DUPLICATE KEY UPDATE password = VALUES(password), site_id = VALUES(site_id)
    ");
    $result2 = $stmt2->execute([$newsiteUsername, $newsiteEmail, $newsiteFullName, $newsiteHashedPassword, $newsiteSiteId]);
    
    if ($result2) {
        echo "✅ Created/Updated admin for newsite.localhost:\n";
        echo "   Username: $newsiteUsername\n";
        echo "   Password: $newsitePassword\n";
        echo "   Site ID: $newsiteSiteId (for newsite.localhost)\n";
        echo "   Login at: http://newsite.localhost/autoprediksi/admin/login.php\n\n";
    }
    
    // Also update existing uji2 to work with newsite.localhost
    $stmt3 = $pdo->prepare("UPDATE site_admins SET site_id = ? WHERE username = 'uji2'");
    $result3 = $stmt3->execute([$newsiteSiteId]);
    
    if ($result3) {
        echo "✅ Updated existing uji2 for newsite.localhost:\n";
        echo "   Username: uji2\n";
        echo "   Password: password123\n";
        echo "   Site ID: $newsiteSiteId (for newsite.localhost)\n";
        echo "   Login at: http://newsite.localhost/autoprediksi/admin/login.php\n\n";
    }
    
} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
}

echo "</pre>";

echo "<h4>Summary of Sub-Admins:</h4>";
echo "<table border='1' cellpadding='5' style='border-collapse: collapse;'>";
echo "<tr><th>Username</th><th>Password</th><th>Domain</th><th>Login URL</th></tr>";
echo "<tr><td>admin_localhost</td><td>password123</td><td>localhost</td><td><a href='http://localhost/autoprediksi/admin/login.php'>Login</a></td></tr>";
echo "<tr><td>admin_newsite</td><td>password123</td><td>newsite.localhost</td><td><a href='http://newsite.localhost/autoprediksi/admin/login.php'>Login</a></td></tr>";
echo "<tr><td>uji2</td><td>password123</td><td>newsite.localhost</td><td><a href='http://newsite.localhost/autoprediksi/admin/login.php'>Login</a></td></tr>";
echo "</table>";

?>