<?php

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

/**
 * PRC-1143 — konsolide şema: `ai_response_logs` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('ai_response_logs', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('ai_staff_profile_id');
            $t->unsignedBigInteger('ticket_id');
            $t->unsignedBigInteger('ticket_reply_id')->nullable();
            $t->enum('mode', ['autonomous','draft'])->default('draft');
            $t->decimal('confidence_score', 4, 3)->default(0.000);
            $t->text('original_response');
            $t->text('edited_response')->nullable();
            $t->enum('feedback', ['approved','rejected','edited'])->nullable();
            $t->text('rejection_reason')->nullable();
            $t->json('context_sources')->nullable();
            $t->integer('prompt_tokens')->default(0);
            $t->integer('completion_tokens')->default(0);
            $t->integer('embedding_tokens')->default(0);
            $t->decimal('total_cost_usd', 10, 6)->default(0.000000);
            $t->string('model_used', 255)->nullable();
            $t->integer('response_time_ms')->default(0);
            $t->boolean('waiting_message_sent')->default(0);
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index(['ai_staff_profile_id', 'created_at'], 'ai_response_logs_ai_staff_profile_id_created_at_index');
            $t->index(['mode', 'feedback'], 'ai_response_logs_mode_feedback_index');
            $t->index('ticket_id', 'ai_response_logs_ticket_id_index');
            $t->foreign('ai_staff_profile_id', 'ai_response_logs_ai_staff_profile_id_foreign')->references('id')->on('ai_staff_profiles')->onDelete('cascade');
        });
    }

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