<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $tables = ['reservations', 'activity_subscriptions'];
    $columns = ['is_archived', 'archived_at', 'archived_by'];

    foreach ($tables as $table) {
        foreach ($columns as $col) {
            $exists = $db->selectOne(
                "SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?",
                [$table, $col]
            );
            if ($exists) {
                continue;
            }
            $sql = match ($col) {
                'is_archived' => "ALTER TABLE `{$table}` ADD COLUMN `is_archived` TINYINT(1) NOT NULL DEFAULT 0",
                'archived_at' => "ALTER TABLE `{$table}` ADD COLUMN `archived_at` TIMESTAMP NULL DEFAULT NULL",
                'archived_by' => "ALTER TABLE `{$table}` ADD COLUMN `archived_by` BIGINT UNSIGNED NULL",
            };
            $db->raw($sql);
        }
    }
};
