<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class WalletBeneficiaryLookupTest extends TestCase
{
    use RefreshDatabase;

    public function test_lookup_returns_beneficiary_payload_when_beneficiary_exists(): void
    {
        $sender = User::factory()->create();
        $beneficiary = User::factory()->create();

        $this->actingAs($sender)
            ->get(route('app.wallet.beneficiary.lookup', ['recipient' => $beneficiary->email]))
            ->assertOk()
            ->assertJsonPath('found', true)
            ->assertJsonPath('beneficiary.id', $beneficiary->id)
            ->assertJsonPath('beneficiary.name', $beneficiary->name)
            ->assertJsonPath('beneficiary.email', $beneficiary->email);
    }

    public function test_lookup_returns_beneficiary_not_found_message_when_identifier_is_invalid(): void
    {
        $sender = User::factory()->create();

        $this->actingAs($sender)
            ->get(route('app.wallet.beneficiary.lookup', ['recipient' => 'not-found-user@example.com']))
            ->assertNotFound()
            ->assertJsonPath('found', false)
            ->assertJsonPath('message', 'Beneficiary not found. Enter a valid registered email, referral code, or user ID.');
    }

    public function test_lookup_rejects_sender_self_as_beneficiary(): void
    {
        $sender = User::factory()->create();

        $this->actingAs($sender)
            ->get(route('app.wallet.beneficiary.lookup', ['recipient' => $sender->email]))
            ->assertStatus(422)
            ->assertJsonPath('found', false)
            ->assertJsonPath('message', 'You cannot transfer to your own wallet.');
    }
}
