<?php

namespace Tests\Feature\V2\Dashboard\RoadObservation;

use App\Facades\Sms\Sms;
use App\Models\LogList;
use App\Models\Permission;
use App\Models\RoadObserved;
use App\Models\User;
use App\Services\NikarayanService;
use Illuminate\Foundation\Testing\RefreshDataBase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Mockery\MockInterface;
use Tests\TestCase;

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

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

        $this->mock(NikarayanService::class, function (MockInterface $mock) {
            $mock->shouldReceive('updateRoadObservedInfoByNikarayan')->andReturn(0);
        });

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

        $response->assertForbidden();
    }

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

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

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

        $mock = $this->mock(NikarayanService::class, function (MockInterface $mock) {
            $mock->shouldReceive('updateRoadObservedInfoByNikarayan')->andReturn(0);
        });

        Sms::shouldReceive('sendSms');
        Storage::fake('public');

        $attributes = [
          'image-before-1' => UploadedFile::fake()->image('file.jpg')->size('30'),
          'image-after-1' => UploadedFile::fake()->image('file.jpg')->size('30'),
          'rms-start-latlng' => '12,12',
          'rms-end-latlng' => '13,13',
        ];

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

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

        $this->assertDatabaseHas('road_observed_histories', [
            'user_id' => $user->id,
            'previous_rms_status' => 0,
            'previous_rms_start_latlng' => $roadObserved->rms_start_latlng,
            'previous_rms_end_latlng' => $roadObserved->rms_end_latlng,
            'previous_rms_description' => $roadObserved->rms_description,
        ]);

        $this->assertDatabaseHas('road_observeds', [
            "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,
            "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,
            "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' => null,
            "rms_province_id" => $roadObserved->rms_province_id,
            "rms_status" => 0,
            'status' => 0,
            'status_fa' => 'در حال بررسی',
            "supervising_time" => $roadObserved->supervising_time,
            "supervisor_description" => $roadObserved->supervisor_description,
            "supervisor_id" => $roadObserved->supervisor_id,
            "supervisor_name" => $roadObserved->supervisor_name,
        ]);
    }
}
