using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class TeachingTasks : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "TeachingTasks",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    TaskNumber = table.Column<string>(type: "varchar(40)", maxLength: 40, nullable: false),
                    Name = table.Column<string>(type: "varchar(120)", maxLength: 120, nullable: false),
                    AcademicTermId = table.Column<Guid>(type: "char(36)", nullable: false),
                    CourseId = table.Column<Guid>(type: "char(36)", nullable: false),
                    Capacity = table.Column<int>(type: "int", nullable: false),
                    StartWeek = table.Column<int>(type: "int", nullable: false),
                    EndWeek = table.Column<int>(type: "int", nullable: false),
                    WeeklyHours = table.Column<int>(type: "int", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    Notes = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true),
                    PublishedAt = 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_TeachingTasks", x => x.Id);
                    table.ForeignKey(
                        name: "FK_TeachingTasks_AcademicTerms_AcademicTermId",
                        column: x => x.AcademicTermId,
                        principalTable: "AcademicTerms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TeachingTasks_Courses_CourseId",
                        column: x => x.CourseId,
                        principalTable: "Courses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "TeachingTaskClasses",
                columns: table => new
                {
                    TeachingTaskId = table.Column<Guid>(type: "char(36)", nullable: false),
                    AdministrativeClassId = table.Column<Guid>(type: "char(36)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TeachingTaskClasses", x => new { x.TeachingTaskId, x.AdministrativeClassId });
                    table.ForeignKey(
                        name: "FK_TeachingTaskClasses_AdministrativeClasses_AdministrativeClas~",
                        column: x => x.AdministrativeClassId,
                        principalTable: "AdministrativeClasses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TeachingTaskClasses_TeachingTasks_TeachingTaskId",
                        column: x => x.TeachingTaskId,
                        principalTable: "TeachingTasks",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "TeachingTaskTeachers",
                columns: table => new
                {
                    TeachingTaskId = table.Column<Guid>(type: "char(36)", nullable: false),
                    TeacherId = table.Column<Guid>(type: "char(36)", nullable: false),
                    IsPrimary = table.Column<bool>(type: "tinyint(1)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TeachingTaskTeachers", x => new { x.TeachingTaskId, x.TeacherId });
                    table.ForeignKey(
                        name: "FK_TeachingTaskTeachers_Teachers_TeacherId",
                        column: x => x.TeacherId,
                        principalTable: "Teachers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TeachingTaskTeachers_TeachingTasks_TeachingTaskId",
                        column: x => x.TeachingTaskId,
                        principalTable: "TeachingTasks",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskClasses_AdministrativeClassId",
                table: "TeachingTaskClasses",
                column: "AdministrativeClassId");

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTasks_AcademicTermId_Status",
                table: "TeachingTasks",
                columns: new[] { "AcademicTermId", "Status" });

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTasks_CourseId",
                table: "TeachingTasks",
                column: "CourseId");

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTasks_TaskNumber",
                table: "TeachingTasks",
                column: "TaskNumber",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskTeachers_TeacherId",
                table: "TeachingTaskTeachers",
                column: "TeacherId");
        }

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

            migrationBuilder.DropTable(
                name: "TeachingTaskTeachers");

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