<?php

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

/**
 * AI Knowledge Agent — per-workspace toggles for the customer-facing
 * behaviour:
 *
 *  - kb_show_citations    Append a numbered "Sources:" footer to KB-backed
 *                         replies. On by default because grounded answers
 *                         with citations are the headline of the feature.
 *
 *  - kb_fallback_enabled  When AI confidence falls below the workspace's
 *                         confidence_threshold AND no chunks were
 *                         retrieved, replace the LLM output with a canned
 *                         "I don't know" message before the customer sees
 *                         it. Without this, low-confidence answers may be
 *                         confidently wrong — the worst possible failure
 *                         mode for a support agent.
 *
 *  - kb_fallback_message  The exact text used when the fallback fires.
 *                         Nullable — null means "use the platform default
 *                         string" so we can tune the wording without
 *                         needing per-workspace migrations.
 *
 * The existing `escalation_*` fields handle the *internal* response to low
 * confidence (assign + tag + notify). These new fields handle the
 * *external* response (what the customer actually reads). The two are
 * complementary, not redundant: escalation fires regardless of whether
 * we showed the customer the fallback or the raw LLM answer.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('ai_configs', function (Blueprint $table) {
            $table->boolean('kb_show_citations')->default(true)->after('use_bullet_points');
            $table->boolean('kb_fallback_enabled')->default(true)->after('kb_show_citations');
            $table->text('kb_fallback_message')->nullable()->after('kb_fallback_enabled');
        });
    }

    public function down(): void
    {
        Schema::table('ai_configs', function (Blueprint $table) {
            $table->dropColumn(['kb_show_citations', 'kb_fallback_enabled', 'kb_fallback_message']);
        });
    }
};
