<?php

return [
    'key' => '2026_03_22_000001_add_tenant_settings_and_license_tables',
    'title' => 'Tenant-Einstellungen und Lizenztabellen anlegen',
    'description' => 'Ergänzt Tenant-Einstellungen, Lizenzpläne, Feature-Zuordnungen und Overrides.',
    'statements' => [
        <<<'SQL'
CREATE TABLE IF NOT EXISTS tenant_settings (
    id CHAR(36) NOT NULL PRIMARY KEY,
    tenant_id CHAR(36) NOT NULL,
    setting_key VARCHAR(120) NOT NULL,
    setting_value TEXT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    UNIQUE KEY tenant_settings_unique (tenant_id, setting_key),
    CONSTRAINT tenant_settings_tenant_fk FOREIGN KEY (tenant_id) REFERENCES tenants(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL,
        <<<'SQL'
CREATE TABLE IF NOT EXISTS license_plans (
    id CHAR(36) NOT NULL PRIMARY KEY,
    plan_key VARCHAR(100) NOT NULL,
    name VARCHAR(255) NOT NULL,
    member_limit INT NULL,
    sort_order INT NOT NULL DEFAULT 0,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    UNIQUE KEY license_plans_key_unique (plan_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL,
        <<<'SQL'
CREATE TABLE IF NOT EXISTS features (
    id CHAR(36) NOT NULL PRIMARY KEY,
    feature_key VARCHAR(120) NOT NULL,
    name VARCHAR(255) NOT NULL,
    description TEXT NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    UNIQUE KEY features_key_unique (feature_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL,
        <<<'SQL'
CREATE TABLE IF NOT EXISTS license_plan_features (
    id CHAR(36) NOT NULL PRIMARY KEY,
    license_plan_id CHAR(36) NOT NULL,
    feature_id CHAR(36) NOT NULL,
    created_at DATETIME NOT NULL,
    UNIQUE KEY license_plan_features_unique (license_plan_id, feature_id),
    CONSTRAINT license_plan_features_plan_fk FOREIGN KEY (license_plan_id) REFERENCES license_plans(id),
    CONSTRAINT license_plan_features_feature_fk FOREIGN KEY (feature_id) REFERENCES features(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL,
        <<<'SQL'
CREATE TABLE IF NOT EXISTS tenant_feature_overrides (
    id CHAR(36) NOT NULL PRIMARY KEY,
    tenant_id CHAR(36) NOT NULL,
    feature_id CHAR(36) NOT NULL,
    is_enabled TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    UNIQUE KEY tenant_feature_overrides_unique (tenant_id, feature_id),
    CONSTRAINT tenant_feature_overrides_tenant_fk FOREIGN KEY (tenant_id) REFERENCES tenants(id),
    CONSTRAINT tenant_feature_overrides_feature_fk FOREIGN KEY (feature_id) REFERENCES features(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL,
        <<<'SQL'
CREATE TABLE IF NOT EXISTS tenant_licenses (
    id CHAR(36) NOT NULL PRIMARY KEY,
    tenant_id CHAR(36) NOT NULL,
    license_plan_id CHAR(36) NOT NULL,
    status VARCHAR(50) NOT NULL DEFAULT 'active',
    starts_at DATETIME NULL,
    ends_at DATETIME NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    UNIQUE KEY tenant_licenses_tenant_unique (tenant_id),
    CONSTRAINT tenant_licenses_tenant_fk FOREIGN KEY (tenant_id) REFERENCES tenants(id),
    CONSTRAINT tenant_licenses_plan_fk FOREIGN KEY (license_plan_id) REFERENCES license_plans(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
SQL,
    ],
];
