<?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('tenant_ai_settings', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id')->unique();
            $table->boolean('ai_enabled')->default(true);
            $table->json('allowed_features')->nullable();
            $table->string('default_language', 5)->default('en');
            $table->string('tone_style')->default('professional');
            $table->unsignedInteger('max_daily_ai_requests')->nullable();
            $table->boolean('human_approval_required')->default(true);
            $table->unsignedInteger('ai_data_retention_days')->nullable();
            $table->string('model_override')->nullable();
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
        });

        Schema::create('ai_interactions', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->unsignedBigInteger('user_id')->nullable();
            $table->uuid('conversation_id')->nullable();
            $table->uuid('contact_id')->nullable();
            $table->uuid('lead_id')->nullable();
            $table->string('feature_key');
            $table->string('provider_key');
            $table->string('model_key')->nullable();
            $table->string('prompt_version')->nullable();
            $table->json('input_context_json');
            $table->json('output_json')->nullable();
            $table->decimal('confidence_score', 5, 4)->nullable();
            $table->string('status');
            $table->unsignedInteger('tokens_input')->nullable();
            $table->unsignedInteger('tokens_output')->nullable();
            $table->unsignedInteger('latency_ms')->nullable();
            $table->decimal('cost_amount', 12, 6)->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->index(['tenant_id', 'feature_key', 'created_at']);
        });

        Schema::create('ai_conversation_summaries', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->uuid('conversation_id');
            $table->text('summary_text');
            $table->json('structured_json')->nullable();
            $table->unsignedInteger('source_message_count')->default(0);
            $table->unsignedBigInteger('generated_by_user_id')->nullable();
            $table->string('prompt_version')->nullable();
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->index(['tenant_id', 'conversation_id']);
        });

        Schema::create('ai_intent_classifications', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->uuid('conversation_id');
            $table->uuid('message_id');
            $table->string('primary_intent');
            $table->string('secondary_intent')->nullable();
            $table->decimal('confidence_score', 5, 4)->nullable();
            $table->json('raw_json')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->index(['tenant_id', 'primary_intent']);
        });

        Schema::create('ai_lead_scores', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->uuid('lead_id');
            $table->string('score_label');
            $table->decimal('score_numeric', 5, 2)->nullable();
            $table->json('reasons_json')->nullable();
            $table->string('prompt_version')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->index(['tenant_id', 'score_label']);
        });

        Schema::create('ai_usage_daily', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->string('feature_key');
            $table->date('usage_date');
            $table->unsignedInteger('requests_count')->default(0);
            $table->unsignedInteger('tokens_input')->default(0);
            $table->unsignedInteger('tokens_output')->default(0);
            $table->decimal('estimated_cost', 12, 6)->default(0);
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->unique(['tenant_id', 'feature_key', 'usage_date']);
        });

        Schema::create('ai_knowledge_snippets', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->string('type');
            $table->string('title');
            $table->text('content_text');
            $table->string('language_code', 5)->nullable();
            $table->string('status')->default('active');
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->index(['tenant_id', 'type', 'status']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('ai_knowledge_snippets');
        Schema::dropIfExists('ai_usage_daily');
        Schema::dropIfExists('ai_lead_scores');
        Schema::dropIfExists('ai_intent_classifications');
        Schema::dropIfExists('ai_conversation_summaries');
        Schema::dropIfExists('ai_interactions');
        Schema::dropIfExists('tenant_ai_settings');
    }
};
