using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class TeachingTaskGradeAnalytics : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "TeachingTaskGradeStatistics",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    GradeSheetId = table.Column<Guid>(type: "char(36)", nullable: false),
                    TeachingTaskId = table.Column<Guid>(type: "char(36)", nullable: false),
                    CourseId = table.Column<Guid>(type: "char(36)", nullable: false),
                    AcademicTermId = table.Column<Guid>(type: "char(36)", nullable: false),
                    StudentCount = table.Column<int>(type: "int", nullable: false),
                    PassedCount = table.Column<int>(type: "int", nullable: false),
                    ExcellentCount = table.Column<int>(type: "int", nullable: false),
                    HighestScore = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: false),
                    AverageScore = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: false),
                    MedianScore = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: false),
                    LowestScore = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: false),
                    StandardDeviation = table.Column<decimal>(type: "decimal(6,2)", precision: 6, scale: 2, nullable: false),
                    PassRate = table.Column<decimal>(type: "decimal(5,2)", precision: 5, scale: 2, nullable: false),
                    ExcellentRate = table.Column<decimal>(type: "decimal(5,2)", precision: 5, scale: 2, nullable: false),
                    CalculatedAt = table.Column<DateTime>(type: "datetime(6)", 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_TeachingTaskGradeStatistics", x => x.Id);
                    table.ForeignKey(
                        name: "FK_TeachingTaskGradeStatistics_AcademicTerms_AcademicTermId",
                        column: x => x.AcademicTermId,
                        principalTable: "AcademicTerms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TeachingTaskGradeStatistics_Courses_CourseId",
                        column: x => x.CourseId,
                        principalTable: "Courses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_TeachingTaskGradeStatistics_GradeSheets_GradeSheetId",
                        column: x => x.GradeSheetId,
                        principalTable: "GradeSheets",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_TeachingTaskGradeStatistics_TeachingTasks_TeachingTaskId",
                        column: x => x.TeachingTaskId,
                        principalTable: "TeachingTasks",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "TeachingTaskGradeScoreBands",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    TeachingTaskGradeStatisticId = table.Column<Guid>(type: "char(36)", nullable: false),
                    Label = table.Column<string>(type: "varchar(30)", maxLength: 30, nullable: false),
                    LowerBound = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: false),
                    UpperBound = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: true),
                    StudentCount = table.Column<int>(type: "int", nullable: false),
                    SortOrder = 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_TeachingTaskGradeScoreBands", x => x.Id);
                    table.ForeignKey(
                        name: "FK_TeachingTaskGradeScoreBands_TeachingTaskGradeStatistics_Teac~",
                        column: x => x.TeachingTaskGradeStatisticId,
                        principalTable: "TeachingTaskGradeStatistics",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskGradeScoreBands_TeachingTaskGradeStatisticId_Sor~",
                table: "TeachingTaskGradeScoreBands",
                columns: new[] { "TeachingTaskGradeStatisticId", "SortOrder" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskGradeStatistics_AcademicTermId",
                table: "TeachingTaskGradeStatistics",
                column: "AcademicTermId");

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskGradeStatistics_CourseId_AcademicTermId",
                table: "TeachingTaskGradeStatistics",
                columns: new[] { "CourseId", "AcademicTermId" });

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskGradeStatistics_GradeSheetId",
                table: "TeachingTaskGradeStatistics",
                column: "GradeSheetId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_TeachingTaskGradeStatistics_TeachingTaskId",
                table: "TeachingTaskGradeStatistics",
                column: "TeachingTaskId",
                unique: true);
        }

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

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