using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace RpgRoller.Migrations
{
    /// <inheritdoc />
    public partial class AddSkillGroupPrototypes : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<bool>(
                name: "AllowFumble",
                table: "SkillGroups",
                type: "INTEGER",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<string>(
                name: "DiceRollDefinition",
                table: "SkillGroups",
                type: "TEXT",
                maxLength: 128,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<int>(
                name: "WildDice",
                table: "SkillGroups",
                type: "INTEGER",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.Sql("""
                UPDATE SkillGroups
                SET DiceRollDefinition = CASE
                        WHEN EXISTS (
                            SELECT 1
                            FROM Characters c
                            INNER JOIN Campaigns cp ON cp.Id = c.CampaignId
                            WHERE c.Id = SkillGroups.CharacterId
                              AND cp.Ruleset = 'D6')
                        THEN '1D'
                        ELSE '1d20'
                    END,
                    WildDice = CASE
                        WHEN EXISTS (
                            SELECT 1
                            FROM Characters c
                            INNER JOIN Campaigns cp ON cp.Id = c.CampaignId
                            WHERE c.Id = SkillGroups.CharacterId
                              AND cp.Ruleset = 'D6')
                        THEN 1
                        ELSE 0
                    END,
                    AllowFumble = CASE
                        WHEN EXISTS (
                            SELECT 1
                            FROM Characters c
                            INNER JOIN Campaigns cp ON cp.Id = c.CampaignId
                            WHERE c.Id = SkillGroups.CharacterId
                              AND cp.Ruleset = 'D6')
                        THEN 1
                        ELSE 0
                    END
                WHERE DiceRollDefinition = '';
                """);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "AllowFumble",
                table: "SkillGroups");

            migrationBuilder.DropColumn(
                name: "DiceRollDefinition",
                table: "SkillGroups");

            migrationBuilder.DropColumn(
                name: "WildDice",
                table: "SkillGroups");
        }
    }
}
