using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace JobTrackerApi.Migrations
{
    /// <inheritdoc />
    public partial class AddUserNotifications : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // EF owns this additive table. Keep MariaDB columns bounded instead of applying
            // the SQLite-scaffolded TEXT types, and do not duplicate it in startup reconciliation.
            if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
            {
                migrationBuilder.Sql("""
                    CREATE TABLE `UserNotifications` (
                        `Id` char(36) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
                        `OwnerUserId` varchar(255) NOT NULL,
                        `OperationId` char(36) CHARACTER SET ascii COLLATE ascii_general_ci NULL,
                        `Kind` varchar(64) NOT NULL,
                        `Title` varchar(160) NOT NULL,
                        `Message` varchar(512) NOT NULL,
                        `LinkPath` varchar(256) NULL,
                        `CreatedAtUtc` datetime(6) NOT NULL,
                        `ReadAtUtc` datetime(6) NULL,
                        `DismissedAtUtc` datetime(6) NULL,
                        CONSTRAINT `PK_UserNotifications` PRIMARY KEY (`Id`),
                        CONSTRAINT `FK_UserNotifications_UserOperations_OperationId`
                            FOREIGN KEY (`OperationId`) REFERENCES `UserOperations` (`Id`) ON DELETE SET NULL
                    ) CHARACTER SET=utf8mb4;
                    """);
            }
            else
            {
                migrationBuilder.CreateTable(
                name: "UserNotifications",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    OwnerUserId = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
                    OperationId = table.Column<Guid>(type: "TEXT", nullable: true),
                    Kind = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
                    Title = table.Column<string>(type: "TEXT", maxLength: 160, nullable: false),
                    Message = table.Column<string>(type: "TEXT", maxLength: 512, nullable: false),
                    LinkPath = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
                    CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
                    ReadAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
                    DismissedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UserNotifications", x => x.Id);
                    table.ForeignKey(
                        name: "FK_UserNotifications_UserOperations_OperationId",
                        column: x => x.OperationId,
                        principalTable: "UserOperations",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.SetNull);
                });
            }

            migrationBuilder.CreateIndex(
                name: "IX_UserNotifications_OperationId",
                table: "UserNotifications",
                column: "OperationId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_UserNotifications_OwnerUserId_DismissedAtUtc_ReadAtUtc_CreatedAtUtc",
                table: "UserNotifications",
                columns: new[] { "OwnerUserId", "DismissedAtUtc", "ReadAtUtc", "CreatedAtUtc" });
        }

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