using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Baya.Infrastructure.Persistence.Migrations
{
    /// <inheritdoc />
    public partial class VerificationPipeline : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.EnsureSchema(
                name: "verif");

            migrationBuilder.CreateTable(
                name: "NurseCredentials",
                schema: "verif",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    NurseId = table.Column<long>(type: "bigint", nullable: false),
                    CredentialType = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    CredentialNumber = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false),
                    HolderNameSnapshot = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    IssuingAuthority = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
                    IssuedAt = table.Column<DateOnly>(type: "date", nullable: true),
                    ExpiresAt = table.Column<DateOnly>(type: "date", nullable: true),
                    VerificationSource = table.Column<string>(type: "nvarchar(300)", maxLength: 300, nullable: true),
                    VerificationMethod = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    VerifiedByAdminId = table.Column<int>(type: "int", nullable: true),
                    DeletedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_NurseCredentials", x => x.Id);
                    table.ForeignKey(
                        name: "FK_NurseCredentials_NurseProfiles_NurseId",
                        column: x => x.NurseId,
                        principalSchema: "usr",
                        principalTable: "NurseProfiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_NurseCredentials_Users_VerifiedByAdminId",
                        column: x => x.VerifiedByAdminId,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId");
                });

            migrationBuilder.CreateTable(
                name: "NurseVerifications",
                schema: "verif",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    NurseId = table.Column<long>(type: "bigint", nullable: false),
                    Status = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    SubmittedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    ApprovedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    RejectedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    SuspendedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    RejectionReason = table.Column<string>(type: "nvarchar(1000)", maxLength: 1000, nullable: true),
                    ReviewedByAdminId = table.Column<int>(type: "int", nullable: true),
                    InternalNotes = table.Column<string>(type: "nvarchar(2000)", maxLength: 2000, nullable: true),
                    DeletedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_NurseVerifications", x => x.Id);
                    table.ForeignKey(
                        name: "FK_NurseVerifications_NurseProfiles_NurseId",
                        column: x => x.NurseId,
                        principalSchema: "usr",
                        principalTable: "NurseProfiles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_NurseVerifications_Users_ReviewedByAdminId",
                        column: x => x.ReviewedByAdminId,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId");
                });

            migrationBuilder.CreateTable(
                name: "VerificationStepTypes",
                schema: "verif",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Code = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    DisplayName = table.Column<string>(type: "nvarchar(150)", maxLength: 150, nullable: false),
                    Description = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    IsRequired = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
                    IsAutomated = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
                    AutomationProvider = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    SortOrder = table.Column<int>(type: "int", nullable: false, defaultValue: 0),
                    IsActive = table.Column<bool>(type: "bit", nullable: false, defaultValue: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_VerificationStepTypes", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "VerificationSteps",
                schema: "verif",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    NurseVerificationId = table.Column<long>(type: "bigint", nullable: false),
                    StepTypeId = table.Column<long>(type: "bigint", nullable: false),
                    Status = table.Column<string>(type: "nvarchar(20)", maxLength: 20, nullable: false),
                    ExternalResponseJson = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    ExpiresAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    IsAutomated = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
                    StartedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CompletedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    FailureReason = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_VerificationSteps", x => x.Id);
                    table.ForeignKey(
                        name: "FK_VerificationSteps_NurseVerifications_NurseVerificationId",
                        column: x => x.NurseVerificationId,
                        principalSchema: "verif",
                        principalTable: "NurseVerifications",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_VerificationSteps_VerificationStepTypes_StepTypeId",
                        column: x => x.StepTypeId,
                        principalSchema: "verif",
                        principalTable: "VerificationStepTypes",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "VerificationDocuments",
                schema: "verif",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    StepId = table.Column<long>(type: "bigint", nullable: false),
                    ObjectStorageKey = table.Column<string>(type: "nvarchar(400)", maxLength: 400, nullable: false),
                    IntegrityHash = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
                    ContentType = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
                    FileSizeBytes = table.Column<long>(type: "bigint", nullable: false),
                    OriginalFileName = table.Column<string>(type: "nvarchar(260)", maxLength: 260, nullable: true),
                    UploadedByUserId = table.Column<int>(type: "int", nullable: false),
                    DeletedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
                    ModifiedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
                    CreatedById = table.Column<int>(type: "int", nullable: true),
                    ModifiedById = table.Column<int>(type: "int", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_VerificationDocuments", x => x.Id);
                    table.ForeignKey(
                        name: "FK_VerificationDocuments_Users_UploadedByUserId",
                        column: x => x.UploadedByUserId,
                        principalSchema: "usr",
                        principalTable: "Users",
                        principalColumn: "UserId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_VerificationDocuments_VerificationSteps_StepId",
                        column: x => x.StepId,
                        principalSchema: "verif",
                        principalTable: "VerificationSteps",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.InsertData(
                schema: "ops",
                table: "PlatformConfigs",
                columns: new[] { "Id", "CreatedAt", "CreatedById", "DataType", "Description", "Key", "ModifiedAt", "ModifiedById", "Value" },
                values: new object[] { 16L, new DateTimeOffset(new DateTime(2026, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)), null, "int", "Hours between credential-expiry scans (the scheduled cron is deferred; the scan is admin-triggered today).", "verification_expiry_scan_cadence_hours", null, null, "24" });

            migrationBuilder.CreateIndex(
                name: "IX_NurseCredentials_NurseId_CredentialType",
                schema: "verif",
                table: "NurseCredentials",
                columns: new[] { "NurseId", "CredentialType" });

            migrationBuilder.CreateIndex(
                name: "IX_NurseCredentials_VerifiedByAdminId",
                schema: "verif",
                table: "NurseCredentials",
                column: "VerifiedByAdminId");

            migrationBuilder.CreateIndex(
                name: "IX_NurseVerifications_NurseId",
                schema: "verif",
                table: "NurseVerifications",
                column: "NurseId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_NurseVerifications_ReviewedByAdminId",
                schema: "verif",
                table: "NurseVerifications",
                column: "ReviewedByAdminId");

            migrationBuilder.CreateIndex(
                name: "IX_VerificationDocuments_StepId",
                schema: "verif",
                table: "VerificationDocuments",
                column: "StepId");

            migrationBuilder.CreateIndex(
                name: "IX_VerificationDocuments_UploadedByUserId",
                schema: "verif",
                table: "VerificationDocuments",
                column: "UploadedByUserId");

            migrationBuilder.CreateIndex(
                name: "IX_VerificationSteps_StepTypeId",
                schema: "verif",
                table: "VerificationSteps",
                column: "StepTypeId");

            migrationBuilder.CreateIndex(
                name: "UX_VerificationSteps_Verification_StepType",
                schema: "verif",
                table: "VerificationSteps",
                columns: new[] { "NurseVerificationId", "StepTypeId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_VerificationStepTypes_Code",
                schema: "verif",
                table: "VerificationStepTypes",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_VerificationStepTypes_IsActive_SortOrder",
                schema: "verif",
                table: "VerificationStepTypes",
                columns: new[] { "IsActive", "SortOrder" });
        }

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

            migrationBuilder.DropTable(
                name: "VerificationDocuments",
                schema: "verif");

            migrationBuilder.DropTable(
                name: "VerificationSteps",
                schema: "verif");

            migrationBuilder.DropTable(
                name: "NurseVerifications",
                schema: "verif");

            migrationBuilder.DropTable(
                name: "VerificationStepTypes",
                schema: "verif");

            migrationBuilder.DeleteData(
                schema: "ops",
                table: "PlatformConfigs",
                keyColumn: "Id",
                keyValue: 16L);
        }
    }
}
