using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Knots.Migrations
{
    /// <inheritdoc />
    public partial class RemoveDiscussionFromGroup : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Groups_Discussions_DiscussionId",
                table: "Groups");

            migrationBuilder.DropForeignKey(
                name: "FK_UserDiscussions_Users_UserId",
                table: "UserDiscussions");

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

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

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

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

            migrationBuilder.CreateTable(
                name: "GroupUsers",
                columns: table => new
                {
                    GroupId = table.Column<int>(type: "int", nullable: false),
                    UserId = table.Column<int>(type: "int", nullable: false),
                    RoleId = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GroupUsers", x => new { x.GroupId, x.UserId });
                    table.ForeignKey(
                        name: "FK_GroupUsers_Groups_GroupId",
                        column: x => x.GroupId,
                        principalTable: "Groups",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_GroupUsers_Roles_RoleId",
                        column: x => x.RoleId,
                        principalTable: "Roles",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_GroupUsers_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Discussions_GroupId",
                table: "Discussions",
                column: "GroupId");

            migrationBuilder.CreateIndex(
                name: "IX_GroupUsers_RoleId",
                table: "GroupUsers",
                column: "RoleId");

            migrationBuilder.CreateIndex(
                name: "IX_GroupUsers_UserId",
                table: "GroupUsers",
                column: "UserId");

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

            migrationBuilder.AddForeignKey(
                name: "FK_UserDiscussions_Users_UserId",
                table: "UserDiscussions",
                column: "UserId",
                principalTable: "Users",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

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

            migrationBuilder.DropForeignKey(
                name: "FK_UserDiscussions_Users_UserId",
                table: "UserDiscussions");

            migrationBuilder.DropTable(
                name: "GroupUsers");

            migrationBuilder.DropIndex(
                name: "IX_Discussions_GroupId",
                table: "Discussions");

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

            migrationBuilder.AddColumn<int>(
                name: "DiscussionId",
                table: "Groups",
                type: "int",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<int>(
                name: "KeyId",
                table: "Groups",
                type: "int",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.CreateIndex(
                name: "IX_Groups_DiscussionId",
                table: "Groups",
                column: "DiscussionId",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_Groups_Discussions_DiscussionId",
                table: "Groups",
                column: "DiscussionId",
                principalTable: "Discussions",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

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