using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class PublishedTimetableOccurrences : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "PublishedScheduleOccurrences",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    SchedulePlanId = table.Column<Guid>(type: "char(36)", nullable: false),
                    AcademicTermId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ScheduleEntryId = table.Column<Guid>(type: "char(36)", nullable: false),
                    TeachingTaskId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ClassroomId = table.Column<Guid>(type: "char(36)", nullable: true),
                    Week = table.Column<int>(type: "int", nullable: false),
                    DayOfWeek = table.Column<int>(type: "int", nullable: false),
                    StartPeriod = table.Column<int>(type: "int", nullable: false),
                    PeriodCount = table.Column<int>(type: "int", nullable: false),
                    Kind = 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_PublishedScheduleOccurrences", x => x.Id);
                    table.ForeignKey(
                        name: "FK_PublishedScheduleOccurrences_Classrooms_ClassroomId",
                        column: x => x.ClassroomId,
                        principalTable: "Classrooms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.SetNull);
                    table.ForeignKey(
                        name: "FK_PublishedScheduleOccurrences_ScheduleEntries_ScheduleEntryId",
                        column: x => x.ScheduleEntryId,
                        principalTable: "ScheduleEntries",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_PublishedScheduleOccurrences_TeachingTasks_TeachingTaskId",
                        column: x => x.TeachingTaskId,
                        principalTable: "TeachingTasks",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_PublishedScheduleOccurrences_AcademicTermId_TeachingTaskId_W~",
                table: "PublishedScheduleOccurrences",
                columns: new[] { "AcademicTermId", "TeachingTaskId", "Week" });

            migrationBuilder.CreateIndex(
                name: "IX_PublishedScheduleOccurrences_ClassroomId",
                table: "PublishedScheduleOccurrences",
                column: "ClassroomId");

            migrationBuilder.CreateIndex(
                name: "IX_PublishedScheduleOccurrences_ScheduleEntryId_Week",
                table: "PublishedScheduleOccurrences",
                columns: new[] { "ScheduleEntryId", "Week" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_PublishedScheduleOccurrences_SchedulePlanId_ClassroomId_Week~",
                table: "PublishedScheduleOccurrences",
                columns: new[] { "SchedulePlanId", "ClassroomId", "Week", "DayOfWeek", "StartPeriod" });

            migrationBuilder.CreateIndex(
                name: "IX_PublishedScheduleOccurrences_TeachingTaskId",
                table: "PublishedScheduleOccurrences",
                column: "TeachingTaskId");
        }

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