<?php

namespace Tests\Feature\V3\Damage;

use App\Models\Damage;
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 , WithFaker;

    public function test_unauthenticated_user_cannot_view_show_damage(): void
    {

        $damage =  Damage::factory()->create();;
        $response = $this->getJson(route('v3.damages.show', [$damage->id]));

        $response->assertStatus(401);
    }

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

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

        $response->assertForbidden();
    }

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

        $damage = Damage::factory()->create();

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

        $response->assertOk()
            ->assertJson([
                'data' => [
                    'id' => $damage->id,
                    'title' => $damage->title,
                    'unit' => $damage->unit,
                    'base_price' => $damage->base_price,
                    'status' => $damage->status,
                ]
            ]);
    }

}
