<?php
/**
 * SonicPesa - Database Domain Update Utility
 * Run this once after migrating database to update system domains.
 */

require_once 'config.php';
require_once 'includes/db.php';

echo "<h1>SonicPesa Domain Updater</h1>";

$new_domain = 'thathendwa.us';

try {
    // 1. Update system_root_domains
    $stmt = $pdo->prepare("SELECT id FROM system_root_domains WHERE domain_name = ?");
    $stmt->execute([$new_domain]);
    if (!$stmt->fetch()) {
        $pdo->prepare("INSERT INTO system_root_domains (domain_name, is_active) VALUES (?, 1)")
            ->execute([$new_domain]);
        echo "<p>✅ Added '$new_domain' to system_root_domains.</p>";
    } else {
        echo "<p>ℹ️ '$new_domain' already exists in system_root_domains.</p>";
    }

    // 2. Set as default domain if table is empty or has old content
    $count = $pdo->query("SELECT COUNT(*) FROM system_root_domains")->fetchColumn();
    echo "<p>Total domains registered: $count</p>";

    // 3. Clear old domains (Optional - keep if you want multi-domain support)
    // $pdo->exec("DELETE FROM system_root_domains WHERE domain_name != '$new_domain'");
    
    echo "<p><strong>Update complete!</strong> You can now delete this file.</p>";
    echo "<a href='dashboard'>Go to Dashboard</a>";

} catch (PDOException $e) {
    echo "<p style='color:red;'>Error: " . $e->getMessage() . "</p>";
}
?>

