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

#nullable disable

namespace JobTrackerApi.Migrations;

/// <summary>
/// Moves persisted job-workspace AI outputs into migration ownership without replacing existing
/// interview preparation, candidate-fit, or focus-plan content.
/// </summary>
[DbContext(typeof(JobTrackerContext))]
[Migration("20260830127000_AdoptJobWorkspaceNotesSchema")]
public sealed class AdoptJobWorkspaceNotesSchema : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
        {
            migrationBuilder.Sql("""
                CREATE TABLE IF NOT EXISTS `InterviewPrepNotes` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `JobApplicationId` int NOT NULL,
                    `AttachmentContextSignature` longtext NOT NULL,
                    `Summary` longtext NOT NULL,
                    `TalkingPointsJson` longtext NOT NULL,
                    `LikelyQuestionsJson` longtext NOT NULL,
                    `WeakSpotsJson` longtext NOT NULL,
                    `GeneratedAtUtc` datetime(6) NOT NULL,
                    PRIMARY KEY (`Id`),
                    CONSTRAINT `FK_InterviewPrepNotes_JobApplications_JobApplicationId`
                        FOREIGN KEY (`JobApplicationId`) REFERENCES `JobApplications` (`Id`) ON DELETE CASCADE
                ) CHARACTER SET=utf8mb4;
                CREATE UNIQUE INDEX IF NOT EXISTS `IX_InterviewPrepNotes_OwnerUserId_JobApplicationId`
                    ON `InterviewPrepNotes` (`OwnerUserId`(191), `JobApplicationId`);

                CREATE TABLE IF NOT EXISTS `AiWorkspaceNotes` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `JobApplicationId` int NOT NULL,
                    `NoteType` varchar(50) NOT NULL,
                    `AttachmentContextSignature` longtext NOT NULL,
                    `ResultJson` longtext NOT NULL,
                    `GeneratedAtUtc` datetime(6) NOT NULL,
                    PRIMARY KEY (`Id`),
                    CONSTRAINT `FK_AiWorkspaceNotes_JobApplications_JobApplicationId`
                        FOREIGN KEY (`JobApplicationId`) REFERENCES `JobApplications` (`Id`) ON DELETE CASCADE
                ) CHARACTER SET=utf8mb4;
                CREATE UNIQUE INDEX IF NOT EXISTS `IX_AiWorkspaceNotes_OwnerUserId_JobApplicationId_NoteType`
                    ON `AiWorkspaceNotes` (`OwnerUserId`(191), `JobApplicationId`, `NoteType`);
                """);
            return;
        }

        migrationBuilder.Sql("""
            CREATE TABLE IF NOT EXISTS "InterviewPrepNotes" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_InterviewPrepNotes" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "JobApplicationId" INTEGER NOT NULL,
                "AttachmentContextSignature" TEXT NOT NULL,
                "Summary" TEXT NOT NULL,
                "TalkingPointsJson" TEXT NOT NULL,
                "LikelyQuestionsJson" TEXT NOT NULL,
                "WeakSpotsJson" TEXT NOT NULL,
                "GeneratedAtUtc" TEXT NOT NULL,
                CONSTRAINT "FK_InterviewPrepNotes_JobApplications_JobApplicationId"
                    FOREIGN KEY ("JobApplicationId") REFERENCES "JobApplications" ("Id") ON DELETE CASCADE
            );
            CREATE UNIQUE INDEX IF NOT EXISTS "IX_InterviewPrepNotes_OwnerUserId_JobApplicationId"
                ON "InterviewPrepNotes" ("OwnerUserId", "JobApplicationId");

            CREATE TABLE IF NOT EXISTS "AiWorkspaceNotes" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_AiWorkspaceNotes" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "JobApplicationId" INTEGER NOT NULL,
                "NoteType" TEXT NOT NULL,
                "AttachmentContextSignature" TEXT NOT NULL,
                "ResultJson" TEXT NOT NULL,
                "GeneratedAtUtc" TEXT NOT NULL,
                CONSTRAINT "FK_AiWorkspaceNotes_JobApplications_JobApplicationId"
                    FOREIGN KEY ("JobApplicationId") REFERENCES "JobApplications" ("Id") ON DELETE CASCADE
            );
            CREATE UNIQUE INDEX IF NOT EXISTS "IX_AiWorkspaceNotes_OwnerUserId_JobApplicationId_NoteType"
                ON "AiWorkspaceNotes" ("OwnerUserId", "JobApplicationId", "NoteType");
            """);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // Preserve generated and user-reviewed job-workspace notes from older installations.
    }
}
