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

#nullable disable

namespace JobTrackerApi.Migrations;

/// <summary>
/// Moves durable user-owned interview preparation into migration ownership without replacing
/// questions, answers, research, or practice state.
/// </summary>
[DbContext(typeof(JobTrackerContext))]
[Migration("20260830132000_AdoptInterviewPrepItemSchema")]
public sealed class AdoptInterviewPrepItemSchema : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
        {
            migrationBuilder.Sql("""
                CREATE TABLE IF NOT EXISTS `InterviewPrepItems` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `JobApplicationId` int NOT NULL,
                    `Category` varchar(32) NOT NULL,
                    `Title` varchar(500) NOT NULL,
                    `Content` longtext NULL,
                    `Source` varchar(16) NOT NULL,
                    `IsPrepared` tinyint(1) NOT NULL,
                    `SortOrder` int NOT NULL,
                    `CreatedAtUtc` datetime(6) NOT NULL,
                    `UpdatedAtUtc` datetime(6) NOT NULL,
                    PRIMARY KEY (`Id`),
                    CONSTRAINT `FK_InterviewPrepItems_JobApplications_JobApplicationId`
                        FOREIGN KEY (`JobApplicationId`) REFERENCES `JobApplications` (`Id`) ON DELETE CASCADE
                ) CHARACTER SET=utf8mb4;
                CREATE INDEX IF NOT EXISTS `IX_InterviewPrepItems_Owner_Job_Sort`
                    ON `InterviewPrepItems` (`OwnerUserId`, `JobApplicationId`, `SortOrder`);
                """);
            return;
        }

        migrationBuilder.Sql("""
            CREATE TABLE IF NOT EXISTS "InterviewPrepItems" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_InterviewPrepItems" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "JobApplicationId" INTEGER NOT NULL,
                "Category" TEXT NOT NULL,
                "Title" TEXT NOT NULL,
                "Content" TEXT NULL,
                "Source" TEXT NOT NULL,
                "IsPrepared" INTEGER NOT NULL,
                "SortOrder" INTEGER NOT NULL,
                "CreatedAtUtc" TEXT NOT NULL,
                "UpdatedAtUtc" TEXT NOT NULL,
                CONSTRAINT "FK_InterviewPrepItems_JobApplications_JobApplicationId"
                    FOREIGN KEY ("JobApplicationId") REFERENCES "JobApplications" ("Id") ON DELETE CASCADE
            );
            CREATE INDEX IF NOT EXISTS "IX_InterviewPrepItems_Owner_Job_Sort"
                ON "InterviewPrepItems" ("OwnerUserId", "JobApplicationId", "SortOrder");
            """);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // Preserve durable user-authored preparation from pre-migration installations.
    }
}
