using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace JobTrackerApi.Migrations
{
    /// <inheritdoc />
    public partial class AddPendingEmailChange : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            var mysql = ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase);
            // Identity historically came from startup reconciliation rather than an EF migration.
            // Ensure the base user table exists so a blank standalone EF chain can reach this
            // additive migration. Normal application startup already created it, making this a no-op.
            migrationBuilder.Sql(mysql
                ? """
                    CREATE TABLE IF NOT EXISTS `AspNetUsers` (
                        `Id` varchar(255) NOT NULL,
                        `UserName` varchar(256) NULL,
                        `NormalizedUserName` varchar(256) NULL,
                        `Email` varchar(256) NULL,
                        `NormalizedEmail` varchar(256) NULL,
                        `EmailConfirmed` tinyint(1) NOT NULL,
                        `PasswordHash` longtext NULL,
                        `SecurityStamp` longtext NULL,
                        `ConcurrencyStamp` longtext NULL,
                        `PhoneNumber` longtext NULL,
                        `PhoneNumberConfirmed` tinyint(1) NOT NULL,
                        `TwoFactorEnabled` tinyint(1) NOT NULL,
                        `LockoutEnd` datetime(6) NULL,
                        `LockoutEnabled` tinyint(1) NOT NULL,
                        `AccessFailedCount` int NOT NULL,
                        CONSTRAINT `PK_AspNetUsers` PRIMARY KEY (`Id`)
                    ) CHARACTER SET=utf8mb4;
                    """
                : """
                    CREATE TABLE IF NOT EXISTS "AspNetUsers" (
                        "Id" TEXT NOT NULL CONSTRAINT "PK_AspNetUsers" PRIMARY KEY,
                        "UserName" TEXT NULL,
                        "NormalizedUserName" TEXT NULL,
                        "Email" TEXT NULL,
                        "NormalizedEmail" TEXT NULL,
                        "EmailConfirmed" INTEGER NOT NULL,
                        "PasswordHash" TEXT NULL,
                        "SecurityStamp" TEXT NULL,
                        "ConcurrencyStamp" TEXT NULL,
                        "PhoneNumber" TEXT NULL,
                        "PhoneNumberConfirmed" INTEGER NOT NULL,
                        "TwoFactorEnabled" INTEGER NOT NULL,
                        "LockoutEnd" TEXT NULL,
                        "LockoutEnabled" INTEGER NOT NULL,
                        "AccessFailedCount" INTEGER NOT NULL
                    );
                    """);

            migrationBuilder.AddColumn<string>(
                name: "PendingEmail",
                table: "AspNetUsers",
                type: mysql ? "varchar(320)" : "TEXT",
                maxLength: 320,
                nullable: true);

            migrationBuilder.AddColumn<DateTimeOffset>(
                name: "PendingEmailRequestedAtUtc",
                table: "AspNetUsers",
                type: mysql ? "datetime(6)" : "TEXT",
                nullable: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "PendingEmail",
                table: "AspNetUsers");

            migrationBuilder.DropColumn(
                name: "PendingEmailRequestedAtUtc",
                table: "AspNetUsers");
        }
    }
}
