using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class GraduationClearance : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "GraduationClearanceBatches",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    Name = table.Column<string>(type: "varchar(120)", maxLength: 120, nullable: false),
                    GraduationYear = 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),
                    ClosedAt = 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_GraduationClearanceBatches", x => x.Id);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "GraduationClearanceItems",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    GraduationClearanceBatchId = table.Column<Guid>(type: "char(36)", nullable: false),
                    Code = table.Column<string>(type: "varchar(30)", maxLength: 30, nullable: false),
                    Name = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false),
                    ResponsibleUnit = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false),
                    ResponsibleRole = table.Column<string>(type: "varchar(30)", maxLength: 30, nullable: false),
                    IsRequired = table.Column<bool>(type: "tinyint(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_GraduationClearanceItems", x => x.Id);
                    table.ForeignKey(
                        name: "FK_GraduationClearanceItems_GraduationClearanceBatches_Graduati~",
                        column: x => x.GraduationClearanceBatchId,
                        principalTable: "GraduationClearanceBatches",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "GraduationClearanceRecords",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    GraduationClearanceItemId = table.Column<Guid>(type: "char(36)", nullable: false),
                    StudentId = table.Column<Guid>(type: "char(36)", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    Notes = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true),
                    CompletedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    CompletedByUserId = table.Column<Guid>(type: "char(36)", 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_GraduationClearanceRecords", x => x.Id);
                    table.ForeignKey(
                        name: "FK_GraduationClearanceRecords_GraduationClearanceItems_Graduati~",
                        column: x => x.GraduationClearanceItemId,
                        principalTable: "GraduationClearanceItems",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_GraduationClearanceRecords_Students_StudentId",
                        column: x => x.StudentId,
                        principalTable: "Students",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_GraduationClearanceBatches_GraduationYear_Status",
                table: "GraduationClearanceBatches",
                columns: new[] { "GraduationYear", "Status" });

            migrationBuilder.CreateIndex(
                name: "IX_GraduationClearanceItems_GraduationClearanceBatchId_Code",
                table: "GraduationClearanceItems",
                columns: new[] { "GraduationClearanceBatchId", "Code" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_GraduationClearanceRecords_GraduationClearanceItemId_Student~",
                table: "GraduationClearanceRecords",
                columns: new[] { "GraduationClearanceItemId", "StudentId" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_GraduationClearanceRecords_StudentId_Status",
                table: "GraduationClearanceRecords",
                columns: new[] { "StudentId", "Status" });
        }

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

            migrationBuilder.DropTable(
                name: "GraduationClearanceItems");

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