<?php

namespace Modules\Shared\Console\Commands;

use Illuminate\Console\Command;
use Modules\Shared\Bnfexpress\BnfexpressAdminClient;
use Modules\Shared\Bnfexpress\Exceptions\BnfexpressApiException;

/**
 * Exercises the signed bnfexpress admin client end to end (list FAQs, get
 * the active instruction) against the real BNFEXPRESS_AI_API_URL, to confirm
 * request signing checks out before any UI is wired up to it.
 */
class BnfexpressSmokeTestCommand extends Command
{
    protected $signature = 'bnfexpress:smoke-test';

    protected $description = 'Call bnfexpress\'s admin API (list EV FAQs, get the active EV instruction) to verify request signing';

    public function handle(BnfexpressAdminClient $client): int
    {
        try {
            $this->components->task('GET /admin/faqs?agent=ev', function () use ($client) {
                $faqs = $client->listFaqs();
                $this->line('  '.json_encode($faqs));
            });

            $this->components->task('GET /admin/agent-instructions/active?agent=ev', function () use ($client) {
                $active = $client->getActiveInstruction();
                $this->line('  '.json_encode($active));
            });
        } catch (BnfexpressApiException $exception) {
            $this->components->error('bnfexpress request failed: '.$exception->getMessage());

            return self::FAILURE;
        }

        $this->components->info('bnfexpress signing verified.');

        return self::SUCCESS;
    }
}
