<?php
declare(strict_types=1);

return function (\App\Core\Database $db): void {
    $table = 'death_cases';

    $columnsToAdd = [
        'primary_spouse_id'         => "BIGINT UNSIGNED NULL AFTER `spouse_id`",
        'secondary_spouses_json'    => "TEXT NULL AFTER `primary_spouse_id`",
        'primary_spouse_form_filled' => "TINYINT(1) NOT NULL DEFAULT 0 AFTER `secondary_spouses_json`",
        'child_id'                  => "BIGINT UNSIGNED NULL AFTER `primary_spouse_form_filled`",
    ];

    foreach ($columnsToAdd as $col => $definition) {
        $exists = $db->selectOne(
            "SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?",
            [$table, $col]
        );
        if (!$exists) {
            $db->raw("ALTER TABLE `{$table}` ADD COLUMN `{$col}` {$definition}");
        }
    }
};
