<?php

namespace Tests\Feature\V3\Damage;

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

class IndexTest extends TestCase
{
    use RefreshDatabase, WithFaker;
    /**
     * A basic feature test example.
     */

    public function test_user_should_have_manage_damage_permission_index_damage(): void
    {
        $user = User::factory()->create();
        $response = $this->actingAs($user)->getJson(route('v3.damages.index'));
        $response->assertForbidden();
    }

    public function test_user_cannot_access_authentication_to_index_damage(): void
    {
        $response = $this->getJson(route('v3.damages.index'));
        $response->assertStatus(401);
    }

    public function test_all_damages_returned_successfully_for_index_damage(): void
    {

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


        Damage::factory()->count(5)->create();

        $response = $this->actingAs($user)->getJson(route('v3.damages.index', [
            'start' => 0,
            'size' => 10,
            'filters' => json_encode([]),
            'sorting' => json_encode([]),
        ]));
        $response->assertOk();
        $response->assertJsonStructure([
            'data' => [
                '*' => [
                    'id',
                    'title',
                    'unit',
                    'base_price',
                    'status',
                    ],
            ],
            'meta' => [
                'totalRowCount',
            ],
        ]);
   }
}
