<?php
declare(strict_types=1);

return function (\App\Core\Database $db) {
    $tables = [
        'members' => [
            ['activated_by_payment_id', 'BIGINT UNSIGNED NULL', 'membership_number'],
            ['activated_at', 'DATETIME NULL', 'activated_by_payment_id'],
        ],
        'spouses' => [
            ['activated_by_payment_id', 'BIGINT UNSIGNED NULL', 'status'],
            ['fee_receipt_number', 'VARCHAR(50) NULL', 'activated_by_payment_id'],
        ],
        'children' => [
            ['activated_by_payment_id', 'BIGINT UNSIGNED NULL', 'status'],
            ['fee_receipt_number', 'VARCHAR(50) NULL', 'activated_by_payment_id'],
        ],
        'temporary_members' => [
            ['activated_by_payment_id', 'BIGINT UNSIGNED NULL', 'status'],
            ['fee_receipt_number', 'VARCHAR(50) NULL', 'activated_by_payment_id'],
        ],
    ];

    foreach ($tables as $table => $columns) {
        foreach ($columns as [$col, $type, $after]) {
            $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}` {$type} AFTER `{$after}`");
            }
        }
    }
};
