using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class OfficialDocuments : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "OfficialDocuments",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    DocumentNumber = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false),
                    VerificationCodeHash = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: false),
                    Type = table.Column<int>(type: "int", nullable: false),
                    Status = table.Column<int>(type: "int", nullable: false),
                    StudentId = table.Column<Guid>(type: "char(36)", nullable: false),
                    IssuedByUserId = table.Column<Guid>(type: "char(36)", nullable: false),
                    IssuedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
                    Purpose = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true),
                    SnapshotJson = table.Column<string>(type: "longtext", nullable: false),
                    PdfContent = table.Column<byte[]>(type: "longblob", nullable: false),
                    PdfSha256 = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: false),
                    InvalidatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
                    InvalidatedByUserId = table.Column<Guid>(type: "char(36)", nullable: true),
                    InvalidationReason = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true),
                    ReissuedFromDocumentId = 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_OfficialDocuments", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OfficialDocuments_AspNetUsers_InvalidatedByUserId",
                        column: x => x.InvalidatedByUserId,
                        principalTable: "AspNetUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_OfficialDocuments_AspNetUsers_IssuedByUserId",
                        column: x => x.IssuedByUserId,
                        principalTable: "AspNetUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_OfficialDocuments_OfficialDocuments_ReissuedFromDocumentId",
                        column: x => x.ReissuedFromDocumentId,
                        principalTable: "OfficialDocuments",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_OfficialDocuments_Students_StudentId",
                        column: x => x.StudentId,
                        principalTable: "Students",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "OfficialDocumentDownloads",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    OfficialDocumentId = table.Column<Guid>(type: "char(36)", nullable: false),
                    DownloadedByUserId = table.Column<Guid>(type: "char(36)", nullable: false),
                    IpAddress = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: true),
                    UserAgent = 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_OfficialDocumentDownloads", x => x.Id);
                    table.ForeignKey(
                        name: "FK_OfficialDocumentDownloads_AspNetUsers_DownloadedByUserId",
                        column: x => x.DownloadedByUserId,
                        principalTable: "AspNetUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_OfficialDocumentDownloads_OfficialDocuments_OfficialDocument~",
                        column: x => x.OfficialDocumentId,
                        principalTable: "OfficialDocuments",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_OfficialDocumentDownloads_DownloadedByUserId_CreatedAt",
                table: "OfficialDocumentDownloads",
                columns: new[] { "DownloadedByUserId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_OfficialDocumentDownloads_OfficialDocumentId_CreatedAt",
                table: "OfficialDocumentDownloads",
                columns: new[] { "OfficialDocumentId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_OfficialDocuments_DocumentNumber",
                table: "OfficialDocuments",
                column: "DocumentNumber",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_OfficialDocuments_InvalidatedByUserId",
                table: "OfficialDocuments",
                column: "InvalidatedByUserId");

            migrationBuilder.CreateIndex(
                name: "IX_OfficialDocuments_IssuedByUserId",
                table: "OfficialDocuments",
                column: "IssuedByUserId");

            migrationBuilder.CreateIndex(
                name: "IX_OfficialDocuments_ReissuedFromDocumentId",
                table: "OfficialDocuments",
                column: "ReissuedFromDocumentId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_OfficialDocuments_Status_IssuedAt",
                table: "OfficialDocuments",
                columns: new[] { "Status", "IssuedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_OfficialDocuments_StudentId_IssuedAt",
                table: "OfficialDocuments",
                columns: new[] { "StudentId", "IssuedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_OfficialDocuments_VerificationCodeHash",
                table: "OfficialDocuments",
                column: "VerificationCodeHash",
                unique: true);
        }

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

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