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

#nullable disable

namespace JobTrackerApi.Migrations;

/// <summary>
/// Moves CV variants and append-only version history into migration ownership while preserving
/// existing builder settings, public slugs, and job-tailoring relationships.
/// </summary>
[DbContext(typeof(JobTrackerContext))]
[Migration("20260830128000_AdoptCvVariantSchema")]
public sealed class AdoptCvVariantSchema : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
        {
            migrationBuilder.Sql("""
                CREATE TABLE IF NOT EXISTS `CvVariants` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `PublicSlug` varchar(64) NOT NULL,
                    `Name` varchar(255) NOT NULL,
                    `JobApplicationId` int NULL,
                    `SettingsJson` longtext NOT NULL,
                    `IsPublic` tinyint(1) NOT NULL,
                    `Version` int NOT NULL,
                    `CreatedAtUtc` datetime(6) NOT NULL,
                    `UpdatedAtUtc` datetime(6) NOT NULL,
                    PRIMARY KEY (`Id`),
                    CONSTRAINT `FK_CvVariants_JobApplications_JobApplicationId`
                        FOREIGN KEY (`JobApplicationId`) REFERENCES `JobApplications` (`Id`) ON DELETE SET NULL
                ) CHARACTER SET=utf8mb4;
                CREATE INDEX IF NOT EXISTS `IX_CvVariants_JobApplicationId`
                    ON `CvVariants` (`JobApplicationId`);
                CREATE INDEX IF NOT EXISTS `IX_CvVariants_OwnerUserId_UpdatedAtUtc`
                    ON `CvVariants` (`OwnerUserId`, `UpdatedAtUtc`);
                CREATE UNIQUE INDEX IF NOT EXISTS `IX_CvVariants_PublicSlug`
                    ON `CvVariants` (`PublicSlug`);

                CREATE TABLE IF NOT EXISTS `CvVariantVersions` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `CvVariantId` int NOT NULL,
                    `Version` int NOT NULL,
                    `SettingsJson` longtext NOT NULL,
                    `Source` varchar(100) NOT NULL,
                    `CreatedAtUtc` datetime(6) NOT NULL,
                    PRIMARY KEY (`Id`),
                    CONSTRAINT `FK_CvVariantVersions_CvVariants_CvVariantId`
                        FOREIGN KEY (`CvVariantId`) REFERENCES `CvVariants` (`Id`) ON DELETE CASCADE
                ) CHARACTER SET=utf8mb4;
                CREATE INDEX IF NOT EXISTS `IX_CvVariantVersions_CvVariantId_Version`
                    ON `CvVariantVersions` (`CvVariantId`, `Version`);
                """);
            return;
        }

        migrationBuilder.Sql("""
            CREATE TABLE IF NOT EXISTS "CvVariants" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_CvVariants" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "PublicSlug" TEXT NOT NULL,
                "Name" TEXT NOT NULL,
                "JobApplicationId" INTEGER NULL,
                "SettingsJson" TEXT NOT NULL,
                "IsPublic" INTEGER NOT NULL,
                "Version" INTEGER NOT NULL,
                "CreatedAtUtc" TEXT NOT NULL,
                "UpdatedAtUtc" TEXT NOT NULL,
                CONSTRAINT "FK_CvVariants_JobApplications_JobApplicationId"
                    FOREIGN KEY ("JobApplicationId") REFERENCES "JobApplications" ("Id") ON DELETE SET NULL
            );
            CREATE INDEX IF NOT EXISTS "IX_CvVariants_JobApplicationId"
                ON "CvVariants" ("JobApplicationId");
            CREATE INDEX IF NOT EXISTS "IX_CvVariants_OwnerUserId_UpdatedAtUtc"
                ON "CvVariants" ("OwnerUserId", "UpdatedAtUtc");
            CREATE UNIQUE INDEX IF NOT EXISTS "IX_CvVariants_PublicSlug"
                ON "CvVariants" ("PublicSlug");

            CREATE TABLE IF NOT EXISTS "CvVariantVersions" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_CvVariantVersions" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "CvVariantId" INTEGER NOT NULL,
                "Version" INTEGER NOT NULL,
                "SettingsJson" TEXT NOT NULL,
                "Source" TEXT NOT NULL,
                "CreatedAtUtc" TEXT NOT NULL,
                CONSTRAINT "FK_CvVariantVersions_CvVariants_CvVariantId"
                    FOREIGN KEY ("CvVariantId") REFERENCES "CvVariants" ("Id") ON DELETE CASCADE
            );
            CREATE INDEX IF NOT EXISTS "IX_CvVariantVersions_CvVariantId_Version"
                ON "CvVariantVersions" ("CvVariantId", "Version");
            """);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // Preserve CV documents and revision history created before migration ownership.
    }
}
