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

#nullable disable

namespace JobTrackerApi.Migrations;

/// <summary>
/// Moves job-specific tailored CV drafts into migration ownership without replacing existing
/// generated or manually edited document content.
/// </summary>
[DbContext(typeof(JobTrackerContext))]
[Migration("20260830126000_AdoptTailoredCvDraftSchema")]
public sealed class AdoptTailoredCvDraftSchema : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
        {
            migrationBuilder.Sql("""
                CREATE TABLE IF NOT EXISTS `TailoredCvDrafts` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `JobApplicationId` int NOT NULL,
                    `CanonicalProfileVersion` int NULL,
                    `TemplateId` varchar(100) NOT NULL,
                    `Headline` longtext NULL,
                    `SummaryJson` longtext NULL,
                    `SelectedSkillsJson` longtext NULL,
                    `ExperienceJson` longtext NULL,
                    `EducationJson` longtext NULL,
                    `CustomSectionsJson` longtext NULL,
                    `RenderOptionsJson` longtext NULL,
                    `GenerationContextHash` longtext NULL,
                    `LastGeneratedAtUtc` datetime(6) NULL,
                    `LastEditedAtUtc` datetime(6) NULL,
                    `Status` varchar(100) NOT NULL,
                    PRIMARY KEY (`Id`),
                    CONSTRAINT `FK_TailoredCvDrafts_JobApplications_JobApplicationId`
                        FOREIGN KEY (`JobApplicationId`) REFERENCES `JobApplications` (`Id`) ON DELETE CASCADE
                ) CHARACTER SET=utf8mb4;
                CREATE UNIQUE INDEX IF NOT EXISTS `IX_TailoredCvDrafts_OwnerUserId_JobApplicationId`
                    ON `TailoredCvDrafts` (`OwnerUserId`(191), `JobApplicationId`);
                CREATE INDEX IF NOT EXISTS `IX_TailoredCvDrafts_JobApplicationId`
                    ON `TailoredCvDrafts` (`JobApplicationId`);
                """);
            return;
        }

        migrationBuilder.Sql("""
            CREATE TABLE IF NOT EXISTS "TailoredCvDrafts" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_TailoredCvDrafts" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "JobApplicationId" INTEGER NOT NULL,
                "CanonicalProfileVersion" INTEGER NULL,
                "TemplateId" TEXT NOT NULL,
                "Headline" TEXT NULL,
                "SummaryJson" TEXT NULL,
                "SelectedSkillsJson" TEXT NULL,
                "ExperienceJson" TEXT NULL,
                "EducationJson" TEXT NULL,
                "CustomSectionsJson" TEXT NULL,
                "RenderOptionsJson" TEXT NULL,
                "GenerationContextHash" TEXT NULL,
                "LastGeneratedAtUtc" TEXT NULL,
                "LastEditedAtUtc" TEXT NULL,
                "Status" TEXT NOT NULL,
                CONSTRAINT "FK_TailoredCvDrafts_JobApplications_JobApplicationId"
                    FOREIGN KEY ("JobApplicationId") REFERENCES "JobApplications" ("Id") ON DELETE CASCADE
            );
            CREATE UNIQUE INDEX IF NOT EXISTS "IX_TailoredCvDrafts_OwnerUserId_JobApplicationId"
                ON "TailoredCvDrafts" ("OwnerUserId", "JobApplicationId");
            CREATE INDEX IF NOT EXISTS "IX_TailoredCvDrafts_JobApplicationId"
                ON "TailoredCvDrafts" ("JobApplicationId");
            """);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // Preserve tailored document content that may pre-date migration ownership.
    }
}
