<?php

namespace Tests\Feature\Call;

use App\Models\Call;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;

class ListTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function test_unauthenticated_user_cannot_access(): void
    {
        $response = $this->getJson(route('calls.list'));
        $response->assertStatus(401);
    }

    public function test_user_without_permission_cannot_access(): void
    {
        $user = User::factory()->create();
        $this->actingAs($user);

        $response = $this->getJson(route('calls.list'));
        $response->assertForbidden();
    }

    public function test_size_must_be_integer(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage_calls',
                'name_fa' => 'مدیریت تماس‌ها',
            ]))->create();
        $this->actingAs($user);

        $response = $this->getJson(route('calls.list', [
            'size' => $this->faker->word,
            ]));

        $response->assertUnprocessable()
            ->assertInvalid([
                'size' => __('validation.integer', ['attribute' => 'حداکثر تعداد']),
            ]);
    }

    public function test_calls_are_limited_by_size_parameter(): void
    {

        $user = User::factory()
            ->has(Permission::factory()->state([
                'id' => 3,
                'name' => 'manage_calls',
                'name_fa' => 'مدیریت تماس‌ها',
            ]))
            ->create();
        Call::factory()->count(5)->create();


        $response = $this->actingAs($user)->getJson(route('calls.list', [
            'size' => $this->faker->numberBetween(1, 4),
        ]));

        $response->assertOk()
            ->assertJson(fn (AssertableJson $json) =>
            $json->has('data')
                ->has('data.0', fn (AssertableJson $json) => $json->hasAll([
                    'id',
                    'operator_id',
                    'operator_full_name',
                    'operator_username',
                    'telephone_id',
                    'caller_phone_number',
                    'description',
                    'category_id',
                    'category_name',
                    'subcategory_id',
                    'subcategory_name',
                    'created_at',
                ]))
            );

    }


    public function test_calls_are_filtered_by_phone_number(): void
    {
        $user = User::factory()
        ->has(Permission::factory([
            'name' => 'manage_calls',
            'name_fa' => 'مدیریت تماس‌ها',
        ]))->create();

        $targetPhone = '09' . $this->faker->numberBetween(100000000, 999999999);

        Call::factory()->count(4)->create([
            'caller_phone_number' => $targetPhone,
        ]);

        Call::factory()->count(2)->create();


        $response = $this->actingAs($user)->getJson(route('calls.list', ['phone_number' => $targetPhone]));

        $response->assertOk()
            ->assertJsonCount(4, 'data')
            ->assertJsonFragment(['caller_phone_number' => $targetPhone]);
    }

}
