<?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 ShowTest 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.show', [$azmayeshType->id]));

        $response->assertForbidden();
    }

    public function test_azmayesh_type_details_returned_successfully(): void
    {
        $azmayeshType = AzmayeshType::factory()->create();
        $azmayeshField = AzmayeshField::factory()->create();

        $azmayeshType->azmayeshFields()->attach([$azmayeshField->id]);

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

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

        $response->assertOk();

        $response->assertExactJson([
            'data' => [
                "id" => $azmayeshType->id,
                "name" => $azmayeshType->name,
                "created_at" => $azmayeshType->created_at,
                "updated_at" => $azmayeshType->updated_at,
                "azmayesh_fields" => [
                    [
                        "id" => $azmayeshField->id,
                        "name" => $azmayeshField->name,
                        "unit" => $azmayeshField->unit,
                        "type" => $azmayeshField->type,
                        "option" => $azmayeshField->option,
                        "created_at" => $azmayeshField->created_at,
                        "updated_at" => $azmayeshField->updated_at,
                    ]
                ]
            ]
        ]);
    }
}
