<?php

namespace Tests\Feature\V3\AzmayeshType;

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

class FieldsTest extends TestCase
{
    use RefreshDatabase;
    /**
     * A basic feature test example.
     */
    public function test_user_should_have_azmayesh_type_management_permission(): void
    {
        $user = User::factory()->create();
        $azmayeshType = AzmayeshType::factory()->create();

        $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.fields', [$azmayeshType->id]));

        $response->assertForbidden();
    }

    public function test_all_fields_for_azmayesh_types_returned_successfully(): void
    {
        AzmayeshField::factory(3)
            ->sequence(
                [
                    'id' => 1,
                    'name' => $this->faker->name,
                    'unit' => $this->faker->name,
                    'type' => $this->faker->name,
                    'option' => json_encode([$this->faker->name]),
                ],
                [
                    'id' => 2,
                    'name' => $this->faker->name,
                    'unit' => $this->faker->name,
                    'type' => $this->faker->name,
                    'option' => json_encode([$this->faker->name]),
                ],
                [
                    'id' => 3,
                    'name' => $this->faker->name,
                    'unit' => $this->faker->name,
                    'type' => $this->faker->name,
                    'option' => json_encode([$this->faker->name]),
                ],
            )
            ->create();

        $azmayeshType = AzmayeshType::factory()->create();

        $azmayeshType->azmayeshFields()->attach([1, 2, 3]);

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-type-management'
            ]))
            ->create();

        $response = $this->actingAs($user)->getJson(route('v3.azmayesh_types.fields', [$azmayeshType->id]));

        $response->assertOk();

        $response->assertJsonStructure([
            'data' => [
                '*' => [
                    'name',
                    'unit',
                    'type',
                    'option',
                    "created_at",
                    "updated_at"
                ],
            ]
        ]);
    }
}
