<?php

declare(strict_types=1);

use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;

uses(DatabaseTransactions::class);

it('GET requisites returns null when none', function () {
    $tenant = Tenant::factory()->create();
    $this->actingAs(User::factory()->create(['tenant_id' => $tenant->id]));

    $this->getJson('/api/tenant/requisites')->assertOk()->assertJson(['data' => null]);
});

it('PUT individual requisites succeeds and normalizes phone', function () {
    $tenant = Tenant::factory()->create();
    $this->actingAs(User::factory()->create(['tenant_id' => $tenant->id]));

    $this->putJson('/api/tenant/requisites', [
        'subject_type' => 'individual',
        'contact_name' => 'Иван',
        'contact_phone' => '8 915 123 45 67',
    ])->assertOk()->assertJsonPath('data.contact_phone', '+79151234567');
});

it('PUT legal entity without inn is rejected', function () {
    $tenant = Tenant::factory()->create();
    $this->actingAs(User::factory()->create(['tenant_id' => $tenant->id]));

    $this->putJson('/api/tenant/requisites', [
        'subject_type' => 'legal_entity',
        'contact_name' => 'A',
        'contact_phone' => '9151234567',
    ])->assertStatus(422)->assertJsonValidationErrors('inn');
});

it('PUT legal entity with bad inn checksum is rejected', function () {
    $tenant = Tenant::factory()->create();
    $this->actingAs(User::factory()->create(['tenant_id' => $tenant->id]));

    $this->putJson('/api/tenant/requisites', [
        'subject_type' => 'legal_entity',
        'contact_name' => 'A',
        'contact_phone' => '9151234567',
        'inn' => '7707083890',
    ])->assertStatus(422)->assertJsonValidationErrors('inn');
});

it('PUT bad phone is rejected', function () {
    $tenant = Tenant::factory()->create();
    $this->actingAs(User::factory()->create(['tenant_id' => $tenant->id]));

    $this->putJson('/api/tenant/requisites', [
        'subject_type' => 'individual',
        'contact_name' => 'A',
        'contact_phone' => '123',
    ])->assertStatus(422)->assertJsonValidationErrors('contact_phone');
});
