<?php
declare(strict_types=1);

return function (\App\Core\Database $db) {
    $tables = [
        'reservations',
        'academy_settlements',
        'achievement_definitions',
        'activity_subscriptions',
        'facility_grids',
        'facility_monthly_plans',
        'facility_zone_schedules',
        'player_evaluations',
        'player_injuries',
        'pool_bookings',
        'pool_configurations',
        'pool_schedules',
        'sa_player_documents',
        'sa_pricing_rules',
        'tournaments',
        'training_groups',
        'training_sessions',
    ];

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

    // Also ensure created_by exists on tables that use autoTrackAuthor
    $needsCreatedBy = ['achievement_definitions', 'player_injuries'];
    foreach ($needsCreatedBy as $table) {
        $exists = $db->selectOne(
            "SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = ? AND column_name = 'created_by'",
            [$table]
        );
        if (!$exists) {
            $db->raw("ALTER TABLE `{$table}` ADD COLUMN `created_by` BIGINT UNSIGNED NULL");
        }
    }
};
