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 Initialtablestructure : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "Audit");

            migrationBuilder.EnsureSchema(
                name: "Contacts");

            migrationBuilder.EnsureSchema(
                name: "CustomFields");

            migrationBuilder.EnsureSchema(
                name: "UserManager");

            migrationBuilder.EnsureSchema(
                name: "Sentinel");

            migrationBuilder.EnsureSchema(
                name: "Forms");

            migrationBuilder.EnsureSchema(
                name: "Glossaries");

            migrationBuilder.EnsureSchema(
                name: "Printer");

            migrationBuilder.EnsureSchema(
                name: "Diagnostics");

            migrationBuilder.EnsureSchema(
                name: "Sequences");

            migrationBuilder.CreateTable(
                name: "AuditDetails",
                schema: "Audit",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<long>(type: "bigint", nullable: false),
                    UserDisplayName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Type = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    DateTime = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    Fields = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Comment = table.Column<string>(type: "nvarchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AuditDetails", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Contacts",
                schema: "Contacts",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    JCard = table.Column<string>(type: "nvarchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Contacts", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "CustomFields",
                schema: "CustomFields",
                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),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    FieldType = table.Column<int>(type: "int", nullable: false),
                    MinEntries = table.Column<long>(type: "bigint", nullable: false),
                    MaxEntries = table.Column<long>(type: "bigint", nullable: true),
                    DefaultValue = table.Column<string>(type: "nvarchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_CustomFields", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "FailedAccessAttempts",
                schema: "Sentinel",
                columns: table => new
                {
                    IPAddress = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    AttemptedTime = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_FailedAccessAttempts", x => new { x.IPAddress, x.AttemptedTime });
                });

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

            migrationBuilder.CreateTable(
                name: "Glossaries",
                schema: "Glossaries",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ParentId = table.Column<long>(type: "bigint", nullable: true),
                    Name = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Glossaries", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Glossaries_Glossaries_ParentId",
                        column: x => x.ParentId,
                        principalSchema: "Glossaries",
                        principalTable: "Glossaries",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateTable(
                name: "Organisations",
                schema: "Printer",
                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(max)", nullable: false),
                    Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Organisations", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "PerformanceReports",
                schema: "Diagnostics",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Host = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ControllerName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ActionName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    RequestType = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ActionParameters = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    StartDateTime = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    Timings = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    TotalTimeMS = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PerformanceReports", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "PerformanceThresholds",
                schema: "Diagnostics",
                columns: table => new
                {
                    ControllerName = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    ActionName = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    RequestType = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    TotalTimeMS = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PerformanceThresholds", x => new { x.ControllerName, x.ActionName, x.RequestType });
                });

            migrationBuilder.CreateTable(
                name: "Sequences",
                schema: "Sequences",
                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),
                    Rollover = table.Column<int>(type: "int", nullable: false),
                    Pattern = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
                    Seed = table.Column<long>(type: "bigint", nullable: false),
                    Increment = table.Column<long>(type: "bigint", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    LastIssueDate = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    LastIssueValue = table.Column<long>(type: "bigint", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Sequences", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "Users",
                schema: "UserManager",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    FirstName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    LastName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    MiddleNames = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Email = table.Column<string>(type: "nvarchar(450)", nullable: false),
                    Password = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    EmailConfirmed = table.Column<bool>(type: "bit", nullable: false),
                    UsingTwoFactorAuthentication = table.Column<bool>(type: "bit", nullable: false),
                    TwoFactorAuthenticationKey = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Created = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    LastUpdated = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    Active = table.Column<bool>(type: "bit", nullable: false),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Users", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "AuditEntries",
                schema: "Audit",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    AuditLogId = table.Column<long>(type: "bigint", nullable: false),
                    EntityName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    PrimaryKey = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    DisplayName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    IsPrimary = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AuditEntries", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AuditEntries_AuditDetails_AuditLogId",
                        column: x => x.AuditLogId,
                        principalSchema: "Audit",
                        principalTable: "AuditDetails",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "CustomFieldFormTemplates",
                schema: "CustomFields",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CustomFieldId = table.Column<long>(type: "bigint", nullable: false),
                    FormTemplateId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_CustomFieldFormTemplates", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CustomFieldFormTemplates_CustomFields_CustomFieldId",
                        column: x => x.CustomFieldId,
                        principalSchema: "CustomFields",
                        principalTable: "CustomFields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_CustomFieldFormTemplates_FormTemplates_FormTemplateId",
                        column: x => x.FormTemplateId,
                        principalSchema: "Forms",
                        principalTable: "FormTemplates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "FormTemplateVersions",
                schema: "Forms",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    TemplateId = table.Column<long>(type: "bigint", nullable: false),
                    Version = table.Column<long>(type: "bigint", nullable: false),
                    FormDefinition = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_FormTemplateVersions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_FormTemplateVersions_FormTemplates_TemplateId",
                        column: x => x.TemplateId,
                        principalSchema: "Forms",
                        principalTable: "FormTemplates",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "CustomFieldGlossaries",
                schema: "CustomFields",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CustomFieldId = table.Column<long>(type: "bigint", nullable: false),
                    GlossaryId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_CustomFieldGlossaries", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CustomFieldGlossaries_CustomFields_CustomFieldId",
                        column: x => x.CustomFieldId,
                        principalSchema: "CustomFields",
                        principalTable: "CustomFields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_CustomFieldGlossaries_Glossaries_GlossaryId",
                        column: x => x.GlossaryId,
                        principalSchema: "Glossaries",
                        principalTable: "Glossaries",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "GlossaryCustomFields",
                schema: "Glossaries",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    GlossaryId = table.Column<long>(type: "bigint", nullable: false),
                    CustomFieldId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GlossaryCustomFields", x => x.Id);
                    table.UniqueConstraint("AK_GlossaryCustomFields_GlossaryId_CustomFieldId", x => new { x.GlossaryId, x.CustomFieldId });
                    table.ForeignKey(
                        name: "FK_GlossaryCustomFields_CustomFields_CustomFieldId",
                        column: x => x.CustomFieldId,
                        principalSchema: "CustomFields",
                        principalTable: "CustomFields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_GlossaryCustomFields_Glossaries_GlossaryId",
                        column: x => x.GlossaryId,
                        principalSchema: "Glossaries",
                        principalTable: "Glossaries",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "GlossaryCustomFieldValues",
                schema: "Glossaries",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    GlossaryId = table.Column<long>(type: "bigint", nullable: false),
                    CustomFieldId = table.Column<long>(type: "bigint", nullable: false),
                    Index = table.Column<long>(type: "bigint", nullable: false),
                    Value = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    DisplayValue = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GlossaryCustomFieldValues", x => x.Id);
                    table.UniqueConstraint("AK_GlossaryCustomFieldValues_GlossaryId_CustomFieldId_Index", x => new { x.GlossaryId, x.CustomFieldId, x.Index });
                    table.ForeignKey(
                        name: "FK_GlossaryCustomFieldValues_CustomFields_CustomFieldId",
                        column: x => x.CustomFieldId,
                        principalSchema: "CustomFields",
                        principalTable: "CustomFields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_GlossaryCustomFieldValues_Glossaries_GlossaryId",
                        column: x => x.GlossaryId,
                        principalSchema: "Glossaries",
                        principalTable: "Glossaries",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "OrganisationContacts",
                schema: "Printer",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    OrganisationId = table.Column<long>(type: "bigint", nullable: false),
                    ContactId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_OrganisationContacts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OrganisationContacts_Contacts_ContactId",
                        column: x => x.ContactId,
                        principalSchema: "Contacts",
                        principalTable: "Contacts",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_OrganisationContacts_Organisations_OrganisationId",
                        column: x => x.OrganisationId,
                        principalSchema: "Printer",
                        principalTable: "Organisations",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Sites",
                schema: "Printer",
                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(max)", nullable: false),
                    Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    OrganisationId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Sites", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Sites_Organisations_OrganisationId",
                        column: x => x.OrganisationId,
                        principalSchema: "Printer",
                        principalTable: "Organisations",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "CustomFieldSequences",
                schema: "CustomFields",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CustomFieldId = table.Column<long>(type: "bigint", nullable: false),
                    SequenceId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_CustomFieldSequences", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CustomFieldSequences_CustomFields_CustomFieldId",
                        column: x => x.CustomFieldId,
                        principalSchema: "CustomFields",
                        principalTable: "CustomFields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_CustomFieldSequences_Sequences_SequenceId",
                        column: x => x.SequenceId,
                        principalSchema: "Sequences",
                        principalTable: "Sequences",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "EmailUserActions",
                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),
                    EmailActionType = table.Column<int>(type: "int", nullable: false),
                    Token = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Created = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    Expires = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_EmailUserActions", x => x.Id);
                    table.UniqueConstraint("AK_EmailUserActions_Token_EmailActionType", x => new { x.Token, x.EmailActionType });
                    table.ForeignKey(
                        name: "FK_EmailUserActions_Users_UserId",
                        column: x => x.UserId,
                        principalSchema: "UserManager",
                        principalTable: "Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "AuditDrillHierarchies",
                schema: "Audit",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    ParentAuditDrillDownEntityId = table.Column<long>(type: "bigint", nullable: false),
                    ChildAuditDrillDownEntityId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AuditDrillHierarchies", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AuditDrillHierarchies_AuditEntries_ChildAuditDrillDownEntityId",
                        column: x => x.ChildAuditDrillDownEntityId,
                        principalSchema: "Audit",
                        principalTable: "AuditEntries",
                        principalColumn: "Id");
                    table.ForeignKey(
                        name: "FK_AuditDrillHierarchies_AuditEntries_ParentAuditDrillDownEntityId",
                        column: x => x.ParentAuditDrillDownEntityId,
                        principalSchema: "Audit",
                        principalTable: "AuditEntries",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateTable(
                name: "FormInstances",
                schema: "Forms",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    FormTemplateVersionId = table.Column<long>(type: "bigint", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_FormInstances", x => x.Id);
                    table.ForeignKey(
                        name: "FK_FormInstances_FormTemplateVersions_FormTemplateVersionId",
                        column: x => x.FormTemplateVersionId,
                        principalSchema: "Forms",
                        principalTable: "FormTemplateVersions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "SiteContacts",
                schema: "Printer",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    SiteId = table.Column<long>(type: "bigint", nullable: false),
                    ContactId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SiteContacts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_SiteContacts_Contacts_ContactId",
                        column: x => x.ContactId,
                        principalSchema: "Contacts",
                        principalTable: "Contacts",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_SiteContacts_Sites_SiteId",
                        column: x => x.SiteId,
                        principalSchema: "Printer",
                        principalTable: "Sites",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "Specifications",
                schema: "Printer",
                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(max)", nullable: false),
                    FormInstanceId = table.Column<long>(type: "bigint", nullable: false),
                    Deleted = table.Column<bool>(type: "bit", nullable: false),
                    SiteId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Specifications", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Specifications_Sites_SiteId",
                        column: x => x.SiteId,
                        principalSchema: "Printer",
                        principalTable: "Sites",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "FormFieldInstances",
                schema: "Forms",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    FormInstanceId = table.Column<long>(type: "bigint", nullable: false),
                    CustomFieldId = table.Column<long>(type: "bigint", nullable: false),
                    Index = table.Column<long>(type: "bigint", nullable: false),
                    Value = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    DisplayValue = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Guid = table.Column<Guid>(type: "uniqueidentifier", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_FormFieldInstances", x => x.Id);
                    table.ForeignKey(
                        name: "FK_FormFieldInstances_CustomFields_CustomFieldId",
                        column: x => x.CustomFieldId,
                        principalSchema: "CustomFields",
                        principalTable: "CustomFields",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_FormFieldInstances_FormInstances_FormInstanceId",
                        column: x => x.FormInstanceId,
                        principalSchema: "Forms",
                        principalTable: "FormInstances",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                schema: "Glossaries",
                table: "Glossaries",
                columns: new[] { "Id", "Deleted", "Guid", "Name", "ParentId" },
                values: new object[,]
                {
                    { 1L, false, new Guid("fa6566f8-b4b0-48c5-9985-336c9284796e"), "System", null },
                    { 2L, false, new Guid("90c48cf1-1a2b-4a76-b30b-260ecb149ccc"), "Domain", null }
                });

            migrationBuilder.InsertData(
                schema: "Diagnostics",
                table: "PerformanceThresholds",
                columns: new[] { "ActionName", "ControllerName", "RequestType", "TotalTimeMS" },
                values: new object[] { "", "", "", 2000 });

            migrationBuilder.InsertData(
                schema: "UserManager",
                table: "Users",
                columns: new[] { "Id", "Active", "Created", "Email", "EmailConfirmed", "FirstName", "Guid", "LastName", "LastUpdated", "MiddleNames", "Password", "TwoFactorAuthenticationKey", "UsingTwoFactorAuthentication" },
                values: new object[] { 1L, true, new DateTimeOffset(new DateTime(2022, 6, 9, 12, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)), "testuser1@sun-strategy.com", true, "Test1", new Guid("30cfcd5b-3385-43f1-b59a-fd35236f3d92"), "User", new DateTimeOffset(new DateTime(2022, 6, 9, 12, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)), "", "AgAAAAIAACcQAAAAAAAAABDGmF9Hre4l8k9lkZAxBRJk0zNHD2b6xfKz4C7h6UjuirkoyoP2fVR6d9b7riT03UnL5yAgFh2pSSVQDx+nQ5PBjZRB9UG4u5FrY8W7ouA/+w==", "4EHHG42OWCN3L72TSRYSHTV6MRJXVOY3", false });

            migrationBuilder.CreateIndex(
                name: "IX_AuditDrillHierarchies_ChildAuditDrillDownEntityId",
                schema: "Audit",
                table: "AuditDrillHierarchies",
                column: "ChildAuditDrillDownEntityId");

            migrationBuilder.CreateIndex(
                name: "IX_AuditDrillHierarchies_ParentAuditDrillDownEntityId",
                schema: "Audit",
                table: "AuditDrillHierarchies",
                column: "ParentAuditDrillDownEntityId");

            migrationBuilder.CreateIndex(
                name: "IX_AuditEntries_AuditLogId",
                schema: "Audit",
                table: "AuditEntries",
                column: "AuditLogId");

            migrationBuilder.CreateIndex(
                name: "IX_Contacts_Guid",
                schema: "Contacts",
                table: "Contacts",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Contacts_Id",
                schema: "Contacts",
                table: "Contacts",
                column: "Id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_CustomFieldFormTemplates_CustomFieldId",
                schema: "CustomFields",
                table: "CustomFieldFormTemplates",
                column: "CustomFieldId");

            migrationBuilder.CreateIndex(
                name: "IX_CustomFieldFormTemplates_FormTemplateId",
                schema: "CustomFields",
                table: "CustomFieldFormTemplates",
                column: "FormTemplateId");

            migrationBuilder.CreateIndex(
                name: "IX_CustomFieldGlossaries_CustomFieldId",
                schema: "CustomFields",
                table: "CustomFieldGlossaries",
                column: "CustomFieldId");

            migrationBuilder.CreateIndex(
                name: "IX_CustomFieldGlossaries_GlossaryId",
                schema: "CustomFields",
                table: "CustomFieldGlossaries",
                column: "GlossaryId");

            migrationBuilder.CreateIndex(
                name: "IX_CustomFields_Guid",
                schema: "CustomFields",
                table: "CustomFields",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_CustomFields_Name",
                schema: "CustomFields",
                table: "CustomFields",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_CustomFieldSequences_CustomFieldId",
                schema: "CustomFields",
                table: "CustomFieldSequences",
                column: "CustomFieldId");

            migrationBuilder.CreateIndex(
                name: "IX_CustomFieldSequences_SequenceId",
                schema: "CustomFields",
                table: "CustomFieldSequences",
                column: "SequenceId");

            migrationBuilder.CreateIndex(
                name: "IX_EmailUserActions_UserId",
                schema: "UserManager",
                table: "EmailUserActions",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_FormFieldInstances_CustomFieldId",
                schema: "Forms",
                table: "FormFieldInstances",
                column: "CustomFieldId");

            migrationBuilder.CreateIndex(
                name: "IX_FormFieldInstances_FormInstanceId_CustomFieldId_Index",
                schema: "Forms",
                table: "FormFieldInstances",
                columns: new[] { "FormInstanceId", "CustomFieldId", "Index" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_FormFieldInstances_Guid",
                schema: "Forms",
                table: "FormFieldInstances",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_FormInstances_FormTemplateVersionId",
                schema: "Forms",
                table: "FormInstances",
                column: "FormTemplateVersionId");

            migrationBuilder.CreateIndex(
                name: "IX_FormInstances_Guid",
                schema: "Forms",
                table: "FormInstances",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_FormTemplates_Guid",
                schema: "Forms",
                table: "FormTemplates",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_FormTemplates_Name",
                schema: "Forms",
                table: "FormTemplates",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_FormTemplateVersions_Guid",
                schema: "Forms",
                table: "FormTemplateVersions",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_FormTemplateVersions_TemplateId_Version",
                schema: "Forms",
                table: "FormTemplateVersions",
                columns: new[] { "TemplateId", "Version" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Glossaries_Guid",
                schema: "Glossaries",
                table: "Glossaries",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Glossaries_Id_ParentId",
                schema: "Glossaries",
                table: "Glossaries",
                columns: new[] { "Id", "ParentId" },
                unique: true,
                filter: "[ParentId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_Glossaries_Name",
                schema: "Glossaries",
                table: "Glossaries",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Glossaries_ParentId",
                schema: "Glossaries",
                table: "Glossaries",
                column: "ParentId");

            migrationBuilder.CreateIndex(
                name: "IX_GlossaryCustomFields_CustomFieldId",
                schema: "Glossaries",
                table: "GlossaryCustomFields",
                column: "CustomFieldId");

            migrationBuilder.CreateIndex(
                name: "IX_GlossaryCustomFieldValues_CustomFieldId",
                schema: "Glossaries",
                table: "GlossaryCustomFieldValues",
                column: "CustomFieldId");

            migrationBuilder.CreateIndex(
                name: "IX_GlossaryCustomFieldValues_GlossaryId_CustomFieldId_Index",
                schema: "Glossaries",
                table: "GlossaryCustomFieldValues",
                columns: new[] { "GlossaryId", "CustomFieldId", "Index" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_GlossaryCustomFieldValues_Guid",
                schema: "Glossaries",
                table: "GlossaryCustomFieldValues",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_OrganisationContacts_ContactId",
                schema: "Printer",
                table: "OrganisationContacts",
                column: "ContactId");

            migrationBuilder.CreateIndex(
                name: "IX_OrganisationContacts_OrganisationId",
                schema: "Printer",
                table: "OrganisationContacts",
                column: "OrganisationId");

            migrationBuilder.CreateIndex(
                name: "IX_Organisations_Guid",
                schema: "Printer",
                table: "Organisations",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Organisations_Id",
                schema: "Printer",
                table: "Organisations",
                column: "Id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Sequences_Guid",
                schema: "Sequences",
                table: "Sequences",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Sequences_Name",
                schema: "Sequences",
                table: "Sequences",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_SiteContacts_ContactId",
                schema: "Printer",
                table: "SiteContacts",
                column: "ContactId");

            migrationBuilder.CreateIndex(
                name: "IX_SiteContacts_SiteId",
                schema: "Printer",
                table: "SiteContacts",
                column: "SiteId");

            migrationBuilder.CreateIndex(
                name: "IX_Sites_Guid",
                schema: "Printer",
                table: "Sites",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Sites_Id",
                schema: "Printer",
                table: "Sites",
                column: "Id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Sites_OrganisationId",
                schema: "Printer",
                table: "Sites",
                column: "OrganisationId");

            migrationBuilder.CreateIndex(
                name: "IX_Specifications_Guid",
                schema: "Printer",
                table: "Specifications",
                column: "Guid",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Specifications_Id",
                schema: "Printer",
                table: "Specifications",
                column: "Id",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Specifications_SiteId",
                schema: "Printer",
                table: "Specifications",
                column: "SiteId");

            migrationBuilder.CreateIndex(
                name: "IX_Users_Email",
                schema: "UserManager",
                table: "Users",
                column: "Email",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Users_Guid",
                schema: "UserManager",
                table: "Users",
                column: "Guid",
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "CustomFieldFormTemplates",
                schema: "CustomFields");

            migrationBuilder.DropTable(
                name: "CustomFieldGlossaries",
                schema: "CustomFields");

            migrationBuilder.DropTable(
                name: "CustomFieldSequences",
                schema: "CustomFields");

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

            migrationBuilder.DropTable(
                name: "FailedAccessAttempts",
                schema: "Sentinel");

            migrationBuilder.DropTable(
                name: "FormFieldInstances",
                schema: "Forms");

            migrationBuilder.DropTable(
                name: "GlossaryCustomFields",
                schema: "Glossaries");

            migrationBuilder.DropTable(
                name: "GlossaryCustomFieldValues",
                schema: "Glossaries");

            migrationBuilder.DropTable(
                name: "OrganisationContacts",
                schema: "Printer");

            migrationBuilder.DropTable(
                name: "PerformanceReports",
                schema: "Diagnostics");

            migrationBuilder.DropTable(
                name: "PerformanceThresholds",
                schema: "Diagnostics");

            migrationBuilder.DropTable(
                name: "SiteContacts",
                schema: "Printer");

            migrationBuilder.DropTable(
                name: "Specifications",
                schema: "Printer");

            migrationBuilder.DropTable(
                name: "AuditEntries",
                schema: "Audit");

            migrationBuilder.DropTable(
                name: "Sequences",
                schema: "Sequences");

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

            migrationBuilder.DropTable(
                name: "FormInstances",
                schema: "Forms");

            migrationBuilder.DropTable(
                name: "CustomFields",
                schema: "CustomFields");

            migrationBuilder.DropTable(
                name: "Glossaries",
                schema: "Glossaries");

            migrationBuilder.DropTable(
                name: "Contacts",
                schema: "Contacts");

            migrationBuilder.DropTable(
                name: "Sites",
                schema: "Printer");

            migrationBuilder.DropTable(
                name: "AuditDetails",
                schema: "Audit");

            migrationBuilder.DropTable(
                name: "FormTemplateVersions",
                schema: "Forms");

            migrationBuilder.DropTable(
                name: "Organisations",
                schema: "Printer");

            migrationBuilder.DropTable(
                name: "FormTemplates",
                schema: "Forms");
        }
    }
}
