using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace JobTrackerApi.Migrations
{
    /// <inheritdoc />
    public partial class AddAccountDeletionLifecycle : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
            {
                migrationBuilder.Sql("""
                    ALTER TABLE `AspNetUsers`
                        ADD COLUMN `DeletionRequestedAtUtc` datetime(6) NULL,
                        ADD COLUMN `DeletionStatus` varchar(32) NOT NULL DEFAULT 'active';

                    CREATE TABLE `AccountDeletionRequests` (
                        `Id` char(36) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
                        `OwnerUserId` varchar(255) NOT NULL,
                        `OwnerKey` char(64) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
                        `RequestedByUserId` varchar(255) NOT NULL,
                        `Status` varchar(32) NOT NULL,
                        `Stage` varchar(32) NOT NULL,
                        `AttemptCount` int NOT NULL,
                        `DatabaseRowCount` int NOT NULL,
                        `FileCount` int NOT NULL,
                        `WarningJson` longtext NULL,
                        `LastErrorCategory` varchar(64) NULL,
                        `LastErrorMessage` varchar(1024) NULL,
                        `RequestedAtUtc` datetime(6) NOT NULL,
                        `StartedAtUtc` datetime(6) NULL,
                        `CompletedAtUtc` datetime(6) NULL,
                        CONSTRAINT `PK_AccountDeletionRequests` PRIMARY KEY (`Id`)
                    ) CHARACTER SET=utf8mb4;

                    CREATE TABLE `AccountDeletionFiles` (
                        `Id` bigint NOT NULL AUTO_INCREMENT,
                        `AccountDeletionRequestId` char(36) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
                        `Category` varchar(64) NOT NULL,
                        `OriginalPath` longtext NOT NULL,
                        `QuarantinePath` longtext NOT NULL,
                        `Status` varchar(32) NOT NULL,
                        `ByteSize` bigint NOT NULL,
                        `Sha256` char(64) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
                        CONSTRAINT `PK_AccountDeletionFiles` PRIMARY KEY (`Id`),
                        CONSTRAINT `FK_AccountDeletionFiles_Request`
                            FOREIGN KEY (`AccountDeletionRequestId`) REFERENCES `AccountDeletionRequests` (`Id`) ON DELETE CASCADE
                    ) CHARACTER SET=utf8mb4;
                    """);
            }
            else
            {
            migrationBuilder.AddColumn<DateTimeOffset>(
                name: "DeletionRequestedAtUtc",
                table: "AspNetUsers",
                type: "TEXT",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "DeletionStatus",
                table: "AspNetUsers",
                type: "TEXT",
                maxLength: 32,
                nullable: false,
                defaultValue: "active");

            migrationBuilder.CreateTable(
                name: "AccountDeletionRequests",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    OwnerUserId = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
                    OwnerKey = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                    RequestedByUserId = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
                    Status = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                    Stage = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                    AttemptCount = table.Column<int>(type: "INTEGER", nullable: false),
                    DatabaseRowCount = table.Column<int>(type: "INTEGER", nullable: false),
                    FileCount = table.Column<int>(type: "INTEGER", nullable: false),
                    WarningJson = table.Column<string>(type: "TEXT", nullable: true),
                    LastErrorCategory = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true),
                    LastErrorMessage = table.Column<string>(type: "TEXT", nullable: true),
                    RequestedAtUtc = table.Column<DateTimeOffset>(type: "TEXT", nullable: false),
                    StartedAtUtc = table.Column<DateTimeOffset>(type: "TEXT", nullable: true),
                    CompletedAtUtc = table.Column<DateTimeOffset>(type: "TEXT", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AccountDeletionRequests", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "AccountDeletionFiles",
                columns: table => new
                {
                    Id = table.Column<long>(type: "INTEGER", nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    AccountDeletionRequestId = table.Column<Guid>(type: "TEXT", nullable: false),
                    Category = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                    OriginalPath = table.Column<string>(type: "TEXT", nullable: false),
                    QuarantinePath = table.Column<string>(type: "TEXT", nullable: false),
                    Status = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                    ByteSize = table.Column<long>(type: "INTEGER", nullable: false),
                    Sha256 = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AccountDeletionFiles", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AccountDeletionFiles_AccountDeletionRequests_AccountDeletionRequestId",
                        column: x => x.AccountDeletionRequestId,
                        principalTable: "AccountDeletionRequests",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });
            }

            migrationBuilder.CreateIndex(
                name: "IX_AccountDeletionFiles_AccountDeletionRequestId_Status",
                table: "AccountDeletionFiles",
                columns: new[] { "AccountDeletionRequestId", "Status" });

            migrationBuilder.CreateIndex(
                name: "IX_AccountDeletionRequests_OwnerUserId_Status",
                table: "AccountDeletionRequests",
                columns: new[] { "OwnerUserId", "Status" });

            migrationBuilder.CreateIndex(
                name: "IX_AccountDeletionRequests_Status_RequestedAtUtc",
                table: "AccountDeletionRequests",
                columns: new[] { "Status", "RequestedAtUtc" });
        }

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

            migrationBuilder.DropTable(
                name: "AccountDeletionRequests");

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

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