<?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 StoreTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function test_user_must_be_authenticated(): void
    {
        $response = $this->postJson(route('permissions.store'), []);
        $response->assertUnauthorized();
    }

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


        $response =  $this->actingAs($user)->postJson(route('permissions.store'));

        $response->assertUnprocessable()
            ->assertInvalid([
                'name' => __('validation.required', ['attribute' => 'نام']),
                'name_fa' => __('validation.required', ['attribute' => 'نام فارسی']),
            ]);
    }
    public function test_name_and_name_fa_fields_must_be_unique(): void
    {
        $user = User::factory()->create();

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

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

        $response = $this->actingAs($user)->postJson(route('permissions.store'), [
            '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
    {
        $user = User::factory()->create();

        $response = $this->actingAs($user)->postJson(route('permissions.store'), [
            '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_must_not_exceed_max_length(): void
    {
        $user = User::factory()->create();

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

        $response = $this->actingAs($user)->postJson(route('permissions.store'), [
            '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_stored_successfully(): void
    {
        $user = User::factory()->create();

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

        $response = $this->actingAs($user)->postJson(route('permissions.store'), $payload);

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

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


}
