using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace JobTrackerApi.Migrations
{
    /// <inheritdoc />
    public partial class AddEmailSendAttempts : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
            {
                migrationBuilder.Sql("""
                    CREATE TABLE `EmailSendAttempts` (
                        `Id` char(36) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
                        `OwnerUserId` varchar(255) NOT NULL,
                        `JobApplicationId` int NOT NULL,
                        `Provider` varchar(32) NOT NULL,
                        `ClientRequestId` varchar(128) NOT NULL,
                        `PayloadHash` char(64) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
                        `Status` varchar(32) NOT NULL,
                        `ProviderMessageId` varchar(256) NULL,
                        `FailureCategory` varchar(64) NULL,
                        `CreatedAtUtc` datetime(6) NOT NULL,
                        `StartedAtUtc` datetime(6) NULL,
                        `CompletedAtUtc` datetime(6) NULL,
                        CONSTRAINT `PK_EmailSendAttempts` PRIMARY KEY (`Id`),
                        CONSTRAINT `FK_EmailSendAttempts_JobApplications_JobApplicationId`
                            FOREIGN KEY (`JobApplicationId`) REFERENCES `JobApplications` (`Id`) ON DELETE CASCADE
                    ) CHARACTER SET=utf8mb4;
                    """);
            }
            else
            {
            migrationBuilder.CreateTable(
                name: "EmailSendAttempts",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    OwnerUserId = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
                    JobApplicationId = table.Column<int>(type: "INTEGER", nullable: false),
                    Provider = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                    ClientRequestId = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false),
                    PayloadHash = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                    Status = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
                    ProviderMessageId = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
                    FailureCategory = table.Column<string>(type: "TEXT", maxLength: 64, nullable: true),
                    CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
                    StartedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                    CompletedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_EmailSendAttempts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_EmailSendAttempts_JobApplications_JobApplicationId",
                        column: x => x.JobApplicationId,
                        principalTable: "JobApplications",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });
            }

            migrationBuilder.CreateIndex(
                name: "IX_EmailSendAttempts_JobApplicationId",
                table: "EmailSendAttempts",
                column: "JobApplicationId");

            migrationBuilder.CreateIndex(
                name: "IX_EmailSendAttempts_OwnerUserId_ClientRequestId",
                table: "EmailSendAttempts",
                columns: new[] { "OwnerUserId", "ClientRequestId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_EmailSendAttempts_OwnerUserId_CreatedAtUtc",
                table: "EmailSendAttempts",
                columns: new[] { "OwnerUserId", "CreatedAtUtc" });
        }

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