using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class UnifiedMessageCenter : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "Category",
                table: "Notifications",
                type: "int",
                nullable: false,
                defaultValue: 1);

            migrationBuilder.AddColumn<Guid>(
                name: "MessageDispatchId",
                table: "Notifications",
                type: "char(36)",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "MessageDispatches",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    SenderUserId = table.Column<Guid>(type: "char(36)", nullable: false),
                    SenderName = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false),
                    Title = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false),
                    Content = table.Column<string>(type: "varchar(1000)", maxLength: 1000, nullable: false),
                    Category = table.Column<int>(type: "int", nullable: false),
                    AudienceType = table.Column<int>(type: "int", nullable: false),
                    AudienceId = table.Column<Guid>(type: "char(36)", nullable: true),
                    AudienceName = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: false),
                    RecipientCount = table.Column<int>(type: "int", nullable: false),
                    LinkUrl = table.Column<string>(type: "varchar(300)", maxLength: 300, nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MessageDispatches", x => x.Id);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_Notifications_MessageDispatchId",
                table: "Notifications",
                column: "MessageDispatchId");

            migrationBuilder.CreateIndex(
                name: "IX_Notifications_UserId_Category_CreatedAt",
                table: "Notifications",
                columns: new[] { "UserId", "Category", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_MessageDispatches_SenderUserId_CreatedAt",
                table: "MessageDispatches",
                columns: new[] { "SenderUserId", "CreatedAt" });

            migrationBuilder.AddForeignKey(
                name: "FK_Notifications_MessageDispatches_MessageDispatchId",
                table: "Notifications",
                column: "MessageDispatchId",
                principalTable: "MessageDispatches",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Notifications_MessageDispatches_MessageDispatchId",
                table: "Notifications");

            migrationBuilder.DropTable(
                name: "MessageDispatches");

            migrationBuilder.DropIndex(
                name: "IX_Notifications_MessageDispatchId",
                table: "Notifications");

            migrationBuilder.DropIndex(
                name: "IX_Notifications_UserId_Category_CreatedAt",
                table: "Notifications");

            migrationBuilder.DropColumn(
                name: "Category",
                table: "Notifications");

            migrationBuilder.DropColumn(
                name: "MessageDispatchId",
                table: "Notifications");
        }
    }
}
