<?php

namespace Tests\Feature\Permission;

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

class UpdateTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function test_user_must_be_authenticated(): void
    {
        $permission = Permission::factory()->create();

        $response = $this->postJson(route('permissions.update', [$permission->id]));

        $response->assertUnauthorized();
    }



    public function test_name_and_name_fa_fields_are_required(): void
    {
        $user = User::factory()->create();
        $permission = Permission::factory()->create();

        $response = $this->actingAs($user)->postJson(route('permissions.update', [$permission->id]));

        $response->assertUnprocessable()
            ->assertInvalid([
                'name' => __('validation.required', ['attribute' => 'نام']),
                'name_fa' => __('validation.required', ['attribute' => 'نام فارسی']),
            ]);
    }

    public function test_name_and_name_fa_fields_must_be_strings(): void
    {
        $user = User::factory()->create();
        $permission = Permission::factory()->create();

        $response = $this->actingAs($user)->postJson(route('permissions.update', [$permission->id]), [
            'name' =>$this->faker->randomDigit(),
            'name_fa' => $this->faker->randomDigit(),
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'name' => __('validation.string', ['attribute' => 'نام']),
                'name_fa' => __('validation.string', ['attribute' => 'نام فارسی']),
            ]);
    }

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

        $permission1 = Permission::factory()->create();
        $permission2 = Permission::factory()->create();

        $response = $this->actingAs($user)->postJson(route('permissions.update', [$permission1->id]), [
            'name' => $permission2->name,
            'name_fa' => $permission2->name_fa,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'name' => __('validation.unique', ['attribute' => 'نام']),
                'name_fa' => __('validation.unique', ['attribute' => 'نام فارسی']),
            ]);
    }

    public function test_name_and_name_fa_must_not_exceed_max_length(): void
    {
        $user = User::factory()->create();
        $permission = Permission::factory()->create();

        $longString = $this->faker->regexify('[a-zA-Z0-9]{256}');

        $response = $this->actingAs($user)->postJson(route('permissions.update', [$permission->id]), [
            'name' => $longString,
            'name_fa' => $longString,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'name' => __('validation.max.string', ['attribute' => 'نام', 'max' => 255]),
                'name_fa' => __('validation.max.string', ['attribute' => 'نام فارسی', 'max' => 255]),
            ]);
    }

    public function test_valid_permission_is_updated_successfully(): void
    {
        $user = User::factory()->create();
        $permission = Permission::factory()->create();

        $payload = [
            'name' => $this->faker->unique()->slug,
            'name_fa' => $this->faker->unique()->words(2, true),
        ];

        $response = $this->actingAs($user)->postJson(route('permissions.update', [$permission->id]), $payload);

        $response->assertOk()
            ->assertJson([
                'message' => __('messages.successful'),
            ]);

        $this->assertDatabaseHas('permissions', [
            'id' => $permission->id,
            'name' => $payload['name'],
            'name_fa' => $payload['name_fa'],
        ]);
    }
}
