<?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('mobile_access_tokens')) {
            Schema::create('mobile_access_tokens', function (Blueprint $table): void {
                $table->id();
                $table->unsignedBigInteger('user_id');
                $table->string('token_hash', 64)->unique();
                $table->string('device_uid', 190);
                $table->string('device_name', 190)->nullable();
                $table->string('platform', 40)->nullable();
                $table->string('app_version', 40)->nullable();
                $table->string('ip_address', 45)->nullable();
                $table->text('user_agent')->nullable();
                $table->timestamp('last_used_at')->nullable();
                $table->timestamp('expires_at')->nullable();
                $table->timestamp('revoked_at')->nullable();
                $table->string('revoked_reason', 120)->nullable();
                $table->json('metadata')->nullable();
                $table->timestamps();

                $table->index(['user_id', 'revoked_at']);
                $table->index(['device_uid', 'revoked_at']);
            });
        }

        if (! Schema::hasTable('student_device_sessions')) {
            Schema::create('student_device_sessions', function (Blueprint $table): void {
                $table->id();
                $table->unsignedBigInteger('user_id');
                $table->unsignedBigInteger('mobile_access_token_id')->nullable();
                $table->uuid('session_key')->unique();
                $table->string('device_uid', 190);
                $table->string('device_name', 190)->nullable();
                $table->string('platform', 40)->nullable();
                $table->string('app_version', 40)->nullable();
                $table->string('ip_address', 45)->nullable();
                $table->text('user_agent')->nullable();
                $table->string('status', 40)->default('active');
                $table->timestamp('login_at');
                $table->timestamp('last_seen_at')->nullable();
                $table->timestamp('logout_at')->nullable();
                $table->string('logout_reason', 120)->nullable();
                $table->json('metadata')->nullable();
                $table->timestamps();

                $table->index(['user_id', 'status']);
                $table->index(['device_uid', 'status']);
                $table->index('mobile_access_token_id');
            });
        }

        if (! Schema::hasTable('student_exam_sessions')) {
            Schema::create('student_exam_sessions', function (Blueprint $table): void {
                $table->id();
                $table->unsignedBigInteger('user_id');
                $table->unsignedBigInteger('competition_assignment_id');
                $table->unsignedBigInteger('competition_id')->nullable();
                $table->unsignedBigInteger('question_set_id')->nullable();
                $table->unsignedBigInteger('student_device_session_id')->nullable();
                $table->unsignedBigInteger('mobile_access_token_id')->nullable();
                $table->uuid('session_key')->unique();
                $table->string('status', 40)->default('active');
                $table->unsignedInteger('duration_minutes')->default(25);
                $table->unsignedInteger('elapsed_seconds')->default(0);
                $table->unsignedInteger('warning_count')->default(0);
                $table->unsignedInteger('suspicious_event_count')->default(0);
                $table->json('draft_answers')->nullable();
                $table->json('metadata')->nullable();
                $table->timestamp('started_at');
                $table->timestamp('last_heartbeat_at')->nullable();
                $table->timestamp('submitted_at')->nullable();
                $table->timestamp('auto_submitted_at')->nullable();
                $table->string('submit_reason', 120)->nullable();
                $table->timestamps();

                $table->index(['user_id', 'status']);
                $table->index(['competition_assignment_id', 'status']);
                $table->index('student_device_session_id');
                $table->index('mobile_access_token_id');
            });
        }

        if (! Schema::hasTable('student_activity_logs')) {
            Schema::create('student_activity_logs', function (Blueprint $table): void {
                $table->id();
                $table->unsignedBigInteger('user_id');
                $table->unsignedBigInteger('student_device_session_id')->nullable();
                $table->unsignedBigInteger('student_exam_session_id')->nullable();
                $table->string('event_type', 120);
                $table->string('level', 30)->default('info');
                $table->string('source', 40)->default('app');
                $table->string('ip_address', 45)->nullable();
                $table->text('user_agent')->nullable();
                $table->json('payload')->nullable();
                $table->timestamp('occurred_at');
                $table->timestamps();

                $table->index(['user_id', 'occurred_at']);
                $table->index(['student_exam_session_id', 'occurred_at']);
                $table->index(['event_type', 'occurred_at']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('student_activity_logs');
        Schema::dropIfExists('student_exam_sessions');
        Schema::dropIfExists('student_device_sessions');
        Schema::dropIfExists('mobile_access_tokens');
    }
};
