<?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 RestoreTest extends TestCase
{
    use RefreshDataBase;
    /**
     * A basic feature test example.
     */
    public function test_user_should_have_restore_fast_react_permission(): void
    {
        $permission = Permission::factory()->create([
            'name' => 'restore-fast-react'
        ]);

        $user = User::factory()->create();

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

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

        $response->assertForbidden();
    }

    public function test_restore_description_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'restore-fast-react'
            ]))
            ->create();

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

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

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

    public function test_restore_description_should_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'restore-fast-react'
            ]))
            ->create();

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

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

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

    public function test_can_restore_a_road_observation(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'restore-fast-react'
            ]))
            ->create();

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

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

        $restore_description = $this->faker->text;

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

        $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" => null,
                    "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" => 0,
                    "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" => null,
                    "updated_at_fa" => $roadObserved->updated_at_fa
                ]
            ]);

        $this->assertDatabaseHas('road_observeds', [
            'image_before' => null,
            'image_after' => null,
            'rms_last_activity' => null,
            'rms_last_activity_fa' => null,
            'updated_at_fa' => null,
            'updated_at' => null,
            'rms_status' => 0,
            'status' => null,
            'status_fa' => null,
        ]);

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