<?php

namespace Tests\Feature\V2\Dashboard\RoadObservation;

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

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

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

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

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

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

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

        $response = $this->actingAs($user)->postJson("/v2/road_observations/refer/{$roadObserved->id}", [
            'edarate_shahri_id' => $this->faker->name
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'edarate_shahri_id' => __('validation.numeric', ['attribute' => 'edarate shahri id'])
            ]);
    }

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

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

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

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

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

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

        $response = $this->actingAs($user)->postJson("/v2/road_observations/refer/{$roadObserved->id}", [
            'refer_description' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'refer_description' => __('validation.string', ['attribute' => 'refer description'])
            ]);
    }

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

        $roadObserved = RoadObserved::factory()->create([
            'rms_status' => $this->faker->numberBetween(1, 10)
        ]);

        $response = $this->actingAs($user)->postJson("/v2/road_observations/refer/{$roadObserved->id}", [
            'refer_description' => $this->faker->name,
            'edarate_shahri_id' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertForbidden()
            ->assertExactJson([
                'message' => 'access to requested resource is forbidden.'
            ]);
    }

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

        $roadObserved = RoadObserved::factory()->create([
            'rms_status' => 0
        ]);

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

        $edarate_shahri = EdarateShahri::factory()->create();
        $refer_description = $this->faker->name;

        $response = $this->actingAs($user)->postJson("/v2/road_observations/refer/{$roadObserved->id}", [
            'refer_description' => $refer_description,
            'edarate_shahri_id' => $edarate_shahri->id
        ]);

        $response->assertOk()
            ->assertExactJson([
                'status' => 'succeed',
                'message' => '',
                'data' => [
                    "AutoID" => $roadObserved->AutoID,
                    "Description" => $roadObserved->Description,
                    "EndDate" => $roadObserved->EndDate,
                    "EndTime" => $roadObserved->EndTime,
                    "EndTime_DateTime" => $roadObserved->EndTime_DateTime,
                    "EventTypeTitle" => $roadObserved->EventTypeTitle,
                    "FeatureTypeTitle" => $roadObserved->FeatureTypeTitle,
                    "MobileForSendEventSms" => $roadObserved->MobileForSendEventSms,
                    "ProvinceName" => $roadObserved->ProvinceName,
                    "StartDate" => $roadObserved->StartDate,
                    "StartTime" => $roadObserved->StartTime,
                    "StartTime_DateTime" => $roadObserved->StartTime_DateTime,
                    "StartTime_DateTime_fa" => $roadObserved->StartTime_DateTime_fa,
                    "Title" => $roadObserved->Title,
                    "TownName" => $roadObserved->TownName,
                    "WeatherName" => $roadObserved->WeatherName,
                    "XLAM" => $roadObserved->XLAM,
                    "YLAM" => $roadObserved->YLAM,
                    "city_fa" => $roadObserved->city_fa,
                    "city_id" => $roadObserved->city_id,
                    "created_at" => $roadObserved->created_at,
                    "edarate_shahri_id" => $edarate_shahri->id,
                    "fk_Country" => $roadObserved->fk_Country,
                    "fk_EventType" => $roadObserved->fk_EventType,
                    "fk_FeatureType" => $roadObserved->fk_FeatureType,
                    "fk_Province" => $roadObserved->fk_Province,
                    "fk_RegisteredEventMessage" => $roadObserved->fk_RegisteredEventMessage,
                    "fk_Town" => $roadObserved->fk_Town,
                    "fk_Weather" => $roadObserved->fk_Weather,
                    "handling_way_id" => $roadObserved->handling_way_id,
                    "id" => $roadObserved->id,
                    "image_after" => $roadObserved->image_after,
                    "image_before" => $roadObserved->image_before,
                    "lat" => $roadObserved->lat,
                    "lng" => $roadObserved->lng,
                    "observed_way_id" => $roadObserved->observed_way_id,
                    "province_fa" => $roadObserved->province_fa,
                    "province_id" => $roadObserved->province_id,
                    "refer_description" => $roadObserved->refer_description,
                    "restore_description" => $roadObserved->restore_description,
                    "rms_city_id" => $roadObserved->rms_city_id,
                    "rms_description" => $roadObserved->rms_description,
                    "rms_end_latlng" => $roadObserved->rms_end_latlng,
                    "rms_last_activity" => $roadObserved->rms_last_activity,
                    "rms_last_activity_fa" => $roadObserved->rms_last_activity_fa,
                    "rms_province_id" => $roadObserved->rms_province_id,
                    "rms_start_latlng" => $roadObserved->rms_start_latlng,
                    "rms_status" => $roadObserved->rms_status,
                    "status" => $roadObserved->status,
                    "status_fa" => $roadObserved->status_fa,
                    "supervising_time" => $roadObserved->supervising_time,
                    "supervisor_description" => $roadObserved->supervisor_description,
                    "supervisor_id" => $roadObserved->supervisor_id,
                    "supervisor_name" => $roadObserved->supervisor_name,
                    "updated_at" => $roadObserved->updated_at,
                    "updated_at_fa" => $roadObserved->updated_at_fa
                ]
            ]);

        $this->assertDatabaseHas('road_observeds', [
            'edarate_shahri_id' => $edarate_shahri->id
        ]);

        $this->assertDatabaseHas('road_observation_histories', [
            'id' => $roadObserved->id,
            'user_id' => $user->id,
            'action' => 'refer',
            'description' => $refer_description,
        ]);
    }
}
