<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('login_history', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('academy_id')->constrained('academies');
            $table->string('ip_address', 45);
            $table->text('user_agent')->nullable();
            $table->string('status', 20);
            $table->string('failed_reason')->nullable();
            $table->timestamp('created_at')->nullable();

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

        DB::statement("ALTER TABLE login_history ADD CONSTRAINT login_history_status_check CHECK (status IN ('success', 'failed', 'locked', 'blocked'))");
    }

    public function down(): void
    {
        Schema::dropIfExists('login_history');
    }
};
