<?php

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

/**
 * AI suggestion audit trail. One row per clinician interaction with an AI
 * suggestion — e.g. a SOAP draft generated, then applied (accepted into the
 * record) or dismissed. Lets the usage dashboard report an acceptance rate
 * ("is the assistant actually useful?") and gives a lightweight, timestamped
 * record of when AI output was relied on.
 *
 * Written best-effort by AiFeedback::record(); never blocks a request.
 */
class CreateAiFeedbackTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('ai_feedback')) {
            return;
        }

        Schema::create('ai_feedback', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('user_id')->nullable();
            $table->string('module', 60)->nullable();
            $table->string('feature', 80)->nullable();
            $table->string('action', 30);            // generated|applied|accepted|rejected|dismissed
            $table->json('meta')->nullable();
            $table->timestamps();

            $table->index(['business_id', 'created_at']);
            $table->index(['business_id', 'feature', 'action']);
        });
    }

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