<?php

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

/**
 * Knowledge Agent quotas — per-workspace caps so resellers can offer
 * tiered pricing on top of the platform license.
 *
 *   kb_monthly_question_limit  Hard cap on KB-grounded AI answers per
 *                              calendar month. 0 = unlimited. When hit,
 *                              AIManager refuses to consult the KB and
 *                              returns the configured fallback message.
 *
 *   kb_per_answer_cost_cap     Maximum USD that may be spent on a single
 *                              answer. Stops runaway prompts (e.g. KB
 *                              chunks exceed expected size after a bad
 *                              ingestion). 0 = no cap.
 *
 * The existing `monthly_cost_limit` field is workspace-global and
 * applies to ALL AI features. These two are Knowledge-Agent-only so
 * a reseller can sell the Knowledge Agent feature as its own
 * metered upsell separate from the generic AI reply allowance.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('ai_configs', function (Blueprint $table) {
            $table->unsignedInteger('kb_monthly_question_limit')->default(0)->after('kb_fallback_message');
            $table->decimal('kb_per_answer_cost_cap', 8, 4)->default(0)->after('kb_monthly_question_limit');
        });
    }

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