using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace NexusIdentity.Api.Migrations;

/// <summary>
/// Platform foundation: Organizations, Devices, DeviceTrust, EnrollmentRequests, Policies; Users.OrganizationId.
/// </summary>
[Migration("20260605120000_PlatformFoundation")]
public partial class PlatformFoundation : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<string>(
            name: "OrganizationId",
            table: "Users",
            type: "TEXT",
            maxLength: 64,
            nullable: true);

        migrationBuilder.CreateTable(
            name: "Organizations",
            columns: table => new
            {
                Id = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                Name = table.Column<string>(type: "TEXT", maxLength: 256, nullable: false),
                CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
                IsActive = table.Column<bool>(type: "INTEGER", nullable: false)
            },
            constraints: table => table.PrimaryKey("PK_Organizations", x => x.Id));

        migrationBuilder.CreateTable(
            name: "Devices",
            columns: table => new
            {
                DeviceId = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                OrganizationId = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true),
                UserId = table.Column<Guid>(type: "TEXT", nullable: true),
                ComputerName = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                FriendlyName = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                Owner = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                Os = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                AgentVersion = table.Column<string>(type: "TEXT", nullable: false),
                ShellVersion = table.Column<string>(type: "TEXT", nullable: false),
                EnrollmentStatus = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                RegisteredAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
                LastSeenUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                Online = table.Column<bool>(type: "INTEGER", nullable: false),
                LastHealth = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Devices", x => x.DeviceId);
                table.ForeignKey(
                    name: "FK_Devices_Organizations_OrganizationId",
                    column: x => x.OrganizationId,
                    principalTable: "Organizations",
                    principalColumn: "Id");
                table.ForeignKey(
                    name: "FK_Devices_Users_UserId",
                    column: x => x.UserId,
                    principalTable: "Users",
                    principalColumn: "Id");
            });

        migrationBuilder.CreateTable(
            name: "EnrollmentRequests",
            columns: table => new
            {
                Id = table.Column<Guid>(type: "TEXT", nullable: false),
                DeviceId = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                OrganizationId = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                UserName = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                DeviceFriendlyName = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                ComputerName = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                ShellVersion = table.Column<string>(type: "TEXT", nullable: false),
                AgentVersion = table.Column<string>(type: "TEXT", nullable: false),
                Status = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                RequestedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
                ProcessedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_EnrollmentRequests", x => x.Id);
                table.ForeignKey(
                    name: "FK_EnrollmentRequests_Organizations_OrganizationId",
                    column: x => x.OrganizationId,
                    principalTable: "Organizations",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateTable(
            name: "Policies",
            columns: table => new
            {
                Id = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                Name = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                Scope = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                IsEnabled = table.Column<bool>(type: "INTEGER", nullable: false),
                Summary = table.Column<string>(type: "TEXT", maxLength: 512, nullable: false),
                RulesJson = table.Column<string>(type: "TEXT", nullable: false),
                OrganizationId = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true),
                CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Policies", x => x.Id);
                table.ForeignKey(
                    name: "FK_Policies_Organizations_OrganizationId",
                    column: x => x.OrganizationId,
                    principalTable: "Organizations",
                    principalColumn: "Id");
            });

        migrationBuilder.CreateTable(
            name: "DeviceTrust",
            columns: table => new
            {
                DeviceId = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                Status = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                EvaluatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                Reason = table.Column<string>(type: "TEXT", maxLength: 512, nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_DeviceTrust", x => x.DeviceId);
                table.ForeignKey(
                    name: "FK_DeviceTrust_Devices_DeviceId",
                    column: x => x.DeviceId,
                    principalTable: "Devices",
                    principalColumn: "DeviceId",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateIndex(name: "IX_Users_OrganizationId", table: "Users", column: "OrganizationId");
        migrationBuilder.CreateIndex(name: "IX_Devices_OrganizationId", table: "Devices", column: "OrganizationId");
        migrationBuilder.CreateIndex(name: "IX_Devices_UserId", table: "Devices", column: "UserId");
        migrationBuilder.CreateIndex(name: "IX_EnrollmentRequests_DeviceId", table: "EnrollmentRequests", column: "DeviceId");
        migrationBuilder.CreateIndex(name: "IX_EnrollmentRequests_OrganizationId", table: "EnrollmentRequests", column: "OrganizationId");
        migrationBuilder.CreateIndex(name: "IX_EnrollmentRequests_Status", table: "EnrollmentRequests", column: "Status");
        migrationBuilder.CreateIndex(name: "IX_Policies_OrganizationId", table: "Policies", column: "OrganizationId");

        migrationBuilder.AddForeignKey(
            name: "FK_Users_Organizations_OrganizationId",
            table: "Users",
            column: "OrganizationId",
            principalTable: "Organizations",
            principalColumn: "Id");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(name: "FK_Users_Organizations_OrganizationId", table: "Users");
        migrationBuilder.DropTable(name: "DeviceTrust");
        migrationBuilder.DropTable(name: "EnrollmentRequests");
        migrationBuilder.DropTable(name: "Policies");
        migrationBuilder.DropTable(name: "Devices");
        migrationBuilder.DropTable(name: "Organizations");
        migrationBuilder.DropIndex(name: "IX_Users_OrganizationId", table: "Users");
        migrationBuilder.DropColumn(name: "OrganizationId", table: "Users");
    }
}
