using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Backend.Migrations
{
    /// <inheritdoc />
    public partial class AddAwardResultCategoryLock : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_Results_SeasonId",
                table: "Results");

            migrationBuilder.AddColumn<int>(
                name: "CategoryId",
                table: "Results",
                type: "integer",
                nullable: true);

            migrationBuilder.Sql(
                """
                UPDATE "Results" AS r
                SET "CategoryId" = c."Id"
                FROM "Categories" AS c
                WHERE r."SeasonId" = c."SeasonId"
                  AND lower(trim(r."CategoryName")) = lower(trim(c."Name"));

                DO $$
                BEGIN
                    IF EXISTS (SELECT 1 FROM "Results" WHERE "CategoryId" IS NULL) THEN
                        RAISE EXCEPTION 'Could not backfill CategoryId for one or more rows in Results.';
                    END IF;
                END $$;
                """);

            migrationBuilder.AlterColumn<int>(
                name: "CategoryId",
                table: "Results",
                type: "integer",
                nullable: false,
                oldClrType: typeof(int),
                oldType: "integer",
                oldNullable: true);

            migrationBuilder.CreateIndex(
                name: "IX_Results_CategoryId",
                table: "Results",
                column: "CategoryId");

            migrationBuilder.CreateIndex(
                name: "IX_Results_SeasonId_CategoryId",
                table: "Results",
                columns: new[] { "SeasonId", "CategoryId" },
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_Results_Categories_CategoryId",
                table: "Results",
                column: "CategoryId",
                principalTable: "Categories",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Results_Categories_CategoryId",
                table: "Results");

            migrationBuilder.DropIndex(
                name: "IX_Results_CategoryId",
                table: "Results");

            migrationBuilder.DropIndex(
                name: "IX_Results_SeasonId_CategoryId",
                table: "Results");

            migrationBuilder.DropColumn(
                name: "CategoryId",
                table: "Results");

            migrationBuilder.CreateIndex(
                name: "IX_Results_SeasonId",
                table: "Results",
                column: "SeasonId");
        }
    }
}
