<?php

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

/**
 * Per-business AI provider configuration. A business may keep more than one
 * named profile (e.g. 'default' and 'fast'); 'default' is the one consumers
 * resolve unless they ask for another. The business_vertical column drives the
 * "smart" persona/grounding/actions layer (vet clinic, retail, restaurant, ...).
 */
class CreateAiProvidersTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('ai_providers')) return;

        Schema::create('ai_providers', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id')->index();
            $table->string('name', 50)->default('default'); // profile name

            // Business type / vertical → tailors persona, grounding and tools.
            $table->string('business_vertical', 40)->default('general');

            $table->string('provider', 20)->default('gemini'); // anthropic|openai|openai_compatible|gemini
            $table->string('base_url', 255)->nullable();
            $table->text('api_key')->nullable();               // stored encrypted via the model cast
            $table->string('model', 100)->nullable();
            $table->boolean('enabled')->default(false);
            $table->decimal('temperature', 3, 2)->default(0.40);
            $table->unsignedInteger('max_tokens')->default(1024);
            $table->unsignedInteger('monthly_token_budget')->nullable();
            $table->text('system_prompt')->nullable();         // optional override of the vertical persona
            $table->json('meta')->nullable();
            $table->timestamps();

            $table->unique(['business_id', 'name']);
        });
    }

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