<?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
    {
        // Tabel pentru setări CAPTCHA
        Schema::create('captcha_settings', function (Blueprint $table) {
            $table->id();
            $table->enum('provider', ['hcaptcha', 'recaptcha_v2', 'recaptcha_v2_invisible', 'recaptcha_v3', 'turnstile', 'trustip'])
                  ->default('hcaptcha');
            $table->boolean('enabled')->default(false);
            $table->string('site_key')->nullable();
            $table->string('secret_key')->nullable();
            $table->string('api_key')->nullable(); // Pentru TrustIP

            // Configurări specifice
            $table->decimal('recaptcha_v3_threshold', 2, 1)->default(0.5); // Score minim pentru v3
            $table->boolean('invisible_badge_hide')->default(false);

            // Locații unde este activ
            $table->boolean('enabled_login')->default(true);
            $table->boolean('enabled_register')->default(true);
            $table->boolean('enabled_forgot_password')->default(true);
            $table->boolean('enabled_contact')->default(false);
            $table->boolean('enabled_api')->default(false);

            // Whitelist
            $table->json('whitelist_ips')->nullable();

            // Testing
            $table->boolean('testing_mode')->default(false);
            $table->string('test_key')->nullable(); // Key special pentru bypass în testing

            // Fallback
            $table->enum('fallback_provider', ['hcaptcha', 'recaptcha_v2', 'recaptcha_v2_invisible', 'recaptcha_v3', 'turnstile', 'trustip', 'none'])
                  ->default('none');
            $table->string('fallback_site_key')->nullable();
            $table->string('fallback_secret_key')->nullable();

            $table->timestamps();
        });

        // Tabel pentru statistici CAPTCHA
        Schema::create('captcha_statistics', function (Blueprint $table) {
            $table->id();
            $table->enum('provider', ['hcaptcha', 'recaptcha_v2', 'recaptcha_v2_invisible', 'recaptcha_v3', 'turnstile', 'trustip']);
            $table->string('location'); // login, register, contact, etc.
            $table->ipAddress('ip_address')->nullable();
            $table->string('user_agent')->nullable();
            $table->boolean('success');
            $table->decimal('score', 2, 1)->nullable(); // Pentru reCAPTCHA v3
            $table->string('error_code')->nullable();
            $table->text('error_message')->nullable();
            $table->integer('response_time_ms')->nullable(); // Timp de răspuns în milisecunde
            $table->json('metadata')->nullable(); // Date suplimentare
            $table->timestamp('verified_at');
            $table->timestamps();

            $table->index(['provider', 'location', 'created_at']);
            $table->index(['success', 'created_at']);
            $table->index('ip_address');
        });

        // Tabel pentru rate limiting per IP
        Schema::create('captcha_rate_limits', function (Blueprint $table) {
            $table->id();
            $table->ipAddress('ip_address');
            $table->string('location');
            $table->integer('attempts')->default(1);
            $table->integer('failed_attempts')->default(0);
            $table->timestamp('first_attempt_at')->nullable();
            $table->timestamp('last_attempt_at')->nullable();
            $table->timestamp('blocked_until')->nullable();
            $table->timestamps();

            $table->unique(['ip_address', 'location']);
            $table->index('blocked_until');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('captcha_rate_limits');
        Schema::dropIfExists('captcha_statistics');
        Schema::dropIfExists('captcha_settings');
    }
};
