using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class ExperimentManagement : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "ExperimentProjects",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    TeachingTaskId = table.Column<Guid>(type: "char(36)", nullable: false),
                    Code = table.Column<string>(type: "varchar(40)", maxLength: 40, nullable: false),
                    Name = table.Column<string>(type: "varchar(120)", maxLength: 120, nullable: false),
                    ArrangementMode = table.Column<int>(type: "int", nullable: false),
                    Description = table.Column<string>(type: "varchar(1000)", maxLength: 1000, nullable: true),
                    Requirements = table.Column<string>(type: "varchar(1000)", maxLength: 1000, nullable: true),
                    StartDate = table.Column<DateTime>(type: "date", nullable: false),
                    EndDate = table.Column<DateTime>(type: "date", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    PublishedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    ClosedAt = 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_ExperimentProjects", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ExperimentProjects_TeachingTasks_TeachingTaskId",
                        column: x => x.TeachingTaskId,
                        principalTable: "TeachingTasks",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "ExperimentSessions",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExperimentProjectId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ClassroomId = table.Column<Guid>(type: "char(36)", nullable: false),
                    SessionDate = table.Column<DateTime>(type: "date", nullable: false),
                    StartPeriod = table.Column<int>(type: "int", nullable: false),
                    PeriodCount = table.Column<int>(type: "int", nullable: false),
                    Capacity = table.Column<int>(type: "int", nullable: false),
                    ReservedCount = table.Column<int>(type: "int", nullable: false),
                    Notes = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true),
                    Status = table.Column<int>(type: "int", nullable: false),
                    CancelledAt = 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_ExperimentSessions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ExperimentSessions_Classrooms_ClassroomId",
                        column: x => x.ClassroomId,
                        principalTable: "Classrooms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ExperimentSessions_ExperimentProjects_ExperimentProjectId",
                        column: x => x.ExperimentProjectId,
                        principalTable: "ExperimentProjects",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "ExperimentBookings",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExperimentProjectId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExperimentSessionId = table.Column<Guid>(type: "char(36)", nullable: false),
                    StudentId = table.Column<Guid>(type: "char(36)", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    BookedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
                    CancelledAt = 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_ExperimentBookings", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ExperimentBookings_ExperimentProjects_ExperimentProjectId",
                        column: x => x.ExperimentProjectId,
                        principalTable: "ExperimentProjects",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ExperimentBookings_ExperimentSessions_ExperimentSessionId",
                        column: x => x.ExperimentSessionId,
                        principalTable: "ExperimentSessions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ExperimentBookings_Students_StudentId",
                        column: x => x.StudentId,
                        principalTable: "Students",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentBookings_ExperimentProjectId_StudentId",
                table: "ExperimentBookings",
                columns: new[] { "ExperimentProjectId", "StudentId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentBookings_ExperimentSessionId_Status_BookedAt",
                table: "ExperimentBookings",
                columns: new[] { "ExperimentSessionId", "Status", "BookedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentBookings_StudentId",
                table: "ExperimentBookings",
                column: "StudentId");

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentProjects_Status_StartDate_EndDate",
                table: "ExperimentProjects",
                columns: new[] { "Status", "StartDate", "EndDate" });

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentProjects_TeachingTaskId_Code",
                table: "ExperimentProjects",
                columns: new[] { "TeachingTaskId", "Code" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentSessions_ClassroomId_SessionDate_Status_StartPeriod",
                table: "ExperimentSessions",
                columns: new[] { "ClassroomId", "SessionDate", "Status", "StartPeriod" });

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentSessions_ExperimentProjectId_SessionDate_StartPeri~",
                table: "ExperimentSessions",
                columns: new[] { "ExperimentProjectId", "SessionDate", "StartPeriod" });
        }

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

            migrationBuilder.DropTable(
                name: "ExperimentSessions");

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