<?php

declare(strict_types=1);

use App\Services\DaData\DaDataPartyClient;
use Illuminate\Http\Client\Factory as HttpFactory;

it('parses a party suggestion into a result', function () {
    $http = new HttpFactory;
    $http->fake([
        'suggestions.dadata.ru/*' => $http->response([
            'suggestions' => [[
                'value' => 'ООО "РОМАШКА"',
                'data' => [
                    'kpp' => '770101001',
                    'ogrn' => '1027700132195',
                    'type' => 'LEGAL',
                    'address' => ['value' => 'г Москва, ул Ленина, д 1'],
                ],
            ]],
        ]),
    ]);
    config()->set('services.dadata.api_key', 'test-key');

    $res = (new DaDataPartyClient($http))->findByInn('7707083893');

    expect($res)->not->toBeNull();
    expect($res->legalName)->toBe('ООО "РОМАШКА"');
    expect($res->kpp)->toBe('770101001');
    expect($res->type)->toBe('LEGAL');
});

it('returns null on empty suggestions', function () {
    $http = new HttpFactory;
    $http->fake(['suggestions.dadata.ru/*' => $http->response(['suggestions' => []])]);
    config()->set('services.dadata.api_key', 'test-key');

    expect((new DaDataPartyClient($http))->findByInn('0000000000'))->toBeNull();
});

it('returns null on HTTP error (soft)', function () {
    $http = new HttpFactory;
    $http->fake(['suggestions.dadata.ru/*' => $http->response('boom', 500)]);
    config()->set('services.dadata.api_key', 'test-key');

    expect((new DaDataPartyClient($http))->findByInn('7707083893'))->toBeNull();
});

it('returns null when api key is empty', function () {
    $http = new HttpFactory;
    config()->set('services.dadata.api_key', '');

    expect((new DaDataPartyClient($http))->findByInn('7707083893'))->toBeNull();
});
