<?php

use App\Support\ApiErrorTranslator;

it('returns null for null error', function () {
    expect(ApiErrorTranslator::translate(null))->toBeNull();
});

it('returns null for empty string error', function () {
    expect(ApiErrorTranslator::translate(''))->toBeNull();
});

it('normalizes array error to string', function () {
    $result = ApiErrorTranslator::translate(['Error one', 'Error two']);

    expect($result)->toContain('Error one');
    expect($result)->toContain('Error two');
});

it('returns string error as-is when no explanation found', function () {
    expect(ApiErrorTranslator::translate('Some unknown error'))->toBe('Some unknown error');
});

it('adds explanation for known search terms error', function () {
    $result = ApiErrorTranslator::translate('Search terms are required. Please provide search criteria.');

    expect($result)->toContain('Search terms are required');
    expect($result)->toContain('Explication');
    expect($result)->toContain('Le champ de recherche est obligatoire.');
});

it('adds explanation for invalid api key error', function () {
    $result = ApiErrorTranslator::translate('Invalid API key provided');

    expect($result)->toContain('Invalid API key');
    expect($result)->toContain('Explication');
    expect($result)->toContain('cle API');
});

it('adds explanation for unknown stkid error', function () {
    $result = ApiErrorTranslator::translate('Unknown STKID');

    expect($result)->toContain('Unknown STKID');
    expect($result)->toContain('Explication');
    expect($result)->toContain('STKID');
});

it('adds explanation for array error with known pattern', function () {
    $result = ApiErrorTranslator::translate(['Search terms are required. Please provide search criteria.']);

    expect($result)->toContain('Explication');
    expect($result)->toContain('Le champ de recherche est obligatoire.');
});

it('normalizes mixed array values', function () {
    $result = ApiErrorTranslator::normalize([123, 'text', null]);

    expect($result)->toBe('123 text ');
});
