<?php

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

// After every draft generation, the drafter analyses the reply body and
// recommends a WHMCS status:
//   - "Answered"     — staff replied, waiting on customer (default)
//   - "In Progress"  — we're working on it / escalated to team
//   - "On Hold"      — quality gate fired or AI explicitly said "waiting"
// AutoReplyTicket reads this and calls WHMCS UpdateTicket after posting
// the reply, so the WHMCS-side status reflects what the AI actually did
// (instead of WHMCS's default behavior of stamping every staff reply
// as "Answered").
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('tickets')) return;
        Schema::table('tickets', function (Blueprint $t) {
            if (! Schema::hasColumn('tickets', 'ai_suggested_whmcs_status')) {
                $t->string('ai_suggested_whmcs_status', 32)->nullable()->after('ai_summary');
            }
        });
    }

    public function down(): void
    {
        if (Schema::hasTable('tickets')) {
            Schema::table('tickets', function (Blueprint $t) {
                if (Schema::hasColumn('tickets', 'ai_suggested_whmcs_status')) {
                    $t->dropColumn('ai_suggested_whmcs_status');
                }
            });
        }
    }
};
