<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('rate_limits', function (Blueprint $table) {
            $table->id();
            $table->string('key')->unique()->index(); // Unique identifier for the rate limit
            $table->string('endpoint'); // Endpoint or action being rate limited
            $table->integer('limit'); // Maximum requests allowed
            $table->integer('window_seconds'); // Time window in seconds
            $table->integer('current_count')->default(0); // Current request count
            $table->timestamp('last_reset')->useCurrent(); // Last reset time
            $table->timestamp('blocked_until')->nullable(); // When the limit is blocked until
            $table->timestamps();
            
            // Indexes for performance
            $table->index(['endpoint', 'last_reset']);
            $table->index('blocked_until');
        });
    }

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