<?php

namespace Tests\Feature\V3\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;
    /**
     * A basic feature test example.
     */
    public function test_user_should_have_manage_permission(): void
    {
        $user = User::factory()->create();

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

        $response->assertForbidden();
    }

    public function test_name_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

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

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

    public function test_name_attribute_should_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
            'name' => $this->faker->numberBetween(1, 10)
        ]);

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

    public function test_name_attribute_should_be_unique(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

        $permission = Permission::factory()->create([
            'name' => $this->faker->name()
        ]);

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

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

    public function test_name_fa_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

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

        $response->assertUnprocessable()
            ->assertInvalid([
                'name_fa' => __('validation.required', ['attribute' => 'name fa'])
            ]);
    }

    public function test_name_fa_attribute_should_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
            'name_fa' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'name_fa' => __('validation.string', ['attribute' => 'name fa'])
            ]);
    }

    public function test_description_attribute_should_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
            'description' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'description' => __('validation.string', ['attribute' => 'توضیحات'])
            ]);
    }

    public function test_type_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

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

        $response->assertUnprocessable()
            ->assertInvalid([
                'type' => __('validation.required', ['attribute' => 'type'])
            ]);
    }

    public function test_type_attribute_should_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
            'type' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'type' => __('validation.string', ['attribute' => 'type'])
            ]);
    }

    public function test_type_fa_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

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

        $response->assertUnprocessable()
            ->assertInvalid([
                'type_fa' => __('validation.required', ['attribute' => 'type fa'])
            ]);
    }

    public function test_type_fa_attribute_should_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
            'type_fa' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'type_fa' => __('validation.string', ['attribute' => 'type fa'])
            ]);
    }

    public function test_need_province_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

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

        $response->assertUnprocessable()
            ->assertInvalid([
                'need_province' => __('validation.required', ['attribute' => 'need province'])
            ]);
    }

    public function test_need_province_attribute_should_be_1_or_0(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
            'need_province' => $this->faker->numberBetween(3, 9),
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'need_province' => __('validation.in', ['attribute' => 'need province'])
            ]);
    }

    public function test_need_edare_shahri_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

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

        $response->assertUnprocessable()
            ->assertInvalid([
                'need_edare_shahri' => __('validation.required', ['attribute' => 'need edare shahri'])
            ]);
    }

    public function test_need_edare_shahri_attribute_should_be_1_or_0(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

        $response = $this->actingAs($user)->postJson(route('v3.permissions.store'), [
            'need_edare_shahri' => $this->faker->numberBetween(3, 9),
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'need_edare_shahri' => __('validation.in', ['attribute' => 'need edare shahri'])
            ]);
    }

    public function test_user_can_create_a_permission()
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'manage-permissions'
            ]))
            ->create();

        $inputData = [
            'name' => $this->faker->name(),
            'name_fa' => $this->faker->name(),
            'description' => $this->faker->lexify(),
            'type' => $this->faker->name(),
            'type_fa' => $this->faker->name(),
            'need_province' => $this->faker->numberBetween(0, 1),
            'need_edare_shahri' => $this->faker->numberBetween(0, 1),
        ];

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

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

        $this->assertDatabaseHas('permissions', $inputData);
    }
}
