using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class CourseGradeStatistics : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "CourseGradeStatistics",
                columns: table => new
                {
                    Id = 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),
                    Scope = table.Column<int>(type: "int", nullable: false),
                    ScopeEntityId = table.Column<Guid>(type: "char(36)", nullable: true),
                    StudentCount = table.Column<int>(type: "int", nullable: false),
                    PassedCount = table.Column<int>(type: "int", nullable: false),
                    Below60Count = table.Column<int>(type: "int", nullable: false),
                    From60To69Count = table.Column<int>(type: "int", nullable: false),
                    From70To79Count = table.Column<int>(type: "int", nullable: false),
                    From80To89Count = table.Column<int>(type: "int", nullable: false),
                    From90To100Count = 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),
                    LowestScore = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: false),
                    PassRate = 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_CourseGradeStatistics", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CourseGradeStatistics_AcademicTerms_AcademicTermId",
                        column: x => x.AcademicTermId,
                        principalTable: "AcademicTerms",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_CourseGradeStatistics_Courses_CourseId",
                        column: x => x.CourseId,
                        principalTable: "Courses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "CourseGradeStatisticsRefreshJobs",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    GradeSheetId = table.Column<Guid>(type: "char(36)", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    StartedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    CompletedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    ErrorMessage = table.Column<string>(type: "varchar(2000)", maxLength: 2000, 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_CourseGradeStatisticsRefreshJobs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CourseGradeStatisticsRefreshJobs_GradeSheets_GradeSheetId",
                        column: x => x.GradeSheetId,
                        principalTable: "GradeSheets",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_CourseGradeStatistics_AcademicTermId_Scope_ScopeEntityId",
                table: "CourseGradeStatistics",
                columns: new[] { "AcademicTermId", "Scope", "ScopeEntityId" });

            migrationBuilder.CreateIndex(
                name: "UX_CourseGradeStatistics_Scope",
                table: "CourseGradeStatistics",
                columns: new[] { "CourseId", "AcademicTermId", "Scope", "ScopeEntityId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_CourseGradeStatisticsRefreshJobs_GradeSheetId",
                table: "CourseGradeStatisticsRefreshJobs",
                column: "GradeSheetId");

            migrationBuilder.CreateIndex(
                name: "IX_CourseGradeStatisticsRefreshJobs_Status_CreatedAt",
                table: "CourseGradeStatisticsRefreshJobs",
                columns: new[] { "Status", "CreatedAt" });
        }

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

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