<?php

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

// Per-persona WHMCS API credentials. Background:
//
// WHMCS's AddTicketNote API attributes the note to the calling API user
// (no `adminusername` parameter exists, unlike AddTicketReply). So when
// the portal's single API account ("MD Noyon Ali") posts an AI-written
// note, WHMCS shows "MD Noyon Ali posted a note" — even though the note
// was generated as the rotated persona "Ayesha Rahman / Tanvir Hossain /
// Rakib Islam". Customers and staff both saw the wrong author.
//
// Fix: each persona gets its own WHMCS admin account (admin creates them
// in WHMCS admin → Setup → Administrators) + its own API identifier /
// secret pair. When posting a note we authenticate as that persona, and
// WHMCS shows the correct author attribution.
//
// Encrypted at rest because these are real WHMCS credentials. Nullable
// so personas without per-persona creds fall back to the default API
// user (current behaviour).
return new class extends Migration {
    public function up(): void
    {
        Schema::table('support_agents', function (Blueprint $t) {
            $t->text('whmcs_api_identifier')->nullable()->after('whmcs_admin_username');
            $t->text('whmcs_api_secret')->nullable()->after('whmcs_api_identifier');
        });
    }

    public function down(): void
    {
        Schema::table('support_agents', function (Blueprint $t) {
            $t->dropColumn(['whmcs_api_identifier', 'whmcs_api_secret']);
        });
    }
};
