// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Server.DB.Migrations;

/// <inheritdoc />
public partial class FixEntityRelationships : Migration
{
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(
            name: "FK_Accounts_GuildMember_GuildMemberId",
            table: "Accounts");

        migrationBuilder.DropForeignKey(
            name: "FK_Characters_Guilds_GuildId",
            table: "Characters");

        migrationBuilder.DropIndex(
            name: "IX_GuildMember_CharacterId",
            table: "GuildMember");

        migrationBuilder.DropIndex(
            name: "IX_Characters_GuildId",
            table: "Characters");

        migrationBuilder.DropIndex(
            name: "IX_Accounts_GuildMemberId",
            table: "Accounts");

        migrationBuilder.DropColumn(
            name: "GuildId",
            table: "Characters");

        migrationBuilder.DropColumn(
            name: "GuildMemberId",
            table: "Accounts");

        migrationBuilder.AlterColumn<Guid>(
            name: "CharacterId",
            table: "InventoryItems",
            type: "uuid",
            nullable: false,
            defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
            oldClrType: typeof(Guid),
            oldType: "uuid",
            oldNullable: true);

        migrationBuilder.AlterColumn<Guid>(
            name: "GuildId",
            table: "GuildMember",
            type: "uuid",
            nullable: false,
            defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
            oldClrType: typeof(Guid),
            oldType: "uuid",
            oldNullable: true);

        migrationBuilder.AlterColumn<Guid>(
            name: "CharacterId",
            table: "GuildMember",
            type: "uuid",
            nullable: false,
            defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
            oldClrType: typeof(Guid),
            oldType: "uuid",
            oldNullable: true);

        migrationBuilder.AlterColumn<Guid>(
            name: "AccountId",
            table: "Characters",
            type: "uuid",
            nullable: false,
            defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
            oldClrType: typeof(Guid),
            oldType: "uuid",
            oldNullable: true);

        migrationBuilder.CreateIndex(
            name: "IX_GuildMember_CharacterId",
            table: "GuildMember",
            column: "CharacterId",
            unique: true);
    }

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

        migrationBuilder.AlterColumn<Guid>(
            name: "CharacterId",
            table: "InventoryItems",
            type: "uuid",
            nullable: true,
            oldClrType: typeof(Guid),
            oldType: "uuid");

        migrationBuilder.AlterColumn<Guid>(
            name: "GuildId",
            table: "GuildMember",
            type: "uuid",
            nullable: true,
            oldClrType: typeof(Guid),
            oldType: "uuid");

        migrationBuilder.AlterColumn<Guid>(
            name: "CharacterId",
            table: "GuildMember",
            type: "uuid",
            nullable: true,
            oldClrType: typeof(Guid),
            oldType: "uuid");

        migrationBuilder.AlterColumn<Guid>(
            name: "AccountId",
            table: "Characters",
            type: "uuid",
            nullable: true,
            oldClrType: typeof(Guid),
            oldType: "uuid");

        migrationBuilder.AddColumn<Guid>(
            name: "GuildId",
            table: "Characters",
            type: "uuid",
            nullable: true);

        migrationBuilder.AddColumn<Guid>(
            name: "GuildMemberId",
            table: "Accounts",
            type: "uuid",
            nullable: true);

        migrationBuilder.CreateIndex(
            name: "IX_GuildMember_CharacterId",
            table: "GuildMember",
            column: "CharacterId");

        migrationBuilder.CreateIndex(
            name: "IX_Characters_GuildId",
            table: "Characters",
            column: "GuildId");

        migrationBuilder.CreateIndex(
            name: "IX_Accounts_GuildMemberId",
            table: "Accounts",
            column: "GuildMemberId");

        migrationBuilder.AddForeignKey(
            name: "FK_Accounts_GuildMember_GuildMemberId",
            table: "Accounts",
            column: "GuildMemberId",
            principalTable: "GuildMember",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);

        migrationBuilder.AddForeignKey(
            name: "FK_Characters_Guilds_GuildId",
            table: "Characters",
            column: "GuildId",
            principalTable: "Guilds",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
    }
}