using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

            migrationBuilder.AddColumn<bool>(
                name: "TwitchAuthManagedByDatabase",
                table: "SiteSettings",
                type: "boolean",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<string>(
                name: "TwitchClientId",
                table: "SiteSettings",
                type: "character varying(120)",
                maxLength: 120,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "TwitchClientSecret",
                table: "SiteSettings",
                type: "character varying(180)",
                maxLength: 180,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "TwitchRedirectUri",
                table: "SiteSettings",
                type: "character varying(400)",
                maxLength: 400,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "TwitchScope",
                table: "SiteSettings",
                type: "character varying(300)",
                maxLength: 300,
                nullable: false,
                defaultValue: "");

            migrationBuilder.UpdateData(
                table: "SiteSettings",
                keyColumn: "Id",
                keyValue: 1,
                columns: new[] { "TwitchAuthManagedByDatabase", "TwitchClientId", "TwitchClientSecret", "TwitchRedirectUri", "TwitchScope" },
                values: new object[] { false, "", "", "", "" });

            migrationBuilder.Sql("""
                DELETE FROM "VoteEntries"
                WHERE "BallotId" IN (
                    SELECT "Id"
                    FROM (
                        SELECT
                            "Id",
                            ROW_NUMBER() OVER (
                                PARTITION BY "SeasonId", "SubmittedByTwitchId"
                                ORDER BY "SubmittedAt" DESC, "Id" DESC
                            ) AS duplicate_rank
                        FROM "VoteBallots"
                    ) ranked_ballots
                    WHERE duplicate_rank > 1
                );

                DELETE FROM "VoteBallots"
                WHERE "Id" IN (
                    SELECT "Id"
                    FROM (
                        SELECT
                            "Id",
                            ROW_NUMBER() OVER (
                                PARTITION BY "SeasonId", "SubmittedByTwitchId"
                                ORDER BY "SubmittedAt" DESC, "Id" DESC
                            ) AS duplicate_rank
                        FROM "VoteBallots"
                    ) ranked_ballots
                    WHERE duplicate_rank > 1
                );
                """);

            migrationBuilder.CreateIndex(
                name: "IX_VoteBallots_SeasonId_SubmittedByTwitchId",
                table: "VoteBallots",
                columns: new[] { "SeasonId", "SubmittedByTwitchId" },
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropIndex(
                name: "IX_VoteBallots_SeasonId_SubmittedByTwitchId",
                table: "VoteBallots");

            migrationBuilder.DropColumn(
                name: "TwitchAuthManagedByDatabase",
                table: "SiteSettings");

            migrationBuilder.DropColumn(
                name: "TwitchClientId",
                table: "SiteSettings");

            migrationBuilder.DropColumn(
                name: "TwitchClientSecret",
                table: "SiteSettings");

            migrationBuilder.DropColumn(
                name: "TwitchRedirectUri",
                table: "SiteSettings");

            migrationBuilder.DropColumn(
                name: "TwitchScope",
                table: "SiteSettings");

            migrationBuilder.CreateIndex(
                name: "IX_VoteBallots_SeasonId",
                table: "VoteBallots",
                column: "SeasonId");
        }
    }
}
