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

#nullable disable

namespace JobTrackerApi.Migrations;

/// <summary>
/// Establishes explicit migration ownership for append-only AI interaction history while retaining
/// rows provisioned by the historical compatibility bootstrap or startup reconciler.
/// </summary>
[DbContext(typeof(JobTrackerContext))]
[Migration("20260830129000_AdoptAiInteractionSchema")]
public sealed class AdoptAiInteractionSchema : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
        {
            migrationBuilder.Sql("""
                CREATE TABLE IF NOT EXISTS `AiInteractions` (
                    `Id` int NOT NULL AUTO_INCREMENT,
                    `OwnerUserId` varchar(255) NOT NULL,
                    `JobApplicationId` int NOT NULL,
                    `Module` varchar(64) NOT NULL,
                    `Mode` varchar(64) NULL,
                    `Title` varchar(255) NOT NULL,
                    `Provider` varchar(100) NOT NULL,
                    `ResultJson` longtext NOT NULL,
                    `InputCharacterCount` int NOT NULL DEFAULT 0,
                    `OutputCharacterCount` int NOT NULL DEFAULT 0,
                    `EstimatedTokenCount` int NOT NULL DEFAULT 0,
                    `CreatedAtUtc` datetime(6) NOT NULL,
                    PRIMARY KEY (`Id`),
                    CONSTRAINT `FK_AiInteractions_JobApplications_JobApplicationId`
                        FOREIGN KEY (`JobApplicationId`) REFERENCES `JobApplications` (`Id`) ON DELETE CASCADE
                ) CHARACTER SET=utf8mb4;
                CREATE INDEX IF NOT EXISTS `IX_AiInteractions_JobApplicationId`
                    ON `AiInteractions` (`JobApplicationId`);
                CREATE INDEX IF NOT EXISTS `IX_AiInteractions_Owner_Job_Module_Created`
                    ON `AiInteractions` (`OwnerUserId`, `JobApplicationId`, `Module`, `CreatedAtUtc`);
                """);
            return;
        }

        migrationBuilder.Sql("""
            CREATE TABLE IF NOT EXISTS "AiInteractions" (
                "Id" INTEGER NOT NULL CONSTRAINT "PK_AiInteractions" PRIMARY KEY AUTOINCREMENT,
                "OwnerUserId" TEXT NOT NULL,
                "JobApplicationId" INTEGER NOT NULL,
                "Module" TEXT NOT NULL,
                "Mode" TEXT NULL,
                "Title" TEXT NOT NULL,
                "Provider" TEXT NOT NULL,
                "ResultJson" TEXT NOT NULL,
                "InputCharacterCount" INTEGER NOT NULL DEFAULT 0,
                "OutputCharacterCount" INTEGER NOT NULL DEFAULT 0,
                "EstimatedTokenCount" INTEGER NOT NULL DEFAULT 0,
                "CreatedAtUtc" TEXT NOT NULL,
                CONSTRAINT "FK_AiInteractions_JobApplications_JobApplicationId"
                    FOREIGN KEY ("JobApplicationId") REFERENCES "JobApplications" ("Id") ON DELETE CASCADE
            );
            CREATE INDEX IF NOT EXISTS "IX_AiInteractions_JobApplicationId"
                ON "AiInteractions" ("JobApplicationId");
            CREATE INDEX IF NOT EXISTS "IX_AiInteractions_Owner_Job_Module_Created"
                ON "AiInteractions" ("OwnerUserId", "JobApplicationId", "Module", "CreatedAtUtc");
            """);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // Preserve append-only AI history that may pre-date explicit migration ownership.
    }
}
