<?php
// upgrade_db_final.php
require_once 'fingerprint_wrapper.php';
header('Content-Type: text/plain');

$pdo = get_db_connection();
if (!$pdo) die("Database connection failed.\n");

try {
    $pdo->exec("ALTER TABLE traffic_logs 
        ADD COLUMN device_type VARCHAR(20) DEFAULT 'Unknown' AFTER is_proxy,
        ADD COLUMN os VARCHAR(30) DEFAULT 'Unknown' AFTER device_type,
        ADD COLUMN browser VARCHAR(30) DEFAULT 'Unknown' AFTER os,
        ADD COLUMN block_reason VARCHAR(30) DEFAULT 'Unknown' AFTER browser");
    echo "SUCCESS: Device Intel and Threat Classification columns added!\n";
} catch (PDOException $e) {
    if (strpos($e->getMessage(), 'Duplicate column name') !== false) {
        echo "NOTICE: Columns already exist. You are good to go.\n";
    } else {
        echo "ERROR: " . $e->getMessage() . "\n";
    }
}
?>