<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('search_embeddings', function (Blueprint $table) {
            $table->id();
            $table->enum('type', ['ai_tool', 'ai_tool_keyword', 'qna']);

            $table->foreignId('category_id')->nullable();
            $table->foreignId('ai_tool_id')->nullable();

            $table->text('query');
            $table->vector('embedding', 384)->nullable();

            $table->timestamps();

            $table->foreign('ai_tool_id')->references('id')->on('ai_tools');
            $table->foreign('category_id')->references('id')->on('categories');

        });

        DB::statement('CREATE INDEX search_embeddings_embedding_idx ON search_embeddings USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');

    }

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