using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
    /// <inheritdoc />
    public partial class AttendanceCheckInAudit : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "AttendanceCheckInAttempts",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(36)", nullable: false),
                    AttendanceSheetId = table.Column<Guid>(type: "char(36)", nullable: false),
                    StudentId = table.Column<Guid>(type: "char(36)", nullable: false),
                    CheckInMethod = table.Column<int>(type: "int", nullable: false),
                    IsSuccessful = table.Column<bool>(type: "tinyint(1)", nullable: false),
                    FailureCode = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: true),
                    DeviceIdentifierHash = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: true),
                    DevicePlatform = table.Column<string>(type: "varchar(32)", maxLength: 32, nullable: true),
                    IpAddress = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: true),
                    UserAgent = table.Column<string>(type: "varchar(500)", maxLength: 500, nullable: true),
                    RiskFlags = table.Column<string>(type: "varchar(300)", maxLength: 300, nullable: true),
                    Latitude = table.Column<decimal>(type: "decimal(10,7)", precision: 10, scale: 7, nullable: true),
                    Longitude = table.Column<decimal>(type: "decimal(10,7)", precision: 10, scale: 7, nullable: true),
                    AccuracyMeters = table.Column<double>(type: "double", nullable: true),
                    DistanceMeters = table.Column<double>(type: "double", 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_AttendanceCheckInAttempts", x => x.Id);
                    table.ForeignKey(
                        name: "FK_AttendanceCheckInAttempts_AttendanceSheets_AttendanceSheetId",
                        column: x => x.AttendanceSheetId,
                        principalTable: "AttendanceSheets",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_AttendanceCheckInAttempts_Students_StudentId",
                        column: x => x.StudentId,
                        principalTable: "Students",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                })
                .Annotation("MySQL:Charset", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_AttendanceCheckInAttempts_AttendanceSheetId_StudentId_Create~",
                table: "AttendanceCheckInAttempts",
                columns: new[] { "AttendanceSheetId", "StudentId", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_AttendanceCheckInAttempts_DeviceIdentifierHash_CreatedAt",
                table: "AttendanceCheckInAttempts",
                columns: new[] { "DeviceIdentifierHash", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_AttendanceCheckInAttempts_IpAddress_CreatedAt",
                table: "AttendanceCheckInAttempts",
                columns: new[] { "IpAddress", "CreatedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_AttendanceCheckInAttempts_StudentId",
                table: "AttendanceCheckInAttempts",
                column: "StudentId");
        }

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