using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class ExamRoomMixing : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "ExamRooms",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExamPlanId = table.Column<Guid>(type: "char(36)", nullable: false),
                    CourseId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ClassroomId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExamDate = table.Column<DateTime>(type: "date", nullable: false),
                    StartPeriod = table.Column<int>(type: "int", nullable: false),
                    PeriodCount = table.Column<int>(type: "int", nullable: false),
                    StartsAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
                    EndsAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
                    RequiredInvigilatorCount = table.Column<int>(type: "int", 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_ExamRooms", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ExamRooms_Classrooms_ClassroomId",
                        column: x => x.ClassroomId,
                        principalTable: "Classrooms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ExamRooms_Courses_CourseId",
                        column: x => x.CourseId,
                        principalTable: "Courses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ExamRooms_ExamPlans_ExamPlanId",
                        column: x => x.ExamPlanId,
                        principalTable: "ExamPlans",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "ExamRoomInvigilators",
                columns: table => new
                {
                    ExamRoomId = table.Column<Guid>(type: "char(36)", nullable: false),
                    TeacherId = table.Column<Guid>(type: "char(36)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ExamRoomInvigilators", x => new { x.ExamRoomId, x.TeacherId });
                    table.ForeignKey(
                        name: "FK_ExamRoomInvigilators_ExamRooms_ExamRoomId",
                        column: x => x.ExamRoomId,
                        principalTable: "ExamRooms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ExamRoomInvigilators_Teachers_TeacherId",
                        column: x => x.TeacherId,
                        principalTable: "Teachers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "ExamRoomSessions",
                columns: table => new
                {
                    ExamRoomId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExamSessionId = table.Column<Guid>(type: "char(36)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ExamRoomSessions", x => new { x.ExamRoomId, x.ExamSessionId });
                    table.ForeignKey(
                        name: "FK_ExamRoomSessions_ExamRooms_ExamRoomId",
                        column: x => x.ExamRoomId,
                        principalTable: "ExamRooms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ExamRoomSessions_ExamSessions_ExamSessionId",
                        column: x => x.ExamSessionId,
                        principalTable: "ExamSessions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "ExamSeats",
                columns: table => new
                {
                    ExamRoomId = table.Column<Guid>(type: "char(36)", nullable: false),
                    StudentId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExamSessionId = table.Column<Guid>(type: "char(36)", nullable: false),
                    SeatNumber = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ExamSeats", x => new { x.ExamRoomId, x.StudentId });
                    table.ForeignKey(
                        name: "FK_ExamSeats_ExamRooms_ExamRoomId",
                        column: x => x.ExamRoomId,
                        principalTable: "ExamRooms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ExamSeats_ExamSessions_ExamSessionId",
                        column: x => x.ExamSessionId,
                        principalTable: "ExamSessions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ExamSeats_Students_StudentId",
                        column: x => x.StudentId,
                        principalTable: "Students",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_ExamRoomInvigilators_TeacherId",
                table: "ExamRoomInvigilators",
                column: "TeacherId");

            migrationBuilder.CreateIndex(
                name: "IX_ExamRooms_ClassroomId",
                table: "ExamRooms",
                column: "ClassroomId");

            migrationBuilder.CreateIndex(
                name: "IX_ExamRooms_CourseId",
                table: "ExamRooms",
                column: "CourseId");

            migrationBuilder.CreateIndex(
                name: "IX_ExamRooms_Plan_Room_Time",
                table: "ExamRooms",
                columns: new[] { "ExamPlanId", "ClassroomId", "StartsAt" });

            migrationBuilder.CreateIndex(
                name: "IX_ExamRooms_Plan_Time",
                table: "ExamRooms",
                columns: new[] { "ExamPlanId", "StartsAt" });

            migrationBuilder.CreateIndex(
                name: "IX_ExamRoomSessions_SessionId",
                table: "ExamRoomSessions",
                column: "ExamSessionId");

            migrationBuilder.CreateIndex(
                name: "IX_ExamSeats_StudentId",
                table: "ExamSeats",
                column: "StudentId");

            migrationBuilder.CreateIndex(
                name: "UX_ExamSeats_Session_Student",
                table: "ExamSeats",
                columns: new[] { "ExamSessionId", "StudentId" },
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "ExamRoomSessions");

            migrationBuilder.DropTable(
                name: "ExamSeats");

            migrationBuilder.DropTable(
                name: "ExamRooms");
        }
    }
}
