-- v22 — RHOYLLE founder lifetime license (all DBAOps Sovereign products)
-- Tier Unlimited: all feature gates open, all engines, no expiration (ExpiresAt NULL).

IF OBJECT_ID('ctl.LicenseEntitlements', 'U') IS NULL
BEGIN
    RAISERROR('ctl.LicenseEntitlements not found — run migrate first.', 16, 1);
    RETURN;
END
GO

DECLARE @features NVARCHAR(MAX) = N'{
  "_meta": { "industryPack": "general", "grant": "lifetime", "holder": "RHOYLLE INNOVATION EMPIRE LLC" },
  "suite.core": true,
  "suite.compliance_center": true,
  "suite.security_ops": true,
  "suite.remediation_ai": true,
  "suite.fin_ops": true,
  "suite.sovereignty_evidence": true,
  "fleet.view": true,
  "fleet.register": true,
  "telemetry.view": true,
  "alerts.view": true,
  "alerts.acknowledge": true,
  "compliance.view": true,
  "compliance.audit": true,
  "remediation.view": true,
  "remediation.execute": true,
  "ai.predictions": true,
  "ai.rootcause": true,
  "security.posture": true,
  "security.audit": true,
  "admin.users": true,
  "admin.roles.custom": true,
  "admin.license": true,
  "integrations.powerbi": true,
  "integrations.webhook": true,
  "export.csv": true,
  "export.odata": true,
  "compliance.pack.cjis": true,
  "compliance.pack.nist80053": true,
  "compliance.pack.pci_dss4": true,
  "compliance.pack.hipaa": true,
  "compliance.pack.sox": true,
  "compliance.pack.stig": true,
  "compliance.containment.cjis": true,
  "compliance.containment.fedramp": true,
  "compliance.containment.itar": true,
  "compliance.containment.hipaa": true,
  "compliance.containment.il4": true,
  "compliance.containment.il5": true,
  "compliance.containment.il6": true,
  "learning.simulator": true
}';

DECLARE @engines NVARCHAR(MAX) = N'["SQL Server","Azure SQL","PostgreSQL","Oracle","MySQL","AWS RDS"]';

IF EXISTS (
    SELECT 1 FROM ctl.LicenseEntitlements
    WHERE IsActive = 1 AND Tier = N'Unlimited' AND ExpiresAt IS NULL
      AND Edition LIKE N'%Lifetime%'
)
BEGIN
    PRINT 'v22 — Lifetime license already active; skipping.';
END
ELSE
BEGIN
    UPDATE ctl.LicenseEntitlements SET IsActive = 0 WHERE IsActive = 1;

    INSERT INTO ctl.LicenseEntitlements (
        Edition, Tier, InstancesAllowed, EnginesAllowed, RetentionDays,
        MaxUsers, MaxCustomRoles, FeaturesJson, ExpiresAt, IsActive, LicenseKey
    )
    VALUES (
        N'DBAOps Sovereign - Lifetime Empire (RHOYLLE)',
        N'Unlimited',
        9999,
        @engines,
        36500,
        999,
        99,
        @features,
        NULL,
        1,
        N'SOVEREIGN-LIFETIME-RHOYLLE-EMPIRE-v1'
    );
    PRINT 'v22 — Lifetime Unlimited license applied for RHOYLLE.';
END
GO

IF OBJECT_ID('ctl.License', 'U') IS NOT NULL
BEGIN
    UPDATE ctl.License SET IsActive = 0 WHERE IsActive = 1;
    INSERT INTO ctl.License (Edition, InstancesAllowed, ExpiresAt, IsActive)
    VALUES (N'DBAOps Sovereign - Lifetime Empire (RHOYLLE)', 9999, NULL, 1);
END
GO

PRINT 'v22 — Lifetime Unlimited license applied for RHOYLLE.';
GO
