<?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('forum_spam_learning')) {
            Schema::create('forum_spam_learning', function (Blueprint $table): void {
                $table->id();
                $table->string('content_hash', 64)->index();
                $table->string('decision', 32)->index();
                $table->string('pattern_signature', 191)->nullable()->index();
                $table->foreignId('reviewed_by')->nullable()->constrained('users')->nullOnDelete();
                $table->json('metadata')->nullable();
                $table->timestamp('created_at')->useCurrent();
            });
        }

        if (!Schema::hasTable('forum_ai_logs')) {
            Schema::create('forum_ai_logs', function (Blueprint $table): void {
                $table->id();
                $table->foreignId('post_id')->constrained('forum_posts')->cascadeOnDelete();
                $table->unsignedSmallInteger('ai_score')->default(0);
                $table->unsignedSmallInteger('behavior_score')->default(0);
                $table->unsignedSmallInteger('link_score')->default(0);
                $table->integer('learning_score')->default(0);
                $table->unsignedSmallInteger('firewall_score')->default(0);
                $table->unsignedSmallInteger('bot_risk_score')->default(0);
                $table->unsignedSmallInteger('risk_score')->default(0)->index();
                $table->string('decision', 32)->default('allow')->index();
                $table->string('provider', 64)->nullable()->index();
                $table->string('source_ip_hash', 64)->nullable()->index();
                $table->json('metadata')->nullable();
                $table->timestamp('created_at')->useCurrent();

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

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