<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * Create password reset logs table for security auditing.
     */
    public function up(): void
    {
        Schema::create('password_reset_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('set null');
            $table->string('email', 191);
            $table->string('ip_address', 45);
            $table->text('user_agent')->nullable();
            $table->string('token_hash', 191)->nullable();
            $table->boolean('successful')->default(false);
            $table->timestamp('requested_at');
            $table->timestamp('completed_at')->nullable();
            
            $table->index(['email', 'requested_at']);
            $table->index('user_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('password_reset_logs');
    }
};
