<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('ai_documents', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('filename');
            $table->string('file_type');
            $table->longText('content');
            $table->integer('chunk_count')->default(0);
            $table->timestamps();
        });

        Schema::create('ai_document_chunks', function (Blueprint $table) {
            $table->id();
            $table->foreignId('document_id')->constrained('ai_documents')->onDelete('cascade');
            $table->integer('chunk_index');
            $table->text('content');
            $table->json('embedding')->nullable();
            $table->timestamps();
            
            $table->index('document_id');
        });
    }

    public function down()
    {
        Schema::dropIfExists('ai_document_chunks');
        Schema::dropIfExists('ai_documents');
    }
};