<?php
$config = require __DIR__ . '/../config/config.php';
$pdo = new PDO(
    'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['name'] . ';charset=utf8mb4',
    $config['db']['user'],
    $config['db']['pass'],
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
$addCol = function ($table, $col, $def) use ($pdo) {
    try {
        $pdo->exec("ALTER TABLE $table ADD COLUMN $col $def");
        echo "Added $table.$col\n";
    } catch (PDOException $e) {
        if (strpos($e->getMessage(), 'Duplicate column') !== false) return;
        echo "Warning $table.$col: " . $e->getMessage() . "\n";
    }
};
$addCol('inventory_items', 'expiry_date', 'DATE NULL');
$addCol('inventory_items', 'batch_number', 'VARCHAR(100) NULL');
$addCol('results', 'tech_name', 'VARCHAR(150) NULL');
$addCol('patients', 'portal_code', 'VARCHAR(20) NULL');
$addCol('patients', 'default_doctor_id', 'INT NULL');
try { $pdo->exec('ALTER TABLE patients ADD UNIQUE KEY uk_portal_code (portal_code)'); } catch (PDOException $e) {}
echo "Done.\n";
