using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace esuite.Database.SqlServer.Migrations
{
    /// <inheritdoc />
    public partial class DomainAndMailTemplates : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "Domain");

            migrationBuilder.EnsureSchema(
                name: "Mail");

            migrationBuilder.AddColumn<long>(
                name: "DomainId",
                schema: "UserManager",
                table: "Users",
                type: "bigint",
                nullable: false,
                defaultValue: 1L);

            migrationBuilder.CreateTable(
                name: "Domains",
                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),
                    Deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Domains", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "MailTemplates",
                schema: "Mail",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    MailType = table.Column<int>(type: "int", nullable: false),
                    Subject = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    TemplateDefinition = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    DomainId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MailTemplates", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MailTemplates_Domains_DomainId",
                        column: x => x.DomainId,
                        principalSchema: "Domain",
                        principalTable: "Domains",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                schema: "Domain",
                table: "Domains",
                columns: new[] { "Id", "Deleted", "Guid", "Name" },
                values: new object[] { 1L, false, new Guid("4eb1b0c7-e81b-4c9d-ba91-384ed47a1cf9"), "Sun-Strategy" });

            migrationBuilder.UpdateData(
                schema: "UserManager",
                table: "Users",
                keyColumn: "Id",
                keyValue: 1L,
                columns: new[] { "DomainId", "Guid" },
                values: new object[] { 1L, new Guid("b16f9801-5e83-4766-921d-b295b3b8db15") });

            migrationBuilder.InsertData(
                schema: "Mail",
                table: "MailTemplates",
                columns: new[] { "Id", "Deleted", "DomainId", "Guid", "MailType", "Subject", "TemplateDefinition" },
                values: new object[,]
                {
                    { 1L, false, 1L, new Guid("32b657af-5e17-47f3-b8dc-59580516b141"), 0, "Confirmation e-mail", "<h1>Please confirm your e-mail</h1> Welcome to .  <a href=\"$url\">Please click on this link to confirm your e-mail address is correct.</a>" },
                    { 2L, false, 1L, new Guid("77fff2b0-dd8c-43e6-a9fd-304c79850f81"), 2, "Password reset", "<h1>Please follow the link to reset your password</h1><br><a href=\"$url\">Reset Password</a>" },
                    { 3L, false, 1L, new Guid("2e2d84a5-be1c-4e3b-a86a-bdafed42801b"), 1, "Disable two factor authentication", "<h1>Please follow the link to reset two factor authentication</h1><br><a href=\"$url\">Disable two factor authentication</a>" }
                });

            migrationBuilder.CreateIndex(
                name: "IX_Users_DomainId",
                schema: "UserManager",
                table: "Users",
                column: "DomainId");

            migrationBuilder.CreateIndex(
                name: "IX_Domains_Guid",
                schema: "Domain",
                table: "Domains",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Domains_Name",
                schema: "Domain",
                table: "Domains",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_MailTemplates_DomainId_MailType",
                schema: "Mail",
                table: "MailTemplates",
                columns: new[] { "DomainId", "MailType" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_MailTemplates_Guid",
                schema: "Mail",
                table: "MailTemplates",
                column: "Guid",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_Users_Domains_DomainId",
                schema: "UserManager",
                table: "Users",
                column: "DomainId",
                principalSchema: "Domain",
                principalTable: "Domains",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Users_Domains_DomainId",
                schema: "UserManager",
                table: "Users");

            migrationBuilder.DropTable(
                name: "MailTemplates",
                schema: "Mail");

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

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

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

            migrationBuilder.UpdateData(
                schema: "UserManager",
                table: "Users",
                keyColumn: "Id",
                keyValue: 1L,
                column: "Guid",
                value: new Guid("30cfcd5b-3385-43f1-b59a-fd35236f3d92"));
        }
    }
}
