using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Backend.Migrations
{
    /// <inheritdoc />
    public partial class AddNominationGroupTrackerIdentity : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Nominations_Categories_CategoryId",
                table: "Nominations");

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

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

            migrationBuilder.AddColumn<string>(
                name: "CategoryGroupName",
                table: "Nominations",
                type: "character varying(80)",
                maxLength: 80,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "ResolvedChannel",
                table: "Nominations",
                type: "character varying(120)",
                maxLength: 120,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "ResolvedPlatform",
                table: "Nominations",
                type: "character varying(40)",
                maxLength: 40,
                nullable: true);

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

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

            migrationBuilder.AddColumn<DateTimeOffset>(
                name: "TrackerCheckedAt",
                table: "Nominations",
                type: "timestamp with time zone",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "TrackerStatus",
                table: "Nominations",
                type: "character varying(40)",
                maxLength: 40,
                nullable: false,
                defaultValue: "pending");

            migrationBuilder.AddColumn<int>(
                name: "NominationTally",
                table: "Candidates",
                type: "integer",
                nullable: false,
                defaultValue: 0);

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

            migrationBuilder.CreateTable(
                name: "StreamerIdentities",
                columns: table => new
                {
                    Id = table.Column<int>(type: "integer", nullable: false)
                        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
                    Platform = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    Login = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: false),
                    NormalizedKey = table.Column<string>(type: "character varying(180)", maxLength: 180, nullable: false),
                    DisplayName = table.Column<string>(type: "character varying(120)", maxLength: 120, nullable: false),
                    ProfileUrl = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    LastResolvedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_StreamerIdentities", x => x.Id);
                });

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 1,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 2,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 3,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 4,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 5,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 6,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 7,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 8,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 9,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 10,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 11,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 12,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Candidates",
                keyColumn: "Id",
                keyValue: 13,
                column: "StreamerIdentityId",
                value: null);

            migrationBuilder.UpdateData(
                table: "Nominations",
                keyColumn: "Id",
                keyValue: 1,
                columns: new[] { "AvgViewers", "CategoryGroupName", "ResolvedChannel", "ResolvedPlatform", "StreamerIdentityId", "SuggestedCategoryId", "TrackerCheckedAt", "TrackerStatus" },
                values: new object[] { null, "", null, null, null, null, null, "pending" });

            migrationBuilder.UpdateData(
                table: "Nominations",
                keyColumn: "Id",
                keyValue: 2,
                columns: new[] { "AvgViewers", "CategoryGroupName", "ResolvedChannel", "ResolvedPlatform", "StreamerIdentityId", "SuggestedCategoryId", "TrackerCheckedAt", "TrackerStatus" },
                values: new object[] { null, "", null, null, null, null, null, "pending" });

            migrationBuilder.Sql("""
                UPDATE "Nominations" n
                SET "CategoryGroupName" = c."GroupName"
                FROM "Categories" c
                WHERE n."CategoryId" = c."Id"
                  AND COALESCE(n."CategoryGroupName", '') = '';
                """);

            migrationBuilder.CreateIndex(
                name: "IX_Nominations_SeasonId_CategoryGroupName_Status",
                table: "Nominations",
                columns: new[] { "SeasonId", "CategoryGroupName", "Status" });

            migrationBuilder.CreateIndex(
                name: "IX_Nominations_SeasonId_StreamerIdentityId_CategoryGroupName",
                table: "Nominations",
                columns: new[] { "SeasonId", "StreamerIdentityId", "CategoryGroupName" });

            migrationBuilder.CreateIndex(
                name: "IX_Nominations_StreamerIdentityId",
                table: "Nominations",
                column: "StreamerIdentityId");

            migrationBuilder.CreateIndex(
                name: "IX_Nominations_SuggestedCategoryId",
                table: "Nominations",
                column: "SuggestedCategoryId");

            migrationBuilder.CreateIndex(
                name: "IX_Candidates_StreamerIdentityId",
                table: "Candidates",
                column: "StreamerIdentityId");

            migrationBuilder.CreateIndex(
                name: "IX_StreamerIdentities_NormalizedKey",
                table: "StreamerIdentities",
                column: "NormalizedKey",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_Candidates_StreamerIdentities_StreamerIdentityId",
                table: "Candidates",
                column: "StreamerIdentityId",
                principalTable: "StreamerIdentities",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_Nominations_Categories_CategoryId",
                table: "Nominations",
                column: "CategoryId",
                principalTable: "Categories",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);

            migrationBuilder.AddForeignKey(
                name: "FK_Nominations_Categories_SuggestedCategoryId",
                table: "Nominations",
                column: "SuggestedCategoryId",
                principalTable: "Categories",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);

            migrationBuilder.AddForeignKey(
                name: "FK_Nominations_StreamerIdentities_StreamerIdentityId",
                table: "Nominations",
                column: "StreamerIdentityId",
                principalTable: "StreamerIdentities",
                principalColumn: "Id");
        }

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

            migrationBuilder.DropForeignKey(
                name: "FK_Nominations_Categories_CategoryId",
                table: "Nominations");

            migrationBuilder.DropForeignKey(
                name: "FK_Nominations_Categories_SuggestedCategoryId",
                table: "Nominations");

            migrationBuilder.DropForeignKey(
                name: "FK_Nominations_StreamerIdentities_StreamerIdentityId",
                table: "Nominations");

            migrationBuilder.DropTable(
                name: "StreamerIdentities");

            migrationBuilder.DropIndex(
                name: "IX_Nominations_SeasonId_CategoryGroupName_Status",
                table: "Nominations");

            migrationBuilder.DropIndex(
                name: "IX_Nominations_SeasonId_StreamerIdentityId_CategoryGroupName",
                table: "Nominations");

            migrationBuilder.DropIndex(
                name: "IX_Nominations_StreamerIdentityId",
                table: "Nominations");

            migrationBuilder.DropIndex(
                name: "IX_Nominations_SuggestedCategoryId",
                table: "Nominations");

            migrationBuilder.DropIndex(
                name: "IX_Candidates_StreamerIdentityId",
                table: "Candidates");

            migrationBuilder.DropColumn(
                name: "AvgViewers",
                table: "Nominations");

            migrationBuilder.DropColumn(
                name: "CategoryGroupName",
                table: "Nominations");

            migrationBuilder.DropColumn(
                name: "ResolvedChannel",
                table: "Nominations");

            migrationBuilder.DropColumn(
                name: "ResolvedPlatform",
                table: "Nominations");

            migrationBuilder.DropColumn(
                name: "StreamerIdentityId",
                table: "Nominations");

            migrationBuilder.DropColumn(
                name: "SuggestedCategoryId",
                table: "Nominations");

            migrationBuilder.DropColumn(
                name: "TrackerCheckedAt",
                table: "Nominations");

            migrationBuilder.DropColumn(
                name: "TrackerStatus",
                table: "Nominations");

            migrationBuilder.DropColumn(
                name: "NominationTally",
                table: "Candidates");

            migrationBuilder.DropColumn(
                name: "StreamerIdentityId",
                table: "Candidates");

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

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