<?php

namespace Tests\Feature\Role;

use App\Models\Permission;
use App\Models\Role;
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_the_user_is_not_authenticated()
    {
        $role = Role::factory()->create();
        $response = $this->postJson(route('roles.update',[$role->id]));

        $response->assertUnauthorized();
    }

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


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

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

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


        $duplicateName = $this->faker->unique()->slug;
        $duplicateNameFa = $this->faker->unique()->word;

        Role::create([
            'name' => $duplicateName,
            'name_fa' => $duplicateNameFa,
            'guard_name' => 'web',
        ]);

        $response = $this->actingAs($user)->postJson(route('roles.update',[$role->id]), [
            'name' => $duplicateName,
            'name_fa' => $duplicateNameFa,
        ]);

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

    public function test_name_and_name_fa_must_be_strings(): void
    {
        $role = Role::factory()->create();

        $user = User::factory()->create();
        $permission = Permission::factory()->create();

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

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

    public function test_name_and_name_fa_must_not_exceed_max_length(): void
    {
        $role = Role::factory()->create();

        $user = User::factory()->create();
        $longString = str_repeat('a', 256);
        $permission = Permission::factory()->create();

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

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

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


        $response = $this->actingAs($user)->postJson(route('roles.update',[$role->id]), [
            'name' => $this->faker->slug,
            'name_fa' => $this->faker->word,
            'permissions' => 1,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'permissions' => __('validation.array', ['attribute' => 'دسترسی ها']),
            ]);
    }

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

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

        $response->assertUnprocessable()
            ->assertInvalid([
                'permissions' => __('validation.exists', ['attribute' => 'دسترسی ها']),
            ]);
    }

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

        $permission = Permission::factory()->create();

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

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

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

        $this->assertDatabaseHas('roles', [
            'name' => $payload['name'],
            'name_fa' => $payload['name_fa'],
        ]);

        $this->assertDatabaseHas('role_has_permissions', [
            'permission_id' => $permission->id,
        ]);
    }
}
