using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace JobTrackerApi.Migrations
{
    /// <inheritdoc />
    public partial class AddEmailDrafts : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
            {
                migrationBuilder.Sql("""
                    CREATE TABLE `EmailDrafts` (
                        `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,
                        `To` varchar(320) NOT NULL,
                        `Subject` varchar(998) NOT NULL,
                        `BodyText` longtext NOT NULL,
                        `ThreadId` varchar(512) NULL,
                        `Revision` bigint NOT NULL,
                        `CreatedAtUtc` datetime(6) NOT NULL,
                        `UpdatedAtUtc` datetime(6) NOT NULL,
                        CONSTRAINT `PK_EmailDrafts` PRIMARY KEY (`Id`),
                        CONSTRAINT `FK_EmailDrafts_JobApplications_JobApplicationId`
                            FOREIGN KEY (`JobApplicationId`) REFERENCES `JobApplications` (`Id`) ON DELETE CASCADE
                    ) CHARACTER SET=utf8mb4;
                    """);
            }
            else
            {
            migrationBuilder.CreateTable(
                name: "EmailDrafts",
                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),
                    To = table.Column<string>(type: "TEXT", maxLength: 320, nullable: false),
                    Subject = table.Column<string>(type: "TEXT", maxLength: 998, nullable: false),
                    BodyText = table.Column<string>(type: "TEXT", nullable: false),
                    ThreadId = table.Column<string>(type: "TEXT", maxLength: 512, nullable: true),
                    Revision = table.Column<long>(type: "INTEGER", nullable: false),
                    CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
                    UpdatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_EmailDrafts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_EmailDrafts_JobApplications_JobApplicationId",
                        column: x => x.JobApplicationId,
                        principalTable: "JobApplications",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });
            }

            migrationBuilder.CreateIndex(
                name: "IX_EmailDrafts_JobApplicationId",
                table: "EmailDrafts",
                column: "JobApplicationId");

            migrationBuilder.CreateIndex(
                name: "IX_EmailDrafts_OwnerUserId_JobApplicationId_UpdatedAtUtc",
                table: "EmailDrafts",
                columns: new[] { "OwnerUserId", "JobApplicationId", "UpdatedAtUtc" });
        }

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