using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace FJPSite.Migrations
{
    /// <inheritdoc />
    public partial class CreatePermissions : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_FeatureAction_Action_ActionId",
                table: "FeatureAction");

            migrationBuilder.DropForeignKey(
                name: "FK_FeatureAction_Feature_FeatureId",
                table: "FeatureAction");

            migrationBuilder.DropForeignKey(
                name: "FK_ModuleFeature_Feature_FeatureId",
                table: "ModuleFeature");

            migrationBuilder.DropForeignKey(
                name: "FK_ModuleFeature_Module_ModuleId",
                table: "ModuleFeature");

            migrationBuilder.DropForeignKey(
                name: "FK_RolePermission_AspNetRoles_RoleId",
                table: "RolePermission");

            migrationBuilder.DropForeignKey(
                name: "FK_RolePermission_FeatureAction_FeatureActionId",
                table: "RolePermission");

            migrationBuilder.DropTable(
                name: "Action");

            migrationBuilder.DropPrimaryKey(
                name: "PK_RolePermission",
                table: "RolePermission");

            migrationBuilder.DropPrimaryKey(
                name: "PK_ModuleFeature",
                table: "ModuleFeature");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Module",
                table: "Module");

            migrationBuilder.DropPrimaryKey(
                name: "PK_FeatureAction",
                table: "FeatureAction");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Feature",
                table: "Feature");

            migrationBuilder.RenameTable(
                name: "RolePermission",
                newName: "RolePermissions");

            migrationBuilder.RenameTable(
                name: "ModuleFeature",
                newName: "ModuleFeatures");

            migrationBuilder.RenameTable(
                name: "Module",
                newName: "Modules");

            migrationBuilder.RenameTable(
                name: "FeatureAction",
                newName: "FeatureActions");

            migrationBuilder.RenameTable(
                name: "Feature",
                newName: "Features");

            migrationBuilder.RenameIndex(
                name: "IX_RolePermission_RoleId",
                table: "RolePermissions",
                newName: "IX_RolePermissions_RoleId");

            migrationBuilder.RenameIndex(
                name: "IX_RolePermission_FeatureActionId",
                table: "RolePermissions",
                newName: "IX_RolePermissions_FeatureActionId");

            migrationBuilder.RenameIndex(
                name: "IX_ModuleFeature_ModuleId",
                table: "ModuleFeatures",
                newName: "IX_ModuleFeatures_ModuleId");

            migrationBuilder.RenameIndex(
                name: "IX_ModuleFeature_FeatureId",
                table: "ModuleFeatures",
                newName: "IX_ModuleFeatures_FeatureId");

            migrationBuilder.RenameIndex(
                name: "IX_FeatureAction_FeatureId",
                table: "FeatureActions",
                newName: "IX_FeatureActions_FeatureId");

            migrationBuilder.RenameIndex(
                name: "IX_FeatureAction_ActionId",
                table: "FeatureActions",
                newName: "IX_FeatureActions_ActionId");

            migrationBuilder.AddPrimaryKey(
                name: "PK_RolePermissions",
                table: "RolePermissions",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_ModuleFeatures",
                table: "ModuleFeatures",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Modules",
                table: "Modules",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_FeatureActions",
                table: "FeatureActions",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Features",
                table: "Features",
                column: "Id");

            migrationBuilder.CreateTable(
                name: "Actions",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Actions", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "UserPermissions",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    FeatureActionId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UserPermissions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_UserPermissions_AspNetUsers_UserId",
                        column: x => x.UserId,
                        principalTable: "AspNetUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_UserPermissions_FeatureActions_FeatureActionId",
                        column: x => x.FeatureActionId,
                        principalTable: "FeatureActions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_UserPermissions_FeatureActionId",
                table: "UserPermissions",
                column: "FeatureActionId");

            migrationBuilder.CreateIndex(
                name: "IX_UserPermissions_UserId",
                table: "UserPermissions",
                column: "UserId");

            migrationBuilder.AddForeignKey(
                name: "FK_FeatureActions_Actions_ActionId",
                table: "FeatureActions",
                column: "ActionId",
                principalTable: "Actions",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_FeatureActions_Features_FeatureId",
                table: "FeatureActions",
                column: "FeatureId",
                principalTable: "Features",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_ModuleFeatures_Features_FeatureId",
                table: "ModuleFeatures",
                column: "FeatureId",
                principalTable: "Features",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_ModuleFeatures_Modules_ModuleId",
                table: "ModuleFeatures",
                column: "ModuleId",
                principalTable: "Modules",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RolePermissions_AspNetRoles_RoleId",
                table: "RolePermissions",
                column: "RoleId",
                principalTable: "AspNetRoles",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RolePermissions_FeatureActions_FeatureActionId",
                table: "RolePermissions",
                column: "FeatureActionId",
                principalTable: "FeatureActions",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

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

            migrationBuilder.DropForeignKey(
                name: "FK_FeatureActions_Features_FeatureId",
                table: "FeatureActions");

            migrationBuilder.DropForeignKey(
                name: "FK_ModuleFeatures_Features_FeatureId",
                table: "ModuleFeatures");

            migrationBuilder.DropForeignKey(
                name: "FK_ModuleFeatures_Modules_ModuleId",
                table: "ModuleFeatures");

            migrationBuilder.DropForeignKey(
                name: "FK_RolePermissions_AspNetRoles_RoleId",
                table: "RolePermissions");

            migrationBuilder.DropForeignKey(
                name: "FK_RolePermissions_FeatureActions_FeatureActionId",
                table: "RolePermissions");

            migrationBuilder.DropTable(
                name: "Actions");

            migrationBuilder.DropTable(
                name: "UserPermissions");

            migrationBuilder.DropPrimaryKey(
                name: "PK_RolePermissions",
                table: "RolePermissions");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Modules",
                table: "Modules");

            migrationBuilder.DropPrimaryKey(
                name: "PK_ModuleFeatures",
                table: "ModuleFeatures");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Features",
                table: "Features");

            migrationBuilder.DropPrimaryKey(
                name: "PK_FeatureActions",
                table: "FeatureActions");

            migrationBuilder.RenameTable(
                name: "RolePermissions",
                newName: "RolePermission");

            migrationBuilder.RenameTable(
                name: "Modules",
                newName: "Module");

            migrationBuilder.RenameTable(
                name: "ModuleFeatures",
                newName: "ModuleFeature");

            migrationBuilder.RenameTable(
                name: "Features",
                newName: "Feature");

            migrationBuilder.RenameTable(
                name: "FeatureActions",
                newName: "FeatureAction");

            migrationBuilder.RenameIndex(
                name: "IX_RolePermissions_RoleId",
                table: "RolePermission",
                newName: "IX_RolePermission_RoleId");

            migrationBuilder.RenameIndex(
                name: "IX_RolePermissions_FeatureActionId",
                table: "RolePermission",
                newName: "IX_RolePermission_FeatureActionId");

            migrationBuilder.RenameIndex(
                name: "IX_ModuleFeatures_ModuleId",
                table: "ModuleFeature",
                newName: "IX_ModuleFeature_ModuleId");

            migrationBuilder.RenameIndex(
                name: "IX_ModuleFeatures_FeatureId",
                table: "ModuleFeature",
                newName: "IX_ModuleFeature_FeatureId");

            migrationBuilder.RenameIndex(
                name: "IX_FeatureActions_FeatureId",
                table: "FeatureAction",
                newName: "IX_FeatureAction_FeatureId");

            migrationBuilder.RenameIndex(
                name: "IX_FeatureActions_ActionId",
                table: "FeatureAction",
                newName: "IX_FeatureAction_ActionId");

            migrationBuilder.AddPrimaryKey(
                name: "PK_RolePermission",
                table: "RolePermission",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Module",
                table: "Module",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_ModuleFeature",
                table: "ModuleFeature",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Feature",
                table: "Feature",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_FeatureAction",
                table: "FeatureAction",
                column: "Id");

            migrationBuilder.CreateTable(
                name: "Action",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Action", x => x.Id);
                });

            migrationBuilder.AddForeignKey(
                name: "FK_FeatureAction_Action_ActionId",
                table: "FeatureAction",
                column: "ActionId",
                principalTable: "Action",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_FeatureAction_Feature_FeatureId",
                table: "FeatureAction",
                column: "FeatureId",
                principalTable: "Feature",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_ModuleFeature_Feature_FeatureId",
                table: "ModuleFeature",
                column: "FeatureId",
                principalTable: "Feature",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_ModuleFeature_Module_ModuleId",
                table: "ModuleFeature",
                column: "ModuleId",
                principalTable: "Module",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RolePermission_AspNetRoles_RoleId",
                table: "RolePermission",
                column: "RoleId",
                principalTable: "AspNetRoles",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RolePermission_FeatureAction_FeatureActionId",
                table: "RolePermission",
                column: "FeatureActionId",
                principalTable: "FeatureAction",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
