using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace esuite.Database.SqlServer.Migrations
{
    /// <inheritdoc />
    public partial class Roles : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Roles",
                schema: "Domain",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    IsSuperUser = table.Column<bool>(type: "bit", nullable: false),
                    IsAdministrator = table.Column<bool>(type: "bit", nullable: false),
                    CanDelete = table.Column<bool>(type: "bit", nullable: false),
                    DomainId = table.Column<long>(type: "bigint", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Roles", x => x.Id);
                    table.UniqueConstraint("AK_Roles_DomainId_Name", x => new { x.DomainId, x.Name });
                    table.ForeignKey(
                        name: "FK_Roles_Domains_DomainId",
                        column: x => x.DomainId,
                        principalSchema: "Domain",
                        principalTable: "Domains",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "RoleAccess",
                schema: "Domain",
                columns: table => new
                {
                    RoleId = table.Column<long>(type: "bigint", nullable: false),
                    AccessKey = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_RoleAccess", x => new { x.RoleId, x.AccessKey });
                    table.ForeignKey(
                        name: "FK_RoleAccess_Roles_RoleId",
                        column: x => x.RoleId,
                        principalSchema: "Domain",
                        principalTable: "Roles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "UserRoles",
                schema: "Domain",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    RoleId = table.Column<long>(type: "bigint", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UserRoles", x => x.Id);
                    table.UniqueConstraint("AK_UserRoles_UserId_RoleId", x => new { x.UserId, x.RoleId });
                    table.ForeignKey(
                        name: "FK_UserRoles_Roles_RoleId",
                        column: x => x.RoleId,
                        principalSchema: "Domain",
                        principalTable: "Roles",
                        principalColumn: "Id",
                        onUpdate: ReferentialAction.NoAction,
                        onDelete: ReferentialAction.NoAction);
                    table.ForeignKey(
                        name: "FK_UserRoles_Users_UserId",
                        column: x => x.UserId,
                        principalSchema: "UserManager",
                        principalTable: "Users",
                        principalColumn: "Id",
                        onUpdate: ReferentialAction.NoAction,
                        onDelete: ReferentialAction.NoAction);
                });

            migrationBuilder.InsertData(
                schema: "Domain",
                table: "Roles",
                columns: ["Id", "CanDelete", "Deleted", "DomainId", "Guid", "IsAdministrator", "IsSuperUser", "Name"],
                values: new object[,]
                {
                    { 1L, false, false, 1L, new Guid("32d65817-4de4-4bd5-912e-2b33054a2171"), false, true, "Super Users" },
                    { 2L, false, false, 1L, new Guid("2f77f57c-fe2c-4a36-ad17-5d902afc86cb"), true, false, "Administrators" }
                });

            migrationBuilder.UpdateData(
                schema: "UserManager",
                table: "Users",
                keyColumn: "Id",
                keyValue: 1L,
                column: "Guid",
                value: new Guid("0a30fec6-bcb8-40cb-b0af-0b5ba711c8d0"));

            migrationBuilder.InsertData(
                schema: "Domain",
                table: "UserRoles",
                columns: ["Id", "Deleted", "RoleId", "UserId"],
                values: [1L, false, 1L, 1L]);

            migrationBuilder.CreateIndex(
                name: "IX_Roles_Guid",
                schema: "Domain",
                table: "Roles",
                column: "Guid",
                unique: true);
            
            migrationBuilder.CreateIndex(
                name: "IX_UserRoles_RoleId",
                schema: "Domain",
                table: "UserRoles",
                column: "RoleId");

            migrationBuilder.Sql(
                "create or alter view Domain.UserAccess with schemabinding as\r\nselect\r\n\tur.UserId,\r\n\tra.AccessKey,\r\n\tr.DomainId\r\nfrom [Domain].[UserRoles] as ur\r\njoin UserManager.Users as u\r\n\ton u.Id = ur.UserId \r\n\tand u.Active = 1\r\njoin [Domain].[Roles] as r\r\n\ton r.Id = ur.RoleId\r\n\tand r.Deleted = 0\r\njoin [Domain].[RoleAccess] as ra\r\n\ton ra.RoleId = ur.RoleId");

            migrationBuilder.Sql(
                "create unique clustered index IX_UserAccess_UserId_AccessKey_DomainId on Domain.UserAccess( UserId, AccessKey, DomainId )");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.Sql("drop index IX_UserAccess_UserId_RoleId_AccessKey on Domain.UserAccess");

            migrationBuilder.Sql("drop view Domain.UserAccess");

            migrationBuilder.DropTable(
                name: "RoleAccess",
                schema: "Domain");
            
            migrationBuilder.DropTable(
                name: "UserRoles",
                schema: "Domain");

            migrationBuilder.DropTable(
                name: "Roles",
                schema: "Domain");

            migrationBuilder.UpdateData(
                schema: "UserManager",
                table: "Users",
                keyColumn: "Id",
                keyValue: 1L,
                column: "Guid",
                value: new Guid("b16f9801-5e83-4766-921d-b295b3b8db15"));
        }
    }
}
