<?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],
];

$features = [
    'members' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc1001', 'name' => 'Mitglieder', 'description' => 'Mitgliederverwaltung und zentrale Personenpflege.'],
    'ledger' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc1002', 'name' => 'Ledger', 'description' => 'Kontostand, Buchungen und Verlauf pro Mandant.'],
    'payments' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc1003', 'name' => 'Einzahlungen', 'description' => 'Einzahlungen erfassen und zuordnen.'],
    'content' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc1004', 'name' => 'Inhalte', 'description' => 'Hinweise, FAQ und einfache Inhalte pro Mandant.'],
    'tenant_settings' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2001', 'name' => 'Mandanten-Einstellungen', 'description' => 'Eigene Einstellungen pro Mandant verwalten.'],
    'pdf_export' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2002', 'name' => 'PDF-Listen', 'description' => 'Druckfertige Listen für den Export bereitstellen.'],
    'paper_strike_entry' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2003', 'name' => 'Papierlisten-Erfassung', 'description' => 'Striche aus Papierlisten sauber nacherfassen.'],
    'basic_exports' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2004', 'name' => 'Basis-Exporte', 'description' => 'Optionale CSV- und Datenexporte außerhalb des Standardumfangs.'],
    'oidc' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2005', 'name' => 'OIDC/SSO', 'description' => 'Zentrale Anmeldung über OIDC oder SSO.'],
    'imports' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2006', 'name' => 'Importe', 'description' => 'Import-Jobs für strukturierte Daten.'],
    'exports' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2007', 'name' => 'Erweiterte Exporte', 'description' => 'Zusätzliche Export-Jobs und Datenausleitungen.'],
    'notifications' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2008', 'name' => 'Benachrichtigungen', 'description' => 'Systemische Benachrichtigungen pro Mandant.'],
    'surveys' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2009', 'name' => 'Umfragen', 'description' => 'Umfragen und Rückmeldungen erfassen.'],
    'advanced_reporting' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2010', 'name' => 'Erweiterte Auswertungen', 'description' => 'Vertiefte Berichte und Auswertungen.'],
    'white_label' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2011', 'name' => 'White-Labeling', 'description' => 'Kundenspezifische Farb- und Markenanpassung.'],
    'custom_features' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2012', 'name' => 'Sonderfunktionen', 'description' => 'Mandantenspezifische Freischaltungen.'],
    'priority_updates' => ['id' => '8f6c468f-8c14-4681-b262-4c8ffacc2013', 'name' => 'Priorisierte Updates', 'description' => 'Bevorzugte Update-Betreuung für Enterprise-Tenants.'],
];

$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' => array_keys($features),
];

$statements = [];

foreach ($plans as $planKey => $plan) {
    $memberLimit = $plan['member_limit'] === null ? 'NULL' : (string) (int) $plan['member_limit'];
    $escapedName = addslashes($plan['name']);
    $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,
        $escapedName,
        $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';",
        $escapedName,
        $memberLimit,
        $plan['sort_order'],
        $planKey
    );
}

foreach ($features as $featureKey => $feature) {
    $statements[] = sprintf(
        "INSERT INTO features (id, feature_key, name, description, is_active, created_at, updated_at)\nSELECT '%s', '%s', '%s', '%s', 1, NOW(), NOW()\nWHERE NOT EXISTS (SELECT 1 FROM features WHERE feature_key = '%s');",
        $feature['id'],
        $featureKey,
        addslashes($feature['name']),
        addslashes($feature['description']),
        $featureKey
    );
    $statements[] = sprintf(
        "UPDATE features SET name = '%s', description = '%s', is_active = 1, updated_at = NOW() WHERE feature_key = '%s';",
        addslashes($feature['name']),
        addslashes($feature['description']),
        $featureKey
    );
}

$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';
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', id, NOW()\nFROM features\nWHERE 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 = features.id\n  );",
            $mappingId,
            $plans[$planKey]['id'],
            $featureKey,
            $plans[$planKey]['id']
        );
    }
}

return [
    'key' => '2026_03_22_000002_seed_license_plans_and_features',
    'title' => 'Lizenzpläne und Features füllen',
    'description' => 'Legt die mitgliederbasierten SaaS-Lizenzstufen und die dazugehörigen Feature-Flags an.',
    'statements' => $statements,
];
