<?php

namespace Tests\Feature\V3\RoadItemsProject;

use App\Models\Permission;
use App\Models\RoadItemsProject;
use App\Models\UserActivityLog;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
use Mockery;
use App\Models\LogList;
use Illuminate\Support\Facades\Schema;



class VerifyBySupervisor extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function test_user_cannot_access_authentication_to_verify_by_supervisor(): void
    {
        $roadItem = RoadItemsProject::factory()->create();

        $response = $this->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id]));

        $response->assertStatus(401);
    }

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

        Gate::shouldReceive('allows')
            ->andReturn(false);

        $roadItem = RoadItemsProject::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id,]));

        $response->assertForbidden();
    }
    public function test_validation_fails_when_required_fields_are_missing(): void
    {
        $user = User::factory()->create();
        $roadItem = RoadItemsProject::factory()->create();

        Gate::shouldReceive('allows')
            ->andReturn(true);

        $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id]));

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

    public function test_validation_fails_when_verify_field_is_invalid(): void
    {
        $user = User::factory()->create();
        $roadItem = RoadItemsProject::factory()->create();

        Gate::shouldReceive('allows')
            ->andReturn(true);

        $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id]), [
            'verify' => 5,
            'description'  => $this->faker()->sentence(),
        ]);

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

    public function test_verify_description_must_be_string(): void
    {
        $user = User::factory()->create();
        $roadItem = RoadItemsProject::factory()->create();

        Gate::shouldReceive('allows')
            ->andReturn(true);

        $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id]), [
            'description' => 12345,
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'description' => __('validation.string', ['attribute' =>"توضیحات"]),
            ]);
    }



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

        LogList::factory()->create([
            'log_unique_code' => 1149,
        ]);
        UserActivityLog::factory()->create();

        $roadItem = RoadItemsProject::factory()->create();

        Gate::shouldReceive('allows')
            ->andReturn(true);

        $description = fake()->sentence();
        $response = $this->actingAs($user)->postJson(route('v3.road_items.VerifyBySupervisor', [$roadItem->id,]), [
            'verify' => 1,
            'description' =>$description,
        ]);

        $response->assertOk()
            ->assertJson([
                'message' => __('messages.successful'),
            ]);
        $this->assertDatabaseHas('road_items_projects', [
            'id' => $roadItem->id,
            'status' => 1,
            'status_fa' => 'تایید شده',
            'supervisor_id' => $user->id,
            'supervisor_name' => $user->name,
            'supervisor_description' =>$description,
        ]);
    }
}
