using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class SchedulingOptimization : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_ScheduleEntries_Classrooms_ClassroomId",
                table: "ScheduleEntries");

            migrationBuilder.AlterColumn<Guid>(
                name: "ClassroomId",
                table: "ScheduleEntries",
                type: "char(36)",
                nullable: true,
                oldClrType: typeof(Guid),
                oldType: "char(36)");

            migrationBuilder.CreateTable(
                name: "ScheduleTimeSlots",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    AcademicTermId = table.Column<Guid>(type: "char(36)", nullable: false),
                    PeriodNumber = table.Column<int>(type: "int", nullable: false),
                    Name = table.Column<string>(type: "varchar(40)", maxLength: 40, nullable: false),
                    StartsAt = table.Column<TimeOnly>(type: "time", nullable: false),
                    EndsAt = table.Column<TimeOnly>(type: "time", nullable: false),
                    IsEnabled = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ScheduleTimeSlots", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ScheduleTimeSlots_AcademicTerms_AcademicTermId",
                        column: x => x.AcademicTermId,
                        principalTable: "AcademicTerms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "TeachingTaskScheduleConstraints",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    TeachingTaskId = table.Column<Guid>(type: "char(36)", nullable: false),
                    RequiresClassroom = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    RequiredCampusId = table.Column<Guid>(type: "char(36)", nullable: true),
                    RequiredBuildingId = table.Column<Guid>(type: "char(36)", nullable: true),
                    AllowedDayOfWeeks = table.Column<string>(type: "varchar(20)", maxLength: 20, nullable: true),
                    EarliestPeriod = table.Column<int>(type: "int", nullable: true),
                    LatestPeriod = table.Column<int>(type: "int", 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_TeachingTaskScheduleConstraints", x => x.Id);
                    table.ForeignKey(
                        name: "FK_TeachingTaskScheduleConstraints_Buildings_RequiredBuildingId",
                        column: x => x.RequiredBuildingId,
                        principalTable: "Buildings",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TeachingTaskScheduleConstraints_Campuses_RequiredCampusId",
                        column: x => x.RequiredCampusId,
                        principalTable: "Campuses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TeachingTaskScheduleConstraints_TeachingTasks_TeachingTaskId",
                        column: x => x.TeachingTaskId,
                        principalTable: "TeachingTasks",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "TeachingTaskAllowedClassrooms",
                columns: table => new
                {
                    TeachingTaskScheduleConstraintId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ClassroomId = table.Column<Guid>(type: "char(36)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TeachingTaskAllowedClassrooms", x => new { x.TeachingTaskScheduleConstraintId, x.ClassroomId });
                    table.ForeignKey(
                        name: "FK_TeachingTaskAllowedClassrooms_Classrooms_ClassroomId",
                        column: x => x.ClassroomId,
                        principalTable: "Classrooms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TeachingTaskAllowedClassrooms_TeachingTaskScheduleConstraint~",
                        column: x => x.TeachingTaskScheduleConstraintId,
                        principalTable: "TeachingTaskScheduleConstraints",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_ScheduleTimeSlots_AcademicTermId_PeriodNumber",
                table: "ScheduleTimeSlots",
                columns: new[] { "AcademicTermId", "PeriodNumber" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskAllowedClassrooms_ClassroomId",
                table: "TeachingTaskAllowedClassrooms",
                column: "ClassroomId");

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskScheduleConstraints_RequiredBuildingId",
                table: "TeachingTaskScheduleConstraints",
                column: "RequiredBuildingId");

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskScheduleConstraints_RequiredCampusId",
                table: "TeachingTaskScheduleConstraints",
                column: "RequiredCampusId");

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskScheduleConstraints_TeachingTaskId",
                table: "TeachingTaskScheduleConstraints",
                column: "TeachingTaskId",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_ScheduleEntries_Classrooms_ClassroomId",
                table: "ScheduleEntries",
                column: "ClassroomId",
                principalTable: "Classrooms",
                principalColumn: "Id",
                onDelete: ReferentialAction.SetNull);
        }

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

            migrationBuilder.DropTable(
                name: "ScheduleTimeSlots");

            migrationBuilder.DropTable(
                name: "TeachingTaskAllowedClassrooms");

            migrationBuilder.DropTable(
                name: "TeachingTaskScheduleConstraints");

            migrationBuilder.AlterColumn<Guid>(
                name: "ClassroomId",
                table: "ScheduleEntries",
                type: "char(36)",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
                oldClrType: typeof(Guid),
                oldType: "char(36)",
                oldNullable: true);

            migrationBuilder.AddForeignKey(
                name: "FK_ScheduleEntries_Classrooms_ClassroomId",
                table: "ScheduleEntries",
                column: "ClassroomId",
                principalTable: "Classrooms",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }
    }
}
