-- v21 — Pilot fact seed (truthfulness gate) + license signing metadata for shipment readiness
-- Idempotent: only seeds when fact tables are empty and fleet servers exist (or seeds one pilot server).

IF NOT EXISTS (SELECT 1 FROM ctl.Servers)
BEGIN
    INSERT INTO ctl.Servers (ServerName, Engine, Version, Edition, HealthScore, LastSeen)
    VALUES (N'SQLPROD01', N'SQL Server', N'2022', N'Enterprise', 95, SYSUTCDATETIME());
END
GO

DECLARE @sid UNIQUEIDENTIFIER = (SELECT TOP 1 ServerId FROM ctl.Servers ORDER BY ServerName);

IF @sid IS NOT NULL
BEGIN
    IF NOT EXISTS (SELECT 1 FROM [backup].DatabaseBackupFact)
    BEGIN
        INSERT INTO [backup].DatabaseBackupFact (ServerId, DatabaseName, RecoveryModel, LastFullBackup, LastLogBackup, RpoHoursTarget, IsCompliant)
        VALUES
            (@sid, N'master', N'SIMPLE', DATEADD(day, -1, SYSUTCDATETIME()), DATEADD(hour, -2, SYSUTCDATETIME()), 24, 1),
            (@sid, N'ContosoOLTP', N'FULL', DATEADD(day, -1, SYSUTCDATETIME()), DATEADD(hour, -1, SYSUTCDATETIME()), 24, 1);
    END

    IF NOT EXISTS (SELECT 1 FROM hadr.AGReplicaFact)
    BEGIN
        INSERT INTO hadr.AGReplicaFact (ServerId, AGName, ReplicaRole, SynchronizationState, EstimatedDataLossSeconds)
        VALUES (@sid, N'ContosoAG', N'PRIMARY', N'SYNCHRONIZED', 0);
    END

    IF NOT EXISTS (SELECT 1 FROM patch.InstancePatchFact)
    BEGIN
        INSERT INTO patch.InstancePatchFact (ServerId, ProductVersion, ProductLevel, SqlBuildNumber, CuBehindCount)
        VALUES (@sid, N'16.0.4195.2', N'CU', 4195, 0);
    END

    IF NOT EXISTS (SELECT 1 FROM inv.DatabaseInventoryFact)
    BEGIN
        INSERT INTO inv.DatabaseInventoryFact (ServerId, DatabaseName, SizeMB, [State], RecoveryModel)
        VALUES
            (@sid, N'master', 512, N'ONLINE', N'SIMPLE'),
            (@sid, N'ContosoOLTP', 8192, N'ONLINE', N'FULL');
    END

    IF NOT EXISTS (SELECT 1 FROM ops.AgentJobRunFact)
    BEGIN
        INSERT INTO ops.AgentJobRunFact (ServerId, JobName, LastRunOutcome, LastRunDate, LastRunDurationSec, IsDisabled)
        VALUES (@sid, N'sp_purge_jobhistory', N'Succeeded', DATEADD(hour, -6, SYSUTCDATETIME()), 12, 0);
    END

    IF NOT EXISTS (SELECT 1 FROM cap.VolumeSpaceFact)
    BEGIN
        INSERT INTO cap.VolumeSpaceFact (ServerId, MountPoint, TotalGB, FreeGB)
        VALUES (@sid, N'C:', 500.00, 180.50);
    END

    IF NOT EXISTS (SELECT 1 FROM perf.Snapshots)
    BEGIN
        INSERT INTO perf.Snapshots (ServerId, CollectedAt, CpuPercent, SqlMemoryUsedMB, BlockingSessions, ActiveSessions, BatchRequestsPerSec, AvgSignalWaitMs)
        VALUES (@sid, SYSUTCDATETIME(), 42.5, 12288, 0, 38, 1250.00, 1.2500);
    END
END
GO

IF OBJECT_ID('ctl.LicenseEntitlements', 'U') IS NOT NULL
BEGIN
    UPDATE ctl.LicenseEntitlements
    SET LicenseKey = N'SOVEREIGN-PILOT-SIGNED-KEY-v1',
        ExpiresAt = DATEADD(year, 1, SYSUTCDATETIME())
    WHERE IsActive = 1
      AND (LicenseKey IS NULL OR LicenseKey IN (N'', N'MOCK-DEV'));
END
GO

PRINT 'v21 — pilot facts seeded (when empty); license metadata hardened for readiness.';
GO
