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

#nullable disable

namespace JobTrackerApi.Migrations;

/// <summary>
/// Transfers creation ownership of per-user rule settings from startup reconciliation to the
/// migration chain without replacing tables created by older deployments.
/// </summary>
[DbContext(typeof(JobTrackerContext))]
[Migration("20260830121000_AdoptUserRuleSettingsSchema")]
public sealed class AdoptUserRuleSettingsSchema : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
        {
            migrationBuilder.Sql("""
                CREATE TABLE IF NOT EXISTS `UserRuleSettings` (
                    `OwnerUserId` varchar(255) NOT NULL,
                    `AppliedFollowUpDays` int NOT NULL,
                    `AppliedGhostDays` int NOT NULL,
                    `OfferFollowUpDays` int NOT NULL,
                    `OfferGhostDays` int NOT NULL,
                    `FeedbackFollowUpDays` int NOT NULL,
                    `FeedbackGhostDays` int NOT NULL,
                    PRIMARY KEY (`OwnerUserId`)
                ) CHARACTER SET=utf8mb4;
                """);
            return;
        }

        migrationBuilder.Sql("""
            CREATE TABLE IF NOT EXISTS "UserRuleSettings" (
                "OwnerUserId" TEXT NOT NULL CONSTRAINT "PK_UserRuleSettings" PRIMARY KEY,
                "AppliedFollowUpDays" INTEGER NOT NULL,
                "AppliedGhostDays" INTEGER NOT NULL,
                "OfferFollowUpDays" INTEGER NOT NULL,
                "OfferGhostDays" INTEGER NOT NULL,
                "FeedbackFollowUpDays" INTEGER NOT NULL,
                "FeedbackGhostDays" INTEGER NOT NULL
            );
            """);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // Preserve settings that may have been created before migration ownership was introduced.
    }
}
