<?php

namespace Tests\Feature\V2\Dashboard\RoadObservation;

use App\Models\LogList;
use App\Models\Permission;
use App\Models\RoadObserved;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDataBase;
use Tests\TestCase;

class verifyBySupervisorTest extends TestCase
{
    use RefreshDataBase;
    /**
     * A basic feature test example.
     */
    public function test_user_should_have_gate_supervise_road_observed_permission(): void
    {
        $user = User::factory()->create();
        $roadObserved = RoadObserved::factory()->create();

        $response = $this->actingAs($user)->postJson("/v2/road_observations/supervisor/cartable/verify/{$roadObserved->id}");

        $response->assertForbidden();
    }

    public function test_verify_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'gate-supervise-road-observed'
            ]))
            ->create();
        $roadObserved = RoadObserved::factory()->create();

        $response = $this->actingAs($user)->postJson("/v2/road_observations/supervisor/cartable/verify/{$roadObserved->id}");

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

    public function test_verify_should_be_1_or_2(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'gate-supervise-road-observed'
            ]))
            ->create();
        $roadObserved = RoadObserved::factory()->create();

        $response = $this->actingAs($user)->postJson("/v2/road_observations/supervisor/cartable/verify/{$roadObserved->id}", [
            'verify' => $this->faker->numberBetween(3, 7)
        ]);

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

    public function test_road_observed_can_be_verified_by_supervisor(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'gate-supervise-road-observed'
            ]))
            ->create();

        $roadObserved = RoadObserved::factory()->create();

        $logList = LogList::factory()->create([
            'log_unique_code' => 1138
        ]);

        $verify = $this->faker->numberBetween(1, 2);

        $response = $this->actingAs($user)->postJson("/v2/road_observations/supervisor/cartable/verify/{$roadObserved->id}", [
            'verify' => $verify
        ]);

        $response->assertOk()
            ->assertExactJson([
                'status' => 'succeed',
            ]);

        $this->assertDatabaseHas('road_observeds', [
            'status' => $verify,
            'supervisor_id' => $user->id,
            'supervisor_name' => $user->name,
            'supervising_time' => now(),
        ]);
    }
}
