using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Remotely.Server.Migrations.SqlServer;

public partial class InitialCreate : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "AspNetRoles",
            columns: table => new
            {
                Id = table.Column<string>(nullable: false),
                Name = table.Column<string>(maxLength: 256, nullable: true),
                NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
                ConcurrencyStamp = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AspNetRoles", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "Organizations",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                OrganizationName = table.Column<string>(maxLength: 25, nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Organizations", x => x.ID);
            });

        migrationBuilder.CreateTable(
            name: "AspNetRoleClaims",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                RoleId = table.Column<string>(nullable: false),
                ClaimType = table.Column<string>(nullable: true),
                ClaimValue = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id);
                table.ForeignKey(
                    name: "FK_AspNetRoleClaims_AspNetRoles_RoleId",
                    column: x => x.RoleId,
                    principalTable: "AspNetRoles",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateTable(
            name: "ApiTokens",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                LastUsed = table.Column<DateTimeOffset>(nullable: true),
                Name = table.Column<string>(maxLength: 200, nullable: true),
                OrganizationID = table.Column<string>(nullable: true),
                Secret = table.Column<string>(nullable: true),
                Token = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_ApiTokens", x => x.ID);
                table.ForeignKey(
                    name: "FK_ApiTokens_Organizations_OrganizationID",
                    column: x => x.OrganizationID,
                    principalTable: "Organizations",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "CommandResults",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                CommandMode = table.Column<string>(nullable: true),
                CommandText = table.Column<string>(nullable: true),
                SenderUserID = table.Column<string>(nullable: true),
                SenderConnectionID = table.Column<string>(nullable: true),
                TargetDeviceIDs = table.Column<string>(nullable: true),
                PSCoreResults = table.Column<string>(nullable: true),
                CommandResults = table.Column<string>(nullable: true),
                TimeStamp = table.Column<DateTimeOffset>(nullable: false),
                OrganizationID = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_CommandResults", x => x.ID);
                table.ForeignKey(
                    name: "FK_CommandResults_Organizations_OrganizationID",
                    column: x => x.OrganizationID,
                    principalTable: "Organizations",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "DeviceGroups",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                Name = table.Column<string>(maxLength: 200, nullable: true),
                OrganizationID = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_DeviceGroups", x => x.ID);
                table.ForeignKey(
                    name: "FK_DeviceGroups_Organizations_OrganizationID",
                    column: x => x.OrganizationID,
                    principalTable: "Organizations",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "EventLogs",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                EventType = table.Column<int>(nullable: false),
                Message = table.Column<string>(nullable: true),
                Source = table.Column<string>(nullable: true),
                StackTrace = table.Column<string>(nullable: true),
                OrganizationID = table.Column<string>(nullable: true),
                TimeStamp = table.Column<DateTimeOffset>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_EventLogs", x => x.ID);
                table.ForeignKey(
                    name: "FK_EventLogs_Organizations_OrganizationID",
                    column: x => x.OrganizationID,
                    principalTable: "Organizations",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "InviteLinks",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                InvitedUser = table.Column<string>(nullable: true),
                IsAdmin = table.Column<bool>(nullable: false),
                DateSent = table.Column<DateTimeOffset>(nullable: false),
                OrganizationID = table.Column<string>(nullable: true),
                ResetUrl = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_InviteLinks", x => x.ID);
                table.ForeignKey(
                    name: "FK_InviteLinks_Organizations_OrganizationID",
                    column: x => x.OrganizationID,
                    principalTable: "Organizations",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "RemotelyUsers",
            columns: table => new
            {
                Id = table.Column<string>(nullable: false),
                UserName = table.Column<string>(maxLength: 256, nullable: true),
                NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
                Email = table.Column<string>(maxLength: 256, nullable: true),
                NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
                EmailConfirmed = table.Column<bool>(nullable: false),
                PasswordHash = table.Column<string>(nullable: true),
                SecurityStamp = table.Column<string>(nullable: true),
                ConcurrencyStamp = table.Column<string>(nullable: true),
                PhoneNumber = table.Column<string>(nullable: true),
                PhoneNumberConfirmed = table.Column<bool>(nullable: false),
                TwoFactorEnabled = table.Column<bool>(nullable: false),
                LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
                LockoutEnabled = table.Column<bool>(nullable: false),
                AccessFailedCount = table.Column<int>(nullable: false),
                Discriminator = table.Column<string>(nullable: false),
                DisplayName = table.Column<string>(maxLength: 100, nullable: true),
                IsAdministrator = table.Column<bool>(nullable: true),
                IsServerAdmin = table.Column<bool>(nullable: true),
                OrganizationID = table.Column<string>(nullable: true),
                TempPassword = table.Column<string>(nullable: true),
                UserOptions = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_RemotelyUsers", x => x.Id);
                table.ForeignKey(
                    name: "FK_RemotelyUsers_Organizations_OrganizationID",
                    column: x => x.OrganizationID,
                    principalTable: "Organizations",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "SharedFiles",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                FileName = table.Column<string>(nullable: true),
                ContentType = table.Column<string>(nullable: true),
                FileContents = table.Column<byte[]>(nullable: true),
                Timestamp = table.Column<DateTimeOffset>(nullable: false),
                OrganizationID = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_SharedFiles", x => x.ID);
                table.ForeignKey(
                    name: "FK_SharedFiles_Organizations_OrganizationID",
                    column: x => x.OrganizationID,
                    principalTable: "Organizations",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "Devices",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                AgentVersion = table.Column<string>(nullable: true),
                Alias = table.Column<string>(maxLength: 100, nullable: true),
                CpuUtilization = table.Column<double>(nullable: false),
                CurrentUser = table.Column<string>(nullable: true),
                DeviceGroupID = table.Column<string>(nullable: true),
                DeviceName = table.Column<string>(nullable: true),
                Drives = table.Column<string>(nullable: true),
                Is64Bit = table.Column<bool>(nullable: false),
                IsOnline = table.Column<bool>(nullable: false),
                LastOnline = table.Column<DateTimeOffset>(nullable: false),
                Notes = table.Column<string>(nullable: true),
                OrganizationID = table.Column<string>(nullable: true),
                OSArchitecture = table.Column<int>(nullable: false),
                OSDescription = table.Column<string>(nullable: true),
                Platform = table.Column<string>(nullable: true),
                ProcessorCount = table.Column<int>(nullable: false),
                PublicIP = table.Column<string>(nullable: true),
                ServerVerificationToken = table.Column<string>(nullable: true),
                Tags = table.Column<string>(maxLength: 200, nullable: true),
                TotalMemory = table.Column<double>(nullable: false),
                TotalStorage = table.Column<double>(nullable: false),
                UsedMemory = table.Column<double>(nullable: false),
                UsedStorage = table.Column<double>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Devices", x => x.ID);
                table.ForeignKey(
                    name: "FK_Devices_DeviceGroups_DeviceGroupID",
                    column: x => x.DeviceGroupID,
                    principalTable: "DeviceGroups",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_Devices_Organizations_OrganizationID",
                    column: x => x.OrganizationID,
                    principalTable: "Organizations",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "AspNetUserClaims",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                UserId = table.Column<string>(nullable: false),
                ClaimType = table.Column<string>(nullable: true),
                ClaimValue = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AspNetUserClaims", x => x.Id);
                table.ForeignKey(
                    name: "FK_AspNetUserClaims_RemotelyUsers_UserId",
                    column: x => x.UserId,
                    principalTable: "RemotelyUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateTable(
            name: "AspNetUserLogins",
            columns: table => new
            {
                LoginProvider = table.Column<string>(maxLength: 128, nullable: false),
                ProviderKey = table.Column<string>(maxLength: 128, nullable: false),
                ProviderDisplayName = table.Column<string>(nullable: true),
                UserId = table.Column<string>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey });
                table.ForeignKey(
                    name: "FK_AspNetUserLogins_RemotelyUsers_UserId",
                    column: x => x.UserId,
                    principalTable: "RemotelyUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateTable(
            name: "AspNetUserRoles",
            columns: table => new
            {
                UserId = table.Column<string>(nullable: false),
                RoleId = table.Column<string>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId });
                table.ForeignKey(
                    name: "FK_AspNetUserRoles_AspNetRoles_RoleId",
                    column: x => x.RoleId,
                    principalTable: "AspNetRoles",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
                table.ForeignKey(
                    name: "FK_AspNetUserRoles_RemotelyUsers_UserId",
                    column: x => x.UserId,
                    principalTable: "RemotelyUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateTable(
            name: "AspNetUserTokens",
            columns: table => new
            {
                UserId = table.Column<string>(nullable: false),
                LoginProvider = table.Column<string>(maxLength: 128, nullable: false),
                Name = table.Column<string>(maxLength: 128, nullable: false),
                Value = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name });
                table.ForeignKey(
                    name: "FK_AspNetUserTokens_RemotelyUsers_UserId",
                    column: x => x.UserId,
                    principalTable: "RemotelyUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateTable(
            name: "PermissionLinks",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                UserID = table.Column<string>(nullable: true),
                DeviceGroupID = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_PermissionLinks", x => x.ID);
                table.ForeignKey(
                    name: "FK_PermissionLinks_DeviceGroups_DeviceGroupID",
                    column: x => x.DeviceGroupID,
                    principalTable: "DeviceGroups",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_PermissionLinks_RemotelyUsers_UserID",
                    column: x => x.UserID,
                    principalTable: "RemotelyUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "Alerts",
            columns: table => new
            {
                ID = table.Column<string>(nullable: false),
                CreatedOn = table.Column<DateTimeOffset>(nullable: false),
                DeviceID = table.Column<string>(nullable: true),
                Message = table.Column<string>(nullable: true),
                OrganizationID = table.Column<string>(nullable: true),
                UserID = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Alerts", x => x.ID);
                table.ForeignKey(
                    name: "FK_Alerts_Devices_DeviceID",
                    column: x => x.DeviceID,
                    principalTable: "Devices",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_Alerts_Organizations_OrganizationID",
                    column: x => x.OrganizationID,
                    principalTable: "Organizations",
                    principalColumn: "ID",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_Alerts_RemotelyUsers_UserID",
                    column: x => x.UserID,
                    principalTable: "RemotelyUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Alerts_DeviceID",
            table: "Alerts",
            column: "DeviceID");

        migrationBuilder.CreateIndex(
            name: "IX_Alerts_OrganizationID",
            table: "Alerts",
            column: "OrganizationID");

        migrationBuilder.CreateIndex(
            name: "IX_Alerts_UserID",
            table: "Alerts",
            column: "UserID");

        migrationBuilder.CreateIndex(
            name: "IX_ApiTokens_OrganizationID",
            table: "ApiTokens",
            column: "OrganizationID");

        migrationBuilder.CreateIndex(
            name: "IX_ApiTokens_Token",
            table: "ApiTokens",
            column: "Token");

        migrationBuilder.CreateIndex(
            name: "IX_AspNetRoleClaims_RoleId",
            table: "AspNetRoleClaims",
            column: "RoleId");

        migrationBuilder.CreateIndex(
            name: "RoleNameIndex",
            table: "AspNetRoles",
            column: "NormalizedName",
            unique: true,
            filter: "[NormalizedName] IS NOT NULL");

        migrationBuilder.CreateIndex(
            name: "IX_AspNetUserClaims_UserId",
            table: "AspNetUserClaims",
            column: "UserId");

        migrationBuilder.CreateIndex(
            name: "IX_AspNetUserLogins_UserId",
            table: "AspNetUserLogins",
            column: "UserId");

        migrationBuilder.CreateIndex(
            name: "IX_AspNetUserRoles_RoleId",
            table: "AspNetUserRoles",
            column: "RoleId");

        migrationBuilder.CreateIndex(
            name: "IX_CommandResults_OrganizationID",
            table: "CommandResults",
            column: "OrganizationID");

        migrationBuilder.CreateIndex(
            name: "IX_DeviceGroups_OrganizationID",
            table: "DeviceGroups",
            column: "OrganizationID");

        migrationBuilder.CreateIndex(
            name: "IX_Devices_DeviceGroupID",
            table: "Devices",
            column: "DeviceGroupID");

        migrationBuilder.CreateIndex(
            name: "IX_Devices_DeviceName",
            table: "Devices",
            column: "DeviceName");

        migrationBuilder.CreateIndex(
            name: "IX_Devices_OrganizationID",
            table: "Devices",
            column: "OrganizationID");

        migrationBuilder.CreateIndex(
            name: "IX_EventLogs_OrganizationID",
            table: "EventLogs",
            column: "OrganizationID");

        migrationBuilder.CreateIndex(
            name: "IX_InviteLinks_OrganizationID",
            table: "InviteLinks",
            column: "OrganizationID");

        migrationBuilder.CreateIndex(
            name: "IX_PermissionLinks_DeviceGroupID",
            table: "PermissionLinks",
            column: "DeviceGroupID");

        migrationBuilder.CreateIndex(
            name: "IX_PermissionLinks_UserID",
            table: "PermissionLinks",
            column: "UserID");

        migrationBuilder.CreateIndex(
            name: "EmailIndex",
            table: "RemotelyUsers",
            column: "NormalizedEmail");

        migrationBuilder.CreateIndex(
            name: "UserNameIndex",
            table: "RemotelyUsers",
            column: "NormalizedUserName",
            unique: true,
            filter: "[NormalizedUserName] IS NOT NULL");

        migrationBuilder.CreateIndex(
            name: "IX_RemotelyUsers_OrganizationID",
            table: "RemotelyUsers",
            column: "OrganizationID");

        migrationBuilder.CreateIndex(
            name: "IX_RemotelyUsers_UserName",
            table: "RemotelyUsers",
            column: "UserName");

        migrationBuilder.CreateIndex(
            name: "IX_SharedFiles_OrganizationID",
            table: "SharedFiles",
            column: "OrganizationID");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Alerts");

        migrationBuilder.DropTable(
            name: "ApiTokens");

        migrationBuilder.DropTable(
            name: "AspNetRoleClaims");

        migrationBuilder.DropTable(
            name: "AspNetUserClaims");

        migrationBuilder.DropTable(
            name: "AspNetUserLogins");

        migrationBuilder.DropTable(
            name: "AspNetUserRoles");

        migrationBuilder.DropTable(
            name: "AspNetUserTokens");

        migrationBuilder.DropTable(
            name: "CommandResults");

        migrationBuilder.DropTable(
            name: "EventLogs");

        migrationBuilder.DropTable(
            name: "InviteLinks");

        migrationBuilder.DropTable(
            name: "PermissionLinks");

        migrationBuilder.DropTable(
            name: "SharedFiles");

        migrationBuilder.DropTable(
            name: "Devices");

        migrationBuilder.DropTable(
            name: "AspNetRoles");

        migrationBuilder.DropTable(
            name: "RemotelyUsers");

        migrationBuilder.DropTable(
            name: "DeviceGroups");

        migrationBuilder.DropTable(
            name: "Organizations");
    }
}
