using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace esuite.Database.SqlServer.Migrations
{
    /// <inheritdoc />
    public partial class SsoSupporttables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<long>(
                name: "SsoProviderId",
                schema: "UserManager",
                table: "Users",
                type: "bigint",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "SsoSubject",
                schema: "UserManager",
                table: "Users",
                type: "nvarchar(max)",
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<long>(
                name: "SsoProviderId",
                schema: "Domain",
                table: "Domains",
                type: "bigint",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "SingleUseGuids",
                schema: "UserManager",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Expires = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SingleUseGuids", x => x.Id);
                    table.ForeignKey(
                        name: "FK_SingleUseGuids_Users_UserId",
                        column: x => x.UserId,
                        principalSchema: "UserManager",
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "SsoProviders",
                schema: "UserManager",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    ClientId = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ClientSecret = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ValidIssuer = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    AuthorizationEndpoint = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    TokenEndpoint = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    IsPublic = table.Column<bool>(type: "bit", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SsoProviders", x => x.Id);
                });

            migrationBuilder.UpdateData(
                schema: "Domain",
                table: "Domains",
                keyColumn: "Id",
                keyValue: 1L,
                column: "SsoProviderId",
                value: null);

            migrationBuilder.UpdateData(
                schema: "UserManager",
                table: "Users",
                keyColumn: "Id",
                keyValue: 1L,
                columns: new[] { "SsoProviderId", "SsoSubject" },
                values: new object[] { null, "" });

            migrationBuilder.CreateIndex(
                name: "IX_Users_SsoProviderId",
                schema: "UserManager",
                table: "Users",
                column: "SsoProviderId");

            migrationBuilder.CreateIndex(
                name: "IX_Domains_SsoProviderId",
                schema: "Domain",
                table: "Domains",
                column: "SsoProviderId");

            migrationBuilder.CreateIndex(
                name: "IX_SingleUseGuids_Guid",
                schema: "UserManager",
                table: "SingleUseGuids",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_SingleUseGuids_UserId",
                schema: "UserManager",
                table: "SingleUseGuids",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_SsoProviders_Name",
                schema: "UserManager",
                table: "SsoProviders",
                column: "Name",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_Domains_SsoProviders_SsoProviderId",
                schema: "Domain",
                table: "Domains",
                column: "SsoProviderId",
                principalSchema: "UserManager",
                principalTable: "SsoProviders",
                principalColumn: "Id");

            migrationBuilder.AddForeignKey(
                name: "FK_Users_SsoProviders_SsoProviderId",
                schema: "UserManager",
                table: "Users",
                column: "SsoProviderId",
                principalSchema: "UserManager",
                principalTable: "SsoProviders",
                principalColumn: "Id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Domains_SsoProviders_SsoProviderId",
                schema: "Domain",
                table: "Domains");

            migrationBuilder.DropForeignKey(
                name: "FK_Users_SsoProviders_SsoProviderId",
                schema: "UserManager",
                table: "Users");

            migrationBuilder.DropTable(
                name: "SingleUseGuids",
                schema: "UserManager");

            migrationBuilder.DropTable(
                name: "SsoProviders",
                schema: "UserManager");

            migrationBuilder.DropIndex(
                name: "IX_Users_SsoProviderId",
                schema: "UserManager",
                table: "Users");

            migrationBuilder.DropIndex(
                name: "IX_Domains_SsoProviderId",
                schema: "Domain",
                table: "Domains");

            migrationBuilder.DropColumn(
                name: "SsoProviderId",
                schema: "UserManager",
                table: "Users");

            migrationBuilder.DropColumn(
                name: "SsoSubject",
                schema: "UserManager",
                table: "Users");

            migrationBuilder.DropColumn(
                name: "SsoProviderId",
                schema: "Domain",
                table: "Domains");
        }
    }
}
