<?php

declare(strict_types=1);

use App\Models\Tenant;
use App\Models\User;
use App\Services\DaData\Dto\PartyLookupResult;
use App\Services\DaData\PartyLookup;
use Illuminate\Foundation\Testing\DatabaseTransactions;

uses(DatabaseTransactions::class);

it('lookup-inn returns found:false when party lookup is null (default Null driver)', function () {
    $tenant = Tenant::factory()->create();
    $this->actingAs(User::factory()->create(['tenant_id' => $tenant->id]));

    $this->postJson('/api/tenant/requisites/lookup-inn', ['inn' => '7707083893'])
        ->assertOk()
        ->assertJson(['found' => false]);
});

it('lookup-inn returns found:true with a fake lookup', function () {
    $tenant = Tenant::factory()->create();
    $this->actingAs(User::factory()->create(['tenant_id' => $tenant->id]));

    $this->app->bind(PartyLookup::class, fn () => new class implements PartyLookup
    {
        public function findByInn(string $inn): PartyLookupResult
        {
            return new PartyLookupResult('ООО Тест', '770101001', '1027700132195', 'Москва', 'LEGAL', []);
        }
    });

    $this->postJson('/api/tenant/requisites/lookup-inn', ['inn' => '7707083893'])
        ->assertOk()
        ->assertJson([
            'found' => true,
            'legal_name' => 'ООО Тест',
            'subject_type_hint' => 'legal_entity',
        ]);
});
