using Microsoft.EntityFrameworkCore;

namespace Backend.Data;

public static class OperationalTablesBootstrapper
{
    public static Task EnsureAsync(AwardsDbContext db) =>
        db.Database.ExecuteSqlRawAsync(
            """
            ALTER TABLE "UserSessions"
            ADD COLUMN IF NOT EXISTS "CreatedFromIp" character varying(80) NOT NULL DEFAULT '';

            ALTER TABLE "UserSessions"
            ADD COLUMN IF NOT EXISTS "UserAgent" character varying(400) NOT NULL DEFAULT '';

            CREATE TABLE IF NOT EXISTS "RiskFlags" (
                "Id" integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
                "SeasonId" integer NULL,
                "TwitchUserId" character varying(120) NULL,
                "Source" character varying(80) NOT NULL,
                "Type" character varying(80) NOT NULL,
                "Severity" character varying(20) NOT NULL,
                "Status" character varying(20) NOT NULL,
                "Summary" character varying(240) NOT NULL,
                "CreatedFromIp" character varying(80) NOT NULL,
                "UserAgent" character varying(400) NOT NULL,
                "MetadataJson" text NOT NULL,
                "ReviewedByTwitchId" character varying(120) NULL,
                "CreatedAt" timestamp with time zone NOT NULL,
                "ReviewedAt" timestamp with time zone NULL
            );

            CREATE INDEX IF NOT EXISTS "IX_RiskFlags_Status_CreatedAt"
            ON "RiskFlags" ("Status", "CreatedAt" DESC);

            CREATE INDEX IF NOT EXISTS "IX_RiskFlags_SeasonId"
            ON "RiskFlags" ("SeasonId");

            CREATE TABLE IF NOT EXISTS "AdminAuditEntries" (
                "Id" integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
                "AdminTwitchUserId" character varying(120) NOT NULL,
                "ActionType" character varying(80) NOT NULL,
                "EntityType" character varying(80) NOT NULL,
                "EntityId" character varying(120) NOT NULL,
                "Summary" character varying(240) NOT NULL,
                "MetadataJson" text NOT NULL,
                "CreatedAt" timestamp with time zone NOT NULL
            );

            CREATE INDEX IF NOT EXISTS "IX_AdminAuditEntries_CreatedAt"
            ON "AdminAuditEntries" ("CreatedAt" DESC);

            CREATE TABLE IF NOT EXISTS "ClipSubmissions" (
                "Id" integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
                "SeasonId" integer NOT NULL,
                "CategoryId" integer NULL,
                "SubmittedByTwitchId" character varying(120) NOT NULL,
                "ClipUrl" character varying(500) NOT NULL,
                "Title" character varying(200) NOT NULL,
                "Creator" character varying(120) NOT NULL,
                "Platform" character varying(40) NOT NULL,
                "Status" character varying(20) NOT NULL,
                "CreatedFromIp" character varying(80) NOT NULL,
                "CreatedAt" timestamp with time zone NOT NULL
            );

            CREATE INDEX IF NOT EXISTS "IX_ClipSubmissions_SeasonId_Status"
            ON "ClipSubmissions" ("SeasonId", "Status");
            """);
}
