using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class ExperimentGradeManagement : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<DateTime>(
                name: "SourceSnapshotAt",
                table: "GradeItems",
                type: "datetime(6)",
                nullable: true);

            migrationBuilder.AddColumn<int>(
                name: "SourceType",
                table: "GradeItems",
                type: "int",
                nullable: false,
                defaultValue: 1);

            migrationBuilder.CreateTable(
                name: "ExperimentGradeSheets",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExperimentProjectId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ContributionWeight = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: false),
                    PassScore = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    ReviewComment = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true),
                    SubmittedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    ReviewedAt = table.Column<DateTime>(type: "datetime(6)", 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_ExperimentGradeSheets", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ExperimentGradeSheets_ExperimentProjects_ExperimentProjectId",
                        column: x => x.ExperimentProjectId,
                        principalTable: "ExperimentProjects",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "ExperimentGradeItems",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExperimentGradeSheetId = table.Column<Guid>(type: "char(36)", nullable: false),
                    Name = table.Column<string>(type: "varchar(60)", maxLength: 60, nullable: false),
                    Kind = table.Column<int>(type: "int", nullable: false),
                    Weight = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, 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_ExperimentGradeItems", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ExperimentGradeItems_ExperimentGradeSheets_ExperimentGradeSh~",
                        column: x => x.ExperimentGradeSheetId,
                        principalTable: "ExperimentGradeSheets",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "ExperimentGradeRecords",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExperimentGradeSheetId = table.Column<Guid>(type: "char(36)", nullable: false),
                    StudentId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExperimentSessionId = table.Column<Guid>(type: "char(36)", nullable: true),
                    ParticipationStatus = table.Column<int>(type: "int", nullable: false),
                    TotalScore = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: true),
                    IsPassed = table.Column<bool>(type: "tinyint(1)", nullable: true),
                    SafetyViolation = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    AttemptNumber = table.Column<int>(type: "int", nullable: false),
                    SubmissionReference = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true),
                    SubmittedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    IsLate = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    TeacherComment = table.Column<string>(type: "varchar(500)", maxLength: 500, 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_ExperimentGradeRecords", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ExperimentGradeRecords_ExperimentGradeSheets_ExperimentGrade~",
                        column: x => x.ExperimentGradeSheetId,
                        principalTable: "ExperimentGradeSheets",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ExperimentGradeRecords_ExperimentSessions_ExperimentSessionId",
                        column: x => x.ExperimentSessionId,
                        principalTable: "ExperimentSessions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ExperimentGradeRecords_Students_StudentId",
                        column: x => x.StudentId,
                        principalTable: "Students",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "ExperimentGradeItemScores",
                columns: table => new
                {
                    ExperimentGradeRecordId = table.Column<Guid>(type: "char(36)", nullable: false),
                    ExperimentGradeItemId = table.Column<Guid>(type: "char(36)", nullable: false),
                    Score = table.Column<decimal>(type: "decimal(5,1)", precision: 5, scale: 1, nullable: true),
                    Comment = table.Column<string>(type: "varchar(300)", maxLength: 300, nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ExperimentGradeItemScores", x => new { x.ExperimentGradeRecordId, x.ExperimentGradeItemId });
                    table.ForeignKey(
                        name: "FK_ExperimentGradeItemScores_ExperimentGradeItems_ExperimentGra~",
                        column: x => x.ExperimentGradeItemId,
                        principalTable: "ExperimentGradeItems",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ExperimentGradeItemScores_ExperimentGradeRecords_ExperimentG~",
                        column: x => x.ExperimentGradeRecordId,
                        principalTable: "ExperimentGradeRecords",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentGradeItems_ExperimentGradeSheetId_SortOrder",
                table: "ExperimentGradeItems",
                columns: new[] { "ExperimentGradeSheetId", "SortOrder" });

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentGradeItemScores_ExperimentGradeItemId",
                table: "ExperimentGradeItemScores",
                column: "ExperimentGradeItemId");

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentGradeRecords_ExperimentGradeSheetId_StudentId",
                table: "ExperimentGradeRecords",
                columns: new[] { "ExperimentGradeSheetId", "StudentId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentGradeRecords_ExperimentSessionId",
                table: "ExperimentGradeRecords",
                column: "ExperimentSessionId");

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentGradeRecords_StudentId",
                table: "ExperimentGradeRecords",
                column: "StudentId");

            migrationBuilder.CreateIndex(
                name: "IX_ExperimentGradeSheets_ExperimentProjectId",
                table: "ExperimentGradeSheets",
                column: "ExperimentProjectId",
                unique: true);
        }

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

            migrationBuilder.DropTable(
                name: "ExperimentGradeItems");

            migrationBuilder.DropTable(
                name: "ExperimentGradeRecords");

            migrationBuilder.DropTable(
                name: "ExperimentGradeSheets");

            migrationBuilder.DropColumn(
                name: "SourceSnapshotAt",
                table: "GradeItems");

            migrationBuilder.DropColumn(
                name: "SourceType",
                table: "GradeItems");
        }
    }
}
