<?php

$plans = [
    'free' => ['id' => '0f6e004d-ec4c-4f12-8f91-fdf795b80a00', 'name' => 'Free', 'sort_order' => 5, 'member_limit' => 10],
    'starter' => ['id' => '0f6e004d-ec4c-4f12-8f91-fdf795b80a01', 'name' => 'Starter', 'sort_order' => 10, 'member_limit' => 25],
    'team' => ['id' => '0f6e004d-ec4c-4f12-8f91-fdf795b80a02', 'name' => 'Team', 'sort_order' => 20, 'member_limit' => 75],
    'business' => ['id' => '0f6e004d-ec4c-4f12-8f91-fdf795b80a03', 'name' => 'Business', 'sort_order' => 30, 'member_limit' => 200],
    'enterprise' => ['id' => '0f6e004d-ec4c-4f12-8f91-fdf795b80a04', 'name' => 'Enterprise', 'sort_order' => 40, 'member_limit' => null],
];

$planFeatures = [
    'free' => ['members', 'ledger', 'payments', 'content', 'tenant_settings', 'pdf_export', 'paper_strike_entry'],
    'starter' => ['members', 'ledger', 'payments', 'content', 'tenant_settings', 'pdf_export', 'paper_strike_entry'],
    'team' => ['members', 'ledger', 'payments', 'content', 'tenant_settings', 'pdf_export', 'paper_strike_entry', 'notifications'],
    'business' => ['members', 'ledger', 'payments', 'content', 'tenant_settings', 'pdf_export', 'paper_strike_entry', 'oidc', 'imports', 'exports', 'notifications', 'surveys', 'advanced_reporting'],
    'enterprise' => ['members', 'ledger', 'payments', 'content', 'tenant_settings', 'pdf_export', 'paper_strike_entry', 'basic_exports', 'oidc', 'imports', 'exports', 'notifications', 'surveys', 'advanced_reporting', 'white_label', 'custom_features', 'priority_updates'],
];

$statements = [];

foreach ($plans as $planKey => $plan) {
    $memberLimit = $plan['member_limit'] === null ? 'NULL' : (string) (int) $plan['member_limit'];
    $statements[] = sprintf(
        "INSERT INTO license_plans (id, plan_key, name, member_limit, sort_order, is_active, created_at, updated_at)\nSELECT '%s', '%s', '%s', %s, %d, 1, NOW(), NOW()\nWHERE NOT EXISTS (SELECT 1 FROM license_plans WHERE plan_key = '%s');",
        $plan['id'],
        $planKey,
        addslashes($plan['name']),
        $memberLimit,
        $plan['sort_order'],
        $planKey
    );
    $statements[] = sprintf(
        "UPDATE license_plans SET name = '%s', member_limit = %s, sort_order = %d, is_active = 1, updated_at = NOW() WHERE plan_key = '%s';",
        addslashes($plan['name']),
        $memberLimit,
        $plan['sort_order'],
        $planKey
    );
}

$statements[] = <<<'SQL'
DELETE lpf
FROM license_plan_features lpf
INNER JOIN features f ON f.id = lpf.feature_id
WHERE f.feature_key = 'basic_exports'
  AND EXISTS (
      SELECT 1
      FROM license_plans lp
      WHERE lp.id = lpf.license_plan_id
        AND lp.plan_key <> 'enterprise'
  );
SQL;

foreach ($planFeatures as $planKey => $featureKeys) {
    foreach ($featureKeys as $featureKey) {
        $mappingId = scripts_uuid_from_string('license-plan-feature:' . $planKey . ':' . $featureKey);
        $statements[] = sprintf(
            "INSERT INTO license_plan_features (id, license_plan_id, feature_id, created_at)\nSELECT '%s', '%s', f.id, NOW()\nFROM features f\nINNER JOIN license_plans lp ON lp.id = '%s'\nWHERE f.feature_key = '%s'\n  AND NOT EXISTS (\n      SELECT 1 FROM license_plan_features lpf WHERE lpf.license_plan_id = '%s' AND lpf.feature_id = f.id\n  );",
            $mappingId,
            $plans[$planKey]['id'],
            $plans[$planKey]['id'],
            $featureKey,
            $plans[$planKey]['id']
        );
    }
}

return [
    'key' => '2026_03_22_000006_align_member_based_license_model',
    'title' => 'Mitgliederbasiertes Lizenzmodell ausrichten',
    'description' => 'Stellt Free-, Starter-, Team-, Business- und Enterprise-Pläne mit passenden Mitgliedergrenzen bereit.',
    'statements' => $statements,
];
