using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using RpgRoller.Data;

namespace RpgRoller.Migrations;

[DbContext(typeof(RpgRollerDbContext))]
[Migration("20260226084000_InitialSchema")]
public sealed class InitialSchema : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.Sql(
            """
            CREATE TABLE IF NOT EXISTS "Users" (
                "Id" TEXT NOT NULL CONSTRAINT "PK_Users" PRIMARY KEY,
                "Username" TEXT NOT NULL,
                "UsernameNormalized" TEXT NOT NULL,
                "PasswordHash" TEXT NOT NULL,
                "DisplayName" TEXT NOT NULL,
                "ActiveCharacterId" TEXT NULL
            );
            """);

        migrationBuilder.Sql(
            """
            CREATE TABLE IF NOT EXISTS "Sessions" (
                "Token" TEXT NOT NULL CONSTRAINT "PK_Sessions" PRIMARY KEY,
                "UserId" TEXT NOT NULL,
                "CreatedAtUtc" TEXT NOT NULL
            );
            """);

        migrationBuilder.Sql(
            """
            CREATE TABLE IF NOT EXISTS "Campaigns" (
                "Id" TEXT NOT NULL CONSTRAINT "PK_Campaigns" PRIMARY KEY,
                "GmUserId" TEXT NOT NULL,
                "Name" TEXT NOT NULL,
                "Ruleset" TEXT NOT NULL,
                "Version" INTEGER NOT NULL
            );
            """);

        migrationBuilder.Sql(
            """
            CREATE TABLE IF NOT EXISTS "Characters" (
                "Id" TEXT NOT NULL CONSTRAINT "PK_Characters" PRIMARY KEY,
                "OwnerUserId" TEXT NOT NULL,
                "CampaignId" TEXT NOT NULL,
                "Name" TEXT NOT NULL
            );
            """);

        migrationBuilder.Sql(
            """
            CREATE TABLE IF NOT EXISTS "Skills" (
                "Id" TEXT NOT NULL CONSTRAINT "PK_Skills" PRIMARY KEY,
                "CharacterId" TEXT NOT NULL,
                "Name" TEXT NOT NULL,
                "DiceRollDefinition" TEXT NOT NULL
            );
            """);

        migrationBuilder.Sql(
            """
            ALTER TABLE "Skills" ADD COLUMN "WildDice" INTEGER NOT NULL DEFAULT 1;
            """);

        migrationBuilder.Sql(
            """
            ALTER TABLE "Skills" ADD COLUMN "AllowFumble" INTEGER NOT NULL DEFAULT 1;
            """);

        migrationBuilder.Sql(
            """
            CREATE TABLE IF NOT EXISTS "RollLogEntries" (
                "Id" TEXT NOT NULL CONSTRAINT "PK_RollLogEntries" PRIMARY KEY,
                "CampaignId" TEXT NOT NULL,
                "CharacterId" TEXT NOT NULL,
                "SkillId" TEXT NOT NULL,
                "RollerUserId" TEXT NOT NULL,
                "Visibility" TEXT NOT NULL,
                "Result" INTEGER NOT NULL,
                "Breakdown" TEXT NOT NULL,
                "TimestampUtc" TEXT NOT NULL
            );
            """);

        migrationBuilder.Sql("""CREATE UNIQUE INDEX IF NOT EXISTS "IX_Users_UsernameNormalized" ON "Users" ("UsernameNormalized");""");
        migrationBuilder.Sql("""CREATE INDEX IF NOT EXISTS "IX_Sessions_UserId" ON "Sessions" ("UserId");""");
        migrationBuilder.Sql("""CREATE INDEX IF NOT EXISTS "IX_Campaigns_GmUserId" ON "Campaigns" ("GmUserId");""");
        migrationBuilder.Sql("""CREATE INDEX IF NOT EXISTS "IX_Characters_OwnerUserId" ON "Characters" ("OwnerUserId");""");
        migrationBuilder.Sql("""CREATE INDEX IF NOT EXISTS "IX_Characters_CampaignId" ON "Characters" ("CampaignId");""");
        migrationBuilder.Sql("""CREATE INDEX IF NOT EXISTS "IX_Skills_CharacterId" ON "Skills" ("CharacterId");""");
        migrationBuilder.Sql("""CREATE INDEX IF NOT EXISTS "IX_RollLogEntries_CampaignId" ON "RollLogEntries" ("CampaignId");""");
        migrationBuilder.Sql("""CREATE INDEX IF NOT EXISTS "IX_RollLogEntries_RollerUserId" ON "RollLogEntries" ("RollerUserId");""");
        migrationBuilder.Sql("""CREATE INDEX IF NOT EXISTS "IX_RollLogEntries_SkillId" ON "RollLogEntries" ("SkillId");""");
        migrationBuilder.Sql("""CREATE INDEX IF NOT EXISTS "IX_RollLogEntries_CharacterId" ON "RollLogEntries" ("CharacterId");""");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.Sql("""DROP TABLE IF EXISTS "RollLogEntries";""");
        migrationBuilder.Sql("""DROP TABLE IF EXISTS "Skills";""");
        migrationBuilder.Sql("""DROP TABLE IF EXISTS "Characters";""");
        migrationBuilder.Sql("""DROP TABLE IF EXISTS "Campaigns";""");
        migrationBuilder.Sql("""DROP TABLE IF EXISTS "Sessions";""");
        migrationBuilder.Sql("""DROP TABLE IF EXISTS "Users";""");
    }
}
