<?php
/**
 * One-off form settings seed for XXXV Konferencja registration.
 *
 * Usage:
 * - Browser: /_rejestracja/sql/apply-2026-04-24-registration-form-settings.php?run=20260424
 * - CLI:     php apply-2026-04-24-registration-form-settings.php --run
 *
 * Remove this file from the server after a successful production run.
 */

ini_set('display_errors', 1);
error_reporting(E_ALL);

$settings = array(
    'registration_form_days_lodging' => array(
        'value' => "3/4 listopada\n4/5 listopada",
        'description' => 'Dni udzialu - jeden dzien z noclegiem',
    ),
    'registration_form_days_no_lodging' => array(
        'value' => "3 listopada\n4 listopada\n5 listopada",
        'description' => 'Dni udzialu - jeden dzien bez noclegu',
    ),
    'registration_form_vat_multiplier' => array(
        'value' => '1.23',
        'description' => 'Mnoznik VAT dla cen formularza rejestracji',
    ),
    'registration_form_one_day_price_prom' => array(
        'value' => '1300',
        'description' => 'Cena netto promocyjna - jeden dzien',
    ),
    'registration_form_one_day_price_normal' => array(
        'value' => '1600',
        'description' => 'Cena netto normalna - jeden dzien',
    ),
);

$isCli = PHP_SAPI === 'cli';
$approved = $isCli
    ? in_array('--run', $argv, true)
    : (isset($_GET['run']) && $_GET['run'] === '20260424');

header_safe('Content-Type: text/plain; charset=utf-8');

if (!$approved) {
    echo "DRY RUN ONLY\n";
    echo "To apply form settings seed, run with ?run=20260424 in browser or --run in CLI.\n";
    echo "No database changes were made.\n";
    exit;
}

$configPath = __DIR__ . '/../core/config/Strona/db.config.ini';
if (!is_file($configPath)) {
    fail("Config file not found: " . $configPath);
}

$config = parse_ini_file($configPath, true);
if (!isset($config['db'])) {
    fail("Missing [db] section in config.");
}

$dbConfig = $config['db'];
foreach (array('prodHost', 'prodUser', 'prodPass', 'prodDb') as $key) {
    if (!array_key_exists($key, $dbConfig)) {
        fail("Missing db config key: " . $key);
    }
}

$mysqli = new mysqli(
    $dbConfig['prodHost'],
    $dbConfig['prodUser'],
    $dbConfig['prodPass'],
    $dbConfig['prodDb']
);

if ($mysqli->connect_errno) {
    fail("MySQL connection failed: " . $mysqli->connect_error);
}

$mysqli->set_charset('utf8');

echo "Applying registration form settings seed...\n";

$inserted = 0;
$skipped = 0;

foreach ($settings as $variable => $row) {
    if (setup_variable_exists($mysqli, $variable)) {
        $skipped++;
        continue;
    }

    insert_setup_variable($mysqli, $variable, $row['value'], $row['description']);
    $inserted++;
}

echo "Form settings seed complete.\n";
echo "Inserted: " . $inserted . "\n";
echo "Skipped: " . $skipped . "\n";
$mysqli->close();

function setup_variable_exists(mysqli $mysqli, $variable) {
    $stmt = $mysqli->prepare("SELECT COUNT(*) AS cnt FROM wp_setup WHERE variable = ?");
    if (!$stmt) {
        fail("Prepare failed: " . $mysqli->error);
    }

    $stmt->bind_param('s', $variable);
    if (!$stmt->execute()) {
        fail("Setup check failed: " . $stmt->error);
    }

    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    $stmt->close();

    return isset($row['cnt']) && (int)$row['cnt'] > 0;
}

function insert_setup_variable(mysqli $mysqli, $variable, $value, $description) {
    $stmt = $mysqli->prepare("INSERT INTO wp_setup (variable, value, description) VALUES (?, ?, ?)");
    if (!$stmt) {
        fail("Prepare failed: " . $mysqli->error);
    }

    $stmt->bind_param('sss', $variable, $value, $description);
    if (!$stmt->execute()) {
        fail("Insert failed for " . $variable . ": " . $stmt->error);
    }

    $stmt->close();
}

function fail($message) {
    echo "ERROR: " . $message . "\n";
    exit(1);
}

function header_safe($header) {
    if (!headers_sent() && PHP_SAPI !== 'cli') {
        header($header);
    }
}

?>
