<?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
    {
        Schema::create('academy_search_logs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->string('visitor_id', 120)->nullable()->index();
            $table->string('query')->index();
            $table->string('normalized_query')->index();
            $table->unsignedInteger('results_count')->default(0)->index();
            $table->string('clicked_content_type')->nullable()->index();
            $table->unsignedBigInteger('clicked_content_id')->nullable()->index();
            $table->json('filters')->nullable();
            $table->boolean('is_logged_in')->default(false)->index();
            $table->boolean('is_subscriber')->default(false)->index();
            $table->boolean('is_bot')->default(false)->index();
            $table->timestamps();

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

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