using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace JobTrackerApi.Migrations
{
    /// <inheritdoc />
    public partial class AddUserOperations : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // This table is EF-owned. The migration branches explicitly because SQLite-scaffolded
            // TEXT/INTEGER types are not a safe MariaDB contract; do not duplicate it in the startup reconciler.
            if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
            {
                migrationBuilder.Sql("""
                    CREATE TABLE `UserOperations` (
                        `Id` char(36) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
                        `OwnerUserId` varchar(255) NOT NULL,
                        `TaskType` varchar(64) NOT NULL,
                        `IdempotencyKey` varchar(128) NOT NULL,
                        `Status` varchar(32) NOT NULL,
                        `Priority` int NOT NULL,
                        `EntitlementDecision` varchar(32) NOT NULL,
                        `PrivacyPolicy` varchar(32) NOT NULL,
                        `SubjectType` varchar(64) NULL,
                        `SubjectId` varchar(128) NULL,
                        `Provider` varchar(128) NULL,
                        `Model` varchar(128) NULL,
                        `AttemptCount` int NOT NULL,
                        `MaxAttempts` int NOT NULL,
                        `CreatedAtUtc` datetime(6) NOT NULL,
                        `AvailableAtUtc` datetime(6) NOT NULL,
                        `StartedAtUtc` datetime(6) NULL,
                        `CompletedAtUtc` datetime(6) NULL,
                        `DeadlineAtUtc` datetime(6) NULL,
                        `CancellationRequestedAtUtc` datetime(6) NULL,
                        `LeaseToken` char(32) CHARACTER SET ascii COLLATE ascii_general_ci NULL,
                        `LeaseExpiresAtUtc` datetime(6) NULL,
                        `LastHeartbeatAtUtc` datetime(6) NULL,
                        `ProgressStage` varchar(64) NULL,
                        `ProgressPercent` int NULL,
                        `FailureCategory` varchar(64) NULL,
                        `FailureMessage` varchar(512) NULL,
                        `ResultReference` varchar(256) NULL,
                        CONSTRAINT `PK_UserOperations` PRIMARY KEY (`Id`)
                    ) CHARACTER SET=utf8mb4;
                    """);
            }
            else
            {
                migrationBuilder.CreateTable(
                name: "UserOperations",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    OwnerUserId = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
                    TaskType = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                    IdempotencyKey = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                    Status = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                    Priority = table.Column<int>(type: "INTEGER", nullable: false),
                    EntitlementDecision = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                    PrivacyPolicy = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                    SubjectType = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true),
                    SubjectId = table.Column<string>(type: "TEXT", maxLength: 128, nullable: true),
                    Provider = table.Column<string>(type: "TEXT", maxLength: 128, nullable: true),
                    Model = table.Column<string>(type: "TEXT", maxLength: 128, nullable: true),
                    AttemptCount = table.Column<int>(type: "INTEGER", nullable: false),
                    MaxAttempts = table.Column<int>(type: "INTEGER", nullable: false),
                    CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
                    AvailableAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
                    StartedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                    CompletedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                    DeadlineAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                    CancellationRequestedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                    LeaseToken = table.Column<string>(type: "TEXT", maxLength: 32, nullable: true),
                    LeaseExpiresAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                    LastHeartbeatAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                    ProgressStage = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true),
                    ProgressPercent = table.Column<int>(type: "INTEGER", nullable: true),
                    FailureCategory = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true),
                    FailureMessage = table.Column<string>(type: "TEXT", maxLength: 512, nullable: true),
                    ResultReference = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UserOperations", x => x.Id);
                });
            }

            migrationBuilder.CreateIndex(
                name: "IX_UserOperations_OwnerUserId_TaskType_IdempotencyKey",
                table: "UserOperations",
                columns: new[] { "OwnerUserId", "TaskType", "IdempotencyKey" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_UserOperations_Status_AvailableAtUtc_Priority",
                table: "UserOperations",
                columns: new[] { "Status", "AvailableAtUtc", "Priority" });
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "UserOperations");
        }
    }
}
