using System;
using JobTrackerApi.Data;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace JobTrackerApi.Migrations;

/// <summary>
/// Moves CV upload and extraction-run creation into the migration chain while preserving imported
/// artifacts, structured results, and their nullable relationship.
/// </summary>
[DbContext(typeof(JobTrackerContext))]
[Migration("20260830125000_AdoptCvExtractionSchema")]
public sealed class AdoptCvExtractionSchema : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
        {
            migrationBuilder.Sql("""
                CREATE TABLE IF NOT EXISTS `CvUploadArtifacts` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `OriginalFileName` longtext NOT NULL,
                    `StoredFileName` longtext NOT NULL,
                    `MimeType` longtext NOT NULL,
                    `ByteSize` bigint NOT NULL,
                    `Sha256` longtext NOT NULL,
                    `StoragePath` longtext NOT NULL,
                    `UploadedAtUtc` datetime(6) NOT NULL,
                    PRIMARY KEY (`Id`)
                ) CHARACTER SET=utf8mb4;
                CREATE INDEX IF NOT EXISTS `IX_CvUploadArtifacts_OwnerUserId_UploadedAtUtc`
                    ON `CvUploadArtifacts` (`OwnerUserId`(191), `UploadedAtUtc`);

                CREATE TABLE IF NOT EXISTS `CvExtractionRuns` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `ArtifactId` int NULL,
                    `Trigger` longtext NOT NULL,
                    `ParserVersion` longtext NOT NULL,
                    `NormalizerVersion` longtext NOT NULL,
                    `LlmPromptVersion` longtext NOT NULL,
                    `Status` longtext NOT NULL,
                    `RawExtractedText` longtext NULL,
                    `NormalizedText` longtext NULL,
                    `StructuredProfileJson` longtext NULL,
                    `ErrorMessage` longtext NULL,
                    `StartedAtUtc` datetime(6) NOT NULL,
                    `CompletedAtUtc` datetime(6) NULL,
                    `AppliedAtUtc` datetime(6) NULL,
                    PRIMARY KEY (`Id`),
                    CONSTRAINT `FK_CvExtractionRuns_CvUploadArtifacts_ArtifactId`
                        FOREIGN KEY (`ArtifactId`) REFERENCES `CvUploadArtifacts` (`Id`) ON DELETE SET NULL
                ) CHARACTER SET=utf8mb4;
                CREATE INDEX IF NOT EXISTS `IX_CvExtractionRuns_OwnerUserId_StartedAtUtc`
                    ON `CvExtractionRuns` (`OwnerUserId`(191), `StartedAtUtc`);
                CREATE INDEX IF NOT EXISTS `IX_CvExtractionRuns_ArtifactId`
                    ON `CvExtractionRuns` (`ArtifactId`);
                """);
            return;
        }

        migrationBuilder.Sql("""
            CREATE TABLE IF NOT EXISTS "CvUploadArtifacts" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_CvUploadArtifacts" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "OriginalFileName" TEXT NOT NULL,
                "StoredFileName" TEXT NOT NULL,
                "MimeType" TEXT NOT NULL,
                "ByteSize" INTEGER NOT NULL,
                "Sha256" TEXT NOT NULL,
                "StoragePath" TEXT NOT NULL,
                "UploadedAtUtc" TEXT NOT NULL
            );
            CREATE INDEX IF NOT EXISTS "IX_CvUploadArtifacts_OwnerUserId_UploadedAtUtc"
                ON "CvUploadArtifacts" ("OwnerUserId", "UploadedAtUtc");

            CREATE TABLE IF NOT EXISTS "CvExtractionRuns" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_CvExtractionRuns" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "ArtifactId" INTEGER NULL,
                "Trigger" TEXT NOT NULL,
                "ParserVersion" TEXT NOT NULL,
                "NormalizerVersion" TEXT NOT NULL,
                "LlmPromptVersion" TEXT NOT NULL,
                "Status" TEXT NOT NULL,
                "RawExtractedText" TEXT NULL,
                "NormalizedText" TEXT NULL,
                "StructuredProfileJson" TEXT NULL,
                "ErrorMessage" TEXT NULL,
                "StartedAtUtc" TEXT NOT NULL,
                "CompletedAtUtc" TEXT NULL,
                "AppliedAtUtc" TEXT NULL,
                CONSTRAINT "FK_CvExtractionRuns_CvUploadArtifacts_ArtifactId"
                    FOREIGN KEY ("ArtifactId") REFERENCES "CvUploadArtifacts" ("Id") ON DELETE SET NULL
            );
            CREATE INDEX IF NOT EXISTS "IX_CvExtractionRuns_OwnerUserId_StartedAtUtc"
                ON "CvExtractionRuns" ("OwnerUserId", "StartedAtUtc");
            CREATE INDEX IF NOT EXISTS "IX_CvExtractionRuns_ArtifactId"
                ON "CvExtractionRuns" ("ArtifactId");
            """);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // Preserve uploaded-artifact metadata and extraction history from pre-migration installs.
    }
}
