<?php

declare(strict_types=1);

require dirname(__DIR__) . '/bootstrap.php';

use Core\Database;

if (!in_array('--force', $argv, true)) {
    echo "Este script altera a tabela admissions. Rode com --force para continuar.\n";
    exit(1);
}

$pdo = Database::connection();
$definitions = [
    'employment_function' => "ALTER TABLE admissions ADD COLUMN employment_function VARCHAR(150) NULL AFTER uniform_size",
    'admission_date' => "ALTER TABLE admissions ADD COLUMN admission_date DATE NULL AFTER employment_function",
    'dismissal_date' => "ALTER TABLE admissions ADD COLUMN dismissal_date DATE NULL AFTER admission_date",
    'workload_hours' => "ALTER TABLE admissions ADD COLUMN workload_hours VARCHAR(30) NULL AFTER dismissal_date",
    'salary_amount' => "ALTER TABLE admissions ADD COLUMN salary_amount DECIMAL(10,2) NULL AFTER workload_hours",
    'salary_type' => "ALTER TABLE admissions ADD COLUMN salary_type VARCHAR(20) NULL AFTER salary_amount",
];

foreach ($definitions as $column => $sql) {
    $exists = (bool) $pdo->query("SHOW COLUMNS FROM admissions LIKE '{$column}'")->fetch();
    if (!$exists) {
        $pdo->exec($sql);
        echo "Coluna {$column} criada com sucesso.\n";
    } else {
        echo "Coluna {$column} já existe.\n";
    }
}

foreach (['bank_agency', 'bank_account'] as $column) {
    $pdo->exec("ALTER TABLE admissions MODIFY COLUMN {$column} " . ($column === 'bank_agency' ? 'VARCHAR(30)' : 'VARCHAR(40)') . ' NULL');
    echo "Coluna {$column} ajustada para aceitar NULL.\n";
}
