<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasTable('attendance_management_events')) {
            Schema::create('attendance_management_events', function (Blueprint $table) {
                $table->id();
                $table->string('person_type', 32)->default('student');
                $table->unsignedBigInteger('person_id')->nullable();
                $table->string('person_name')->nullable();
                $table->string('role_grade', 128)->nullable();
                $table->string('badge_id', 128)->nullable()->index();
                $table->date('event_date')->index();
                $table->string('attendance_status', 48)->index();
                $table->string('report_status', 48)->default('pending_clarification')->index();
                $table->dateTime('entry_time')->nullable();
                $table->dateTime('exit_time')->nullable();
                $table->dateTime('official_entry_time')->nullable();
                $table->dateTime('official_exit_time')->nullable();
                $table->string('entry_method', 48)->nullable();
                $table->string('exit_method', 48)->nullable();
                $table->string('manual_reason')->nullable();
                $table->string('reason')->nullable();
                $table->string('scan_location')->nullable();
                $table->string('exit_location')->nullable();
                $table->string('authorized_by')->nullable();
                $table->unsignedBigInteger('entered_by')->nullable();
                $table->unsignedInteger('absence_count')->default(0);
                $table->unsignedInteger('late_count')->default(0);
                $table->unsignedInteger('early_dismissal_count')->default(0);
                $table->unsignedInteger('badge_exception_count')->default(0);
                $table->string('combination_code')->nullable();
                $table->string('risk_level', 32)->default('low');
                $table->boolean('follow_up_required')->default(false)->index();
                $table->boolean('follow_up_completed')->default(false)->index();
                $table->string('action_needed')->nullable();
                $table->text('notes')->nullable();
                $table->string('final_decision')->nullable();
                $table->json('audit')->nullable();
                $table->timestamps();

                $table->index(['person_type', 'person_id', 'event_date'], 'ame_person_date_idx');
                $table->index(['attendance_status', 'report_status'], 'ame_status_report_idx');
            });
        }

        if (! Schema::hasTable('badge_exceptions')) {
            Schema::create('badge_exceptions', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('attendance_management_event_id')->nullable();
                $table->string('person_type', 32)->default('student');
                $table->unsignedBigInteger('person_id')->nullable();
                $table->string('person_name')->nullable();
                $table->date('exception_date')->index();
                $table->string('exception_type', 64);
                $table->string('badge_status', 64)->nullable();
                $table->unsignedInteger('exception_count')->default(1);
                $table->string('action')->nullable();
                $table->string('decision')->nullable();
                $table->unsignedBigInteger('recorded_by')->nullable();
                $table->text('notes')->nullable();
                $table->timestamps();

                $table->index(['person_type', 'person_id', 'exception_date'], 'be_person_date_idx');
            });
        }

        if (! Schema::hasTable('late_slip_reprints')) {
            Schema::create('late_slip_reprints', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('attendance_management_event_id')->nullable();
                $table->unsignedBigInteger('late_slip_log_id')->nullable();
                $table->unsignedBigInteger('student_id')->nullable();
                $table->string('student_name')->nullable();
                $table->string('slip_number')->nullable();
                $table->dateTime('reprinted_at');
                $table->unsignedBigInteger('reprinted_by')->nullable();
                $table->string('reason')->nullable();
                $table->unsignedInteger('reprint_count')->default(1);
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('late_slip_reprints');
        Schema::dropIfExists('badge_exceptions');
        Schema::dropIfExists('attendance_management_events');
    }
};
