using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class AutomaticScheduleJobs : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "AutomaticScheduleJobs",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    SchedulePlanId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ActiveSchedulePlanId = table.Column<Guid>(type: "char(36)", nullable: true),
                    RequestedByUserId = table.Column<Guid>(type: "char(36)", nullable: true),
                    Status = table.Column<int>(type: "int", nullable: false),
                    TotalTasks = table.Column<int>(type: "int", nullable: false),
                    ProcessedTasks = table.Column<int>(type: "int", nullable: false),
                    CreatedEntries = table.Column<int>(type: "int", nullable: false),
                    CompletedTasks = table.Column<int>(type: "int", nullable: false),
                    MessagesJson = table.Column<string>(type: "longtext", nullable: true),
                    ErrorMessage = table.Column<string>(type: "varchar(2000)", maxLength: 2000, nullable: true),
                    StartedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    CompletedAt = table.Column<DateTime>(type: "datetime(6)", 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_AutomaticScheduleJobs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AutomaticScheduleJobs_AspNetUsers_RequestedByUserId",
                        column: x => x.RequestedByUserId,
                        principalTable: "AspNetUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "FK_AutomaticScheduleJobs_SchedulePlans_SchedulePlanId",
                        column: x => x.SchedulePlanId,
                        principalTable: "SchedulePlans",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_AutomaticScheduleJobs_ActiveSchedulePlanId",
                table: "AutomaticScheduleJobs",
                column: "ActiveSchedulePlanId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_AutomaticScheduleJobs_RequestedByUserId",
                table: "AutomaticScheduleJobs",
                column: "RequestedByUserId");

            migrationBuilder.CreateIndex(
                name: "IX_AutomaticScheduleJobs_SchedulePlanId_CreatedAt",
                table: "AutomaticScheduleJobs",
                columns: new[] { "SchedulePlanId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_AutomaticScheduleJobs_Status_CreatedAt",
                table: "AutomaticScheduleJobs",
                columns: new[] { "Status", "CreatedAt" });
        }

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