<?php

declare(strict_types=1);

use App\Support\ProductKnowledge\ContextualHelpCatalog;
use App\Support\ProductKnowledge\ContextualHelpResolver;
use App\Support\Providers\ProviderReasonCodes;

it('returns null for blank or unknown topic keys through the fallback contract', function (): void {
    $resolver = app(ContextualHelpResolver::class);

    expect($resolver->tryResolve(null))->toBeNull()
        ->and($resolver->tryResolve(''))->toBeNull()
        ->and($resolver->tryResolve('unknown-topic'))->toBeNull();
});

it('keeps dynamic link metadata but no tenant-specific url when tenant context is unavailable', function (): void {
    $payload = app(ContextualHelpResolver::class)->resolve(ContextualHelpCatalog::REQUIRED_PERMISSIONS_MISSING);

    expect($payload['docs_links'][0])->toMatchArray([
        'label' => 'Open required permissions',
        'resolver' => ContextualHelpCatalog::LINK_RESOLVER_REQUIRED_PERMISSIONS,
        'url' => null,
    ]);
});

it('exposes a safe machine-readable knowledge source without tenant or secret fields', function (): void {
    $knowledgeSource = app(ContextualHelpResolver::class)->knowledgeSource();
    $encoded = json_encode($knowledgeSource, JSON_THROW_ON_ERROR);

    expect($encoded)->not->toContain('managed_environment_id')
        ->not->toContain('provider_connection_id')
        ->not->toContain('raw_response_body')
        ->not->toContain('credential')
        ->and($knowledgeSource['topic_count'])->toBe(8);
});

it('keeps explicit unmapped support-diagnostics reason codes on the null fallback path', function (): void {
    $resolver = app(ContextualHelpResolver::class);

    expect($resolver->topicKeyForSupportDiagnostics(
        reasonCode: 'ext.support.manual_lookup_needed',
        hasIncompleteEvidence: true,
        runOutcome: null,
    ))->toBeNull()
        ->and($resolver->topicKeyForSupportDiagnostics(
            reasonCode: ProviderReasonCodes::UnknownError,
            hasIncompleteEvidence: true,
            runOutcome: null,
        ))->toBe(ContextualHelpCatalog::MANUAL_HANDOFF_REQUIRED);
});
