<?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('ai_chats', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('user_id')->constrained()->cascadeOnDelete();
            $t->string('title')->default('Conversație nouă');
            $t->string('provider')->default('claude');  // claude / gpt / gemini
            $t->timestamps();

            $t->index(['company_id', 'user_id', 'updated_at']);
        });

        Schema::create('ai_messages', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->foreignId('ai_chat_id')->constrained()->cascadeOnDelete();
            $t->string('role');                          // system / user / assistant
            $t->longText('content');
            $t->json('meta')->nullable();                // tokens, model, latency_ms
            $t->timestamps();

            $t->index(['company_id', 'ai_chat_id', 'created_at']);
        });
    }

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