using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Ablera.Serdica.DBModels.Oidc.Migrations.Migrations
{
    /// <inheritdoc />
    public partial class InitialOpenIddictMigration : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "SRD_SYS");

            migrationBuilder.CreateTable(
                name: "OIDC_APPLICATIONS",
                schema: "SRD_SYS",
                columns: table => new
                {
                    Id = table.Column<string>(type: "NVARCHAR2(450)", nullable: false),
                    ApplicationType = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true),
                    ClientId = table.Column<string>(type: "NVARCHAR2(100)", maxLength: 100, nullable: true),
                    ClientSecret = table.Column<string>(type: "NVARCHAR2(256)", maxLength: 256, nullable: true),
                    ClientType = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true),
                    ConcurrencyToken = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true),
                    ConsentType = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true),
                    DisplayName = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    DisplayNames = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    JsonWebKeySet = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    Permissions = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    PostLogoutRedirectUris = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    Properties = table.Column<string>(type: "NVARCHAR2(4000)", nullable: true),
                    RedirectUris = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    Requirements = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    Settings = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OIDC_APPLICATIONS", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "OIDC_SCOPES",
                schema: "SRD_SYS",
                columns: table => new
                {
                    Id = table.Column<string>(type: "NVARCHAR2(450)", nullable: false),
                    ConcurrencyToken = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true),
                    Description = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    Descriptions = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    DisplayName = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    DisplayNames = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    Name = table.Column<string>(type: "NVARCHAR2(200)", maxLength: 200, nullable: true),
                    Properties = table.Column<string>(type: "NVARCHAR2(4000)", nullable: true),
                    Resources = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OIDC_SCOPES", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "OIDC_AUTHORIZATIONS",
                schema: "SRD_SYS",
                columns: table => new
                {
                    Id = table.Column<string>(type: "NVARCHAR2(450)", nullable: false),
                    ApplicationId = table.Column<string>(type: "NVARCHAR2(450)", nullable: true),
                    ConcurrencyToken = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true),
                    CreationDate = table.Column<DateTime>(type: "TIMESTAMP(7)", nullable: true),
                    Properties = table.Column<string>(type: "NVARCHAR2(4000)", nullable: true),
                    Scopes = table.Column<string>(type: "NVARCHAR2(2000)", nullable: true),
                    Status = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true),
                    Subject = table.Column<string>(type: "NVARCHAR2(400)", maxLength: 400, nullable: true),
                    Type = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OIDC_AUTHORIZATIONS", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OIDC_AUTHORIZATIONS_OIDC_APPLICATIONS_ApplicationId",
                        column: x => x.ApplicationId,
                        principalSchema: "SRD_SYS",
                        principalTable: "OIDC_APPLICATIONS",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateTable(
                name: "OIDC_TOKENS",
                schema: "SRD_SYS",
                columns: table => new
                {
                    Id = table.Column<string>(type: "NVARCHAR2(450)", nullable: false),
                    ApplicationId = table.Column<string>(type: "NVARCHAR2(450)", nullable: true),
                    AuthorizationId = table.Column<string>(type: "NVARCHAR2(450)", nullable: true),
                    ConcurrencyToken = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true),
                    CreationDate = table.Column<DateTime>(type: "TIMESTAMP(7)", nullable: true),
                    ExpirationDate = table.Column<DateTime>(type: "TIMESTAMP(7)", nullable: true),
                    Payload = table.Column<string>(type: "CLOB", nullable: true),
                    Properties = table.Column<string>(type: "VARCHAR2(4000)", nullable: true),
                    RedemptionDate = table.Column<DateTime>(type: "TIMESTAMP(7)", nullable: true),
                    ReferenceId = table.Column<string>(type: "NVARCHAR2(100)", maxLength: 100, nullable: true),
                    Status = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true),
                    Subject = table.Column<string>(type: "NVARCHAR2(400)", maxLength: 400, nullable: true),
                    Type = table.Column<string>(type: "NVARCHAR2(50)", maxLength: 50, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OIDC_TOKENS", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OIDC_TOKENS_OIDC_APPLICATIONS_ApplicationId",
                        column: x => x.ApplicationId,
                        principalSchema: "SRD_SYS",
                        principalTable: "OIDC_APPLICATIONS",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_OIDC_TOKENS_OIDC_AUTHORIZATIONS_AuthorizationId",
                        column: x => x.AuthorizationId,
                        principalSchema: "SRD_SYS",
                        principalTable: "OIDC_AUTHORIZATIONS",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateIndex(
                name: "IX_OIDC_APPLICATIONS_ClientId",
                schema: "SRD_SYS",
                table: "OIDC_APPLICATIONS",
                column: "ClientId",
                unique: true,
                filter: "\"ClientId\" IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_OIDC_AUTHORIZATIONS_ApplicationId_Status_Subject_Type",
                schema: "SRD_SYS",
                table: "OIDC_AUTHORIZATIONS",
                columns: new[] { "ApplicationId", "Status", "Subject", "Type" });

            migrationBuilder.CreateIndex(
                name: "IX_OIDC_SCOPES_Name",
                schema: "SRD_SYS",
                table: "OIDC_SCOPES",
                column: "Name",
                unique: true,
                filter: "\"Name\" IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_OIDC_TOKENS_ApplicationId_Status_Subject_Type",
                schema: "SRD_SYS",
                table: "OIDC_TOKENS",
                columns: new[] { "ApplicationId", "Status", "Subject", "Type" });

            migrationBuilder.CreateIndex(
                name: "IX_OIDC_TOKENS_AuthorizationId",
                schema: "SRD_SYS",
                table: "OIDC_TOKENS",
                column: "AuthorizationId");

            migrationBuilder.CreateIndex(
                name: "IX_OIDC_TOKENS_ReferenceId",
                schema: "SRD_SYS",
                table: "OIDC_TOKENS",
                column: "ReferenceId",
                unique: true,
                filter: "\"ReferenceId\" IS NOT NULL");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "OIDC_SCOPES",
                schema: "SRD_SYS");

            migrationBuilder.DropTable(
                name: "OIDC_TOKENS",
                schema: "SRD_SYS");

            migrationBuilder.DropTable(
                name: "OIDC_AUTHORIZATIONS",
                schema: "SRD_SYS");

            migrationBuilder.DropTable(
                name: "OIDC_APPLICATIONS",
                schema: "SRD_SYS");
        }
    }
}
