<?php
/**
 * One-time / occasional schema sync for live servers after code updates.
 * Smartsupp: uses existing system_settings.live_chat_provider + live_chat_code
 *            (set provider to "smartsupp" in System Settings — no extra column).
 *
 * Usage: log in to admin, then open this file in the browser.
 *        Remove or protect this file if you do not want it reachable.
 */
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

include __DIR__ . '/check_installation.php';
require_once __DIR__ . '/../config/topup_helpers.php';

if (!isset($_SESSION['adminid'])) {
    header('Location: login.php');
    exit;
}

$log = [];

function addColumnIfMissing(mysqli $con, string $table, string $column, string $alterSql): void
{
    global $log;
    $safeCol = preg_replace('/[^a-zA-Z0-9_]/', '', $column);
    $res = $con->query("SHOW COLUMNS FROM `{$table}` LIKE '{$safeCol}'");
    if ($res && $res->num_rows === 0) {
        $con->query($alterSql);
        $log[] = "Added column {$table}.{$safeCol}";
    } else {
        $log[] = "OK (exists): {$table}.{$safeCol}";
    }
    if ($res) {
        $res->free();
    }
}

// --- Live chat (Smartsupp / Tawk / Tidio share these columns) ---
addColumnIfMissing(
    $con,
    'system_settings',
    'live_chat_provider',
    "ALTER TABLE system_settings ADD COLUMN live_chat_provider VARCHAR(50) DEFAULT NULL COMMENT 'tawk, tidio, smartsupp, or null'"
);
addColumnIfMissing(
    $con,
    'system_settings',
    'live_chat_code',
    'ALTER TABLE system_settings ADD COLUMN live_chat_code TEXT DEFAULT NULL COMMENT "JS embed code for the chat widget"'
);

// --- SMTP columns (admin email settings) ---
$smtpColumns = [
    'smtp_host' => "ALTER TABLE system_settings ADD COLUMN smtp_host VARCHAR(190) DEFAULT NULL",
    'smtp_port' => "ALTER TABLE system_settings ADD COLUMN smtp_port INT(11) DEFAULT 465",
    'smtp_username' => "ALTER TABLE system_settings ADD COLUMN smtp_username VARCHAR(190) DEFAULT NULL",
    'smtp_password' => "ALTER TABLE system_settings ADD COLUMN smtp_password VARCHAR(255) DEFAULT NULL",
    'smtp_encryption' => "ALTER TABLE system_settings ADD COLUMN smtp_encryption VARCHAR(20) DEFAULT 'ssl'",
    'smtp_from_email' => "ALTER TABLE system_settings ADD COLUMN smtp_from_email VARCHAR(190) DEFAULT NULL",
    'smtp_from_name' => "ALTER TABLE system_settings ADD COLUMN smtp_from_name VARCHAR(190) DEFAULT NULL",
];
foreach ($smtpColumns as $col => $sql) {
    addColumnIfMissing($con, 'system_settings', $col, $sql);
}

// --- Top up tables + columns (payment_methods.network_type, topup_requests.notified_customer) ---
ensureTopupSchema($con);
$log[] = 'Ran ensureTopupSchema() (payment_methods, topup_requests, migrations).';

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Schema migration</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body { font-family: system-ui, sans-serif; max-width: 720px; margin: 40px auto; padding: 0 16px; }
        h1 { font-size: 1.25rem; }
        ul { line-height: 1.6; }
        .note { background: #f0f7ff; border: 1px solid #cfe8ff; padding: 12px 16px; border-radius: 8px; margin: 20px 0; }
    </style>
</head>
<body>
    <h1>Database schema check complete</h1>
    <div class="note">
        <strong>Smartsupp:</strong> there is no separate column. Use
        <code>live_chat_provider = smartsupp</code> and paste your script into
        <code>live_chat_code</code> in <strong>System Settings</strong>.
    </div>
    <ul>
        <?php foreach ($log as $line): ?>
            <li><?= htmlspecialchars($line, ENT_QUOTES, 'UTF-8'); ?></li>
        <?php endforeach; ?>
    </ul>
    <p><a href="dashboard.php">Back to dashboard</a></p>
</body>
</html>
