<?php
require 'db.php';

try {
    echo "Updating database schema...\n";

    // Add columns if they don't exist
    $columns = [
        'age' => 'INT DEFAULT 30',
        'features' => 'VARCHAR(255) DEFAULT "Brak"',
        'location' => 'VARCHAR(255) DEFAULT "Nieznana"',
        'profession' => 'VARCHAR(255) DEFAULT "Gość"'
    ];

    foreach ($columns as $col => $def) {
        try {
            $pdo->query("SELECT $col FROM guests LIMIT 1");
            echo "Column $col already exists.\n";
        } catch (Exception $e) {
            echo "Adding column $col...\n";
            $pdo->exec("ALTER TABLE guests ADD COLUMN $col $def");
        }
    }

    // Update sample data
    echo "Updating sample data...\n";

    // Hacker One
    $stmt = $pdo->prepare("UPDATE guests SET age = ?, features = ?, location = ?, profession = ? WHERE name = ?");
    $stmt->execute([24, 'Tatuaż na szyi, okulary', 'Warszawa, PL', 'Cybersec Specialist', 'Hacker One']);

    // Stark
    $stmt->execute([48, 'Wysoki poziom arogancji, koia bródka', 'Malibu, USA', 'CEO Stark Industries', 'Tony Stark']);

    echo "Database updated successfully.\n";

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>