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

#nullable disable

namespace JobTrackerApi.Migrations;

/// <summary>
/// Moves the per-application workflow checklist into migration ownership while preserving stable
/// system keys, manual items, ordering, and completion state.
/// </summary>
[DbContext(typeof(JobTrackerContext))]
[Migration("20260830130000_AdoptApplicationChecklistSchema")]
public sealed class AdoptApplicationChecklistSchema : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
        {
            migrationBuilder.Sql("""
                CREATE TABLE IF NOT EXISTS `ApplicationChecklistItems` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `JobApplicationId` int NOT NULL,
                    `SystemKey` varchar(64) NULL,
                    `AutoSignal` varchar(64) NULL,
                    `Title` varchar(255) NOT NULL,
                    `Description` longtext NULL,
                    `Category` varchar(32) NOT NULL,
                    `Status` varchar(32) NOT NULL,
                    `Section` varchar(64) NULL,
                    `SortOrder` int NOT NULL,
                    `IsSystemGenerated` tinyint(1) NOT NULL,
                    `IsAutoCompleted` tinyint(1) NOT NULL,
                    `CompletedAt` datetime(6) NULL,
                    `CreatedAtUtc` datetime(6) NOT NULL,
                    `UpdatedAtUtc` datetime(6) NOT NULL,
                    PRIMARY KEY (`Id`),
                    CONSTRAINT `FK_ApplicationChecklistItems_JobApplications_JobApplicationId`
                        FOREIGN KEY (`JobApplicationId`) REFERENCES `JobApplications` (`Id`) ON DELETE CASCADE
                ) CHARACTER SET=utf8mb4;
                CREATE UNIQUE INDEX IF NOT EXISTS `IX_ApplicationChecklistItems_JobApplicationId_SystemKey`
                    ON `ApplicationChecklistItems` (`JobApplicationId`, `SystemKey`);
                CREATE INDEX IF NOT EXISTS `IX_ApplicationChecklistItems_Owner_Job_Sort`
                    ON `ApplicationChecklistItems` (`OwnerUserId`, `JobApplicationId`, `SortOrder`);
                """);
            return;
        }

        migrationBuilder.Sql("""
            CREATE TABLE IF NOT EXISTS "ApplicationChecklistItems" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_ApplicationChecklistItems" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "JobApplicationId" INTEGER NOT NULL,
                "SystemKey" TEXT NULL,
                "AutoSignal" TEXT NULL,
                "Title" TEXT NOT NULL,
                "Description" TEXT NULL,
                "Category" TEXT NOT NULL,
                "Status" TEXT NOT NULL,
                "Section" TEXT NULL,
                "SortOrder" INTEGER NOT NULL,
                "IsSystemGenerated" INTEGER NOT NULL,
                "IsAutoCompleted" INTEGER NOT NULL,
                "CompletedAt" TEXT NULL,
                "CreatedAtUtc" TEXT NOT NULL,
                "UpdatedAtUtc" TEXT NOT NULL,
                CONSTRAINT "FK_ApplicationChecklistItems_JobApplications_JobApplicationId"
                    FOREIGN KEY ("JobApplicationId") REFERENCES "JobApplications" ("Id") ON DELETE CASCADE
            );
            CREATE UNIQUE INDEX IF NOT EXISTS "IX_ApplicationChecklistItems_JobApplicationId_SystemKey"
                ON "ApplicationChecklistItems" ("JobApplicationId", "SystemKey");
            CREATE INDEX IF NOT EXISTS "IX_ApplicationChecklistItems_Owner_Job_Sort"
                ON "ApplicationChecklistItems" ("OwnerUserId", "JobApplicationId", "SortOrder");
            """);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // Preserve workflow decisions that may pre-date explicit migration ownership.
    }
}
