<?php

$statements = ['SELECT 1;'];

try {
    $env = scripts_read_env_file(scripts_env_path());
    $config = scripts_update_config_from_env($env);

    if (($config['database'] ?? '') !== '') {
        $pdo = scripts_connect_pdo($config);
        $columnExists = scripts_query_one(
            $pdo,
            <<<'SQL'
SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
  AND TABLE_NAME = 'license_plans'
  AND COLUMN_NAME = 'member_limit'
LIMIT 1
SQL
        ) !== null;

        if (!$columnExists) {
            $statements = [
                <<<'SQL'
ALTER TABLE license_plans
ADD COLUMN member_limit INT NULL AFTER name;
SQL,
            ];
        }
    }
} catch (Throwable) {
    $statements = [
        <<<'SQL'
ALTER TABLE license_plans
ADD COLUMN member_limit INT NULL AFTER name;
SQL,
    ];
}

return [
    'key' => '2026_03_22_000005_add_member_limit_to_license_plans',
    'title' => 'Mitgliederlimit für Lizenzpläne ergänzen',
    'description' => 'Erweitert die Lizenzpläne um ein optionales Limit für aktive Mitglieder.',
    'statements' => $statements,
];
