<?php
/**
 * One-time helper: apply the new ORANGE (Workwear-style light) admin theme
 * to THIS installation's database.
 *
 * Open in a browser:  http://localhost/shopping_cart_fixed_latest/apply_orange_admin_theme.php
 *
 * It runs app/config/updates/update_2026_06_03_00_00_00.sql, which only
 * overwrites admin colors that are still at a previous default — any colors
 * you customised yourself are left untouched. Safe to run more than once.
 *
 * DELETE this file after you have run it once.
 */

error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
header('Content-Type: text/html; charset=utf-8');

$configFile = __DIR__ . '/app/config/config.inc.php';
$sqlFile    = __DIR__ . '/app/config/updates/update_2026_06_03_00_00_00.sql';

echo "<!doctype html><html><head><meta charset='utf-8'><title>Apply orange admin theme</title>";
echo "<style>body{font-family:Arial,Helvetica,sans-serif;max-width:760px;margin:40px auto;color:#0f172a}"
   . ".ok{color:#10b981}.err{color:#ef4444}.box{background:#f8fafc;border:1px solid #e2e8f0;border-radius:8px;padding:16px 20px}"
   . "code{background:#fff7ed;padding:2px 6px;border-radius:4px}</style></head><body>";
echo "<h2>Apply orange admin theme</h2><div class='box'>";

if (!is_file($configFile)) { exit("<p class='err'>Could not find app/config/config.inc.php</p></div></body></html>"); }
if (!is_file($sqlFile))    { exit("<p class='err'>Could not find the migration SQL file.</p></div></body></html>"); }

require_once $configFile;

$host   = defined('PJ_HOST')   ? PJ_HOST   : 'localhost';
$user   = defined('PJ_USER')   ? PJ_USER   : 'root';
$pass   = defined('PJ_PASS')   ? PJ_PASS   : '';
$dbname = defined('PJ_DB')     ? PJ_DB     : '';
$prefix = defined('PJ_PREFIX') ? PJ_PREFIX : '';

$mysqli = @new mysqli($host, $user, $pass, $dbname);
if ($mysqli->connect_errno) {
    exit("<p class='err'>DB connection failed: " . htmlspecialchars($mysqli->connect_error) . "</p></div></body></html>");
}
$mysqli->set_charset('utf8');

$sql = file_get_contents($sqlFile);
// Honour table prefix if this install uses one.
if ($prefix !== '') {
    $sql = str_replace('`plugin_base_options`', '`' . $prefix . 'plugin_base_options`', $sql);
}

$ok = 0; $fail = 0; $errors = array();
if ($mysqli->multi_query($sql)) {
    do {
        $ok++;
        if ($mysqli->errno) { $fail++; $errors[] = $mysqli->error; }
    } while ($mysqli->more_results() && $mysqli->next_result());
}
if ($mysqli->errno) { $fail++; $errors[] = $mysqli->error; }

if ($fail === 0) {
    echo "<p class='ok'><strong>Done.</strong> The orange admin theme is now the default for this installation.</p>";
    echo "<p>Open the admin panel and hard-refresh (Cmd/Ctrl + Shift + R). To fine-tune, go to "
       . "<code>Settings &rarr; Visual</code> &mdash; all preset palettes still work.</p>";
    echo "<p>You can now delete <code>apply_orange_admin_theme.php</code>.</p>";
} else {
    echo "<p class='err'>Finished with " . (int)$fail . " error(s):</p><ul>";
    foreach (array_unique($errors) as $e) { echo "<li>" . htmlspecialchars($e) . "</li>"; }
    echo "</ul>";
}

$mysqli->close();
echo "</div></body></html>";
