using Microsoft.EntityFrameworkCore.Migrations;

namespace ReallifeGamemode.Database.Migrations
{
  public partial class BankAccountRefactor : Migration
  {
    protected override void Up(MigrationBuilder migrationBuilder)
    {
      migrationBuilder.DropForeignKey(
          name: "FK_FactionBankAccounts_Factions_FactionId",
          table: "FactionBankAccounts");

      migrationBuilder.DropForeignKey(
          name: "FK_GroupBankAccounts_Groups_GroupId",
          table: "GroupBankAccounts");

      migrationBuilder.DropForeignKey(
          name: "FK_UserBankAccounts_Users_UserId",
          table: "UserBankAccounts");

      migrationBuilder.DropIndex(
          name: "IX_UserBankAccounts_UserId",
          table: "UserBankAccounts");

      migrationBuilder.DropIndex(
          name: "IX_GroupBankAccounts_GroupId",
          table: "GroupBankAccounts");

      migrationBuilder.DropIndex(
          name: "IX_FactionBankAccounts_FactionId",
          table: "FactionBankAccounts");

      migrationBuilder.AddColumn<int>(
                name: "BankAccountId",
                table: "Users",
                nullable: true);

      migrationBuilder.AddColumn<int>(
          name: "BankAccountId",
          table: "Groups",
          nullable: true);

      migrationBuilder.AddColumn<int>(
          name: "BankAccountId",
          table: "Factions",
          nullable: true);

      migrationBuilder.Sql(@"
UPDATE users u
INNER JOIN userbankaccounts ub ON ub.userid = u.id
SET u.bankaccountid = ub.id");

      migrationBuilder.Sql(@"
UPDATE factions f
INNER JOIN factionbankaccounts fb ON fb.factionid = f.id
SET f.bankaccountid = fb.id");

      migrationBuilder.Sql(@"
UPDATE groups g
INNER JOIN groupbankaccounts gb ON gb.groupid = g.id
SET g.bankaccountid = gb.id");

      migrationBuilder.DropColumn(
                name: "UserId",
                table: "UserBankAccounts");

      migrationBuilder.DropColumn(
          name: "GroupId",
          table: "GroupBankAccounts");

      migrationBuilder.DropColumn(
          name: "FactionId",
          table: "FactionBankAccounts");

      migrationBuilder.CreateIndex(
          name: "IX_Users_BankAccountId",
          table: "Users",
          column: "BankAccountId");

      migrationBuilder.CreateIndex(
          name: "IX_Groups_BankAccountId",
          table: "Groups",
          column: "BankAccountId");

      migrationBuilder.CreateIndex(
          name: "IX_Factions_BankAccountId",
          table: "Factions",
          column: "BankAccountId");

      migrationBuilder.AddForeignKey(
          name: "FK_Factions_FactionBankAccounts_BankAccountId",
          table: "Factions",
          column: "BankAccountId",
          principalTable: "FactionBankAccounts",
          principalColumn: "Id",
          onDelete: ReferentialAction.Restrict);

      migrationBuilder.AddForeignKey(
          name: "FK_Groups_GroupBankAccounts_BankAccountId",
          table: "Groups",
          column: "BankAccountId",
          principalTable: "GroupBankAccounts",
          principalColumn: "Id",
          onDelete: ReferentialAction.Restrict);

      migrationBuilder.AddForeignKey(
          name: "FK_Users_UserBankAccounts_BankAccountId",
          table: "Users",
          column: "BankAccountId",
          principalTable: "UserBankAccounts",
          principalColumn: "Id",
          onDelete: ReferentialAction.Restrict);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
      migrationBuilder.DropForeignKey(
          name: "FK_Factions_FactionBankAccounts_BankAccountId",
          table: "Factions");

      migrationBuilder.DropForeignKey(
          name: "FK_Groups_GroupBankAccounts_BankAccountId",
          table: "Groups");

      migrationBuilder.DropForeignKey(
          name: "FK_Users_UserBankAccounts_BankAccountId",
          table: "Users");

      migrationBuilder.DropIndex(
          name: "IX_Users_BankAccountId",
          table: "Users");

      migrationBuilder.DropIndex(
          name: "IX_Groups_BankAccountId",
          table: "Groups");

      migrationBuilder.DropIndex(
          name: "IX_Factions_BankAccountId",
          table: "Factions");

      migrationBuilder.DropColumn(
          name: "BankAccountId",
          table: "Users");

      migrationBuilder.DropColumn(
          name: "BankAccountId",
          table: "Groups");

      migrationBuilder.DropColumn(
          name: "BankAccountId",
          table: "Factions");

      migrationBuilder.AddColumn<int>(
          name: "UserId",
          table: "UserBankAccounts",
          nullable: false,
          defaultValue: 0);

      migrationBuilder.AddColumn<int>(
          name: "GroupId",
          table: "GroupBankAccounts",
          nullable: true);

      migrationBuilder.AddColumn<int>(
          name: "FactionId",
          table: "FactionBankAccounts",
          nullable: false,
          defaultValue: 0);

      migrationBuilder.CreateIndex(
          name: "IX_UserBankAccounts_UserId",
          table: "UserBankAccounts",
          column: "UserId");

      migrationBuilder.CreateIndex(
          name: "IX_GroupBankAccounts_GroupId",
          table: "GroupBankAccounts",
          column: "GroupId");

      migrationBuilder.CreateIndex(
          name: "IX_FactionBankAccounts_FactionId",
          table: "FactionBankAccounts",
          column: "FactionId");

      migrationBuilder.AddForeignKey(
          name: "FK_FactionBankAccounts_Factions_FactionId",
          table: "FactionBankAccounts",
          column: "FactionId",
          principalTable: "Factions",
          principalColumn: "Id",
          onDelete: ReferentialAction.Cascade);

      migrationBuilder.AddForeignKey(
          name: "FK_GroupBankAccounts_Groups_GroupId",
          table: "GroupBankAccounts",
          column: "GroupId",
          principalTable: "Groups",
          principalColumn: "Id",
          onDelete: ReferentialAction.Restrict);

      migrationBuilder.AddForeignKey(
          name: "FK_UserBankAccounts_Users_UserId",
          table: "UserBankAccounts",
          column: "UserId",
          principalTable: "Users",
          principalColumn: "Id",
          onDelete: ReferentialAction.Cascade);
    }
  }
}
