<?php

namespace Tests\Feature\V2\Dashboard\SafetyAndPrivacy;

use App\Models\City;
use App\Models\EdarateOstani;
use App\Models\EdarateShahri;
use App\Models\InfoItem;
use App\Models\LogList;
use App\Models\Permission;
use App\Models\User;
use App\Models\UserActivityLog;
use App\Services\NominatimService;
use Illuminate\Foundation\Testing\RefreshDataBase;
use Illuminate\Http\UploadedFile;
use Mockery\MockInterface;
use Tests\TestCase;

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

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step');

        $response->assertForbidden();
    }

    public function test_user_should_not_have_city_id(): void
    {
        $edarateOstani = EdarateOstani::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'edarate_ostani_id' => $edarateOstani->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step');

        $response->assertStatus(400)
            ->assertExactJson([
                'message' => 'امکان ثبت این مورد برای ادارات استانی و ستادی وجود ندارد!'
            ]);
    }

    public function test_user_should_not_have_edarate_shahri_id(): void
    {
        $city = City::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step');

        $response->assertStatus(400)
            ->assertExactJson([
                'message' => 'اداره شهری برای شما در سامانه ثبت نشده است!'
            ]);
    }

    public function test_point_is_required(): void
    {
        $city = City::factory()->create();
        $edarateShari = EdarateShahri::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id,
                'edarate_shahri_id' => $edarateShari->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step');

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

    public function test_info_id_is_required(): void
    {
        $city = City::factory()->create();
        $edarateShari = EdarateShahri::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id,
                'edarate_shahri_id' => $edarateShari->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step');

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

    public function test_activity_date_is_required(): void
    {
        $city = City::factory()->create();
        $edarateShari = EdarateShahri::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id,
                'edarate_shahri_id' => $edarateShari->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step');

        $response->assertUnprocessable()
            ->assertInvalid([
                'activity_date' => __('validation.required', ['attribute' => 'تاریخ فعالیت'])
            ]);
    }

    public function test_activity_date_should_match_the_format(): void
    {
        $city = City::factory()->create();
        $edarateShari = EdarateShahri::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id,
                'edarate_shahri_id' => $edarateShari->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step', [
            'activity_date' => $this->faker->date('d-m-Y-H-i-s')
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'activity_date' => __('validation.date_format', ['attribute' => 'تاریخ فعالیت', 'format' => 'Y-m-d'])
            ]);
    }

    public function test_activity_time_is_required(): void
    {
        $city = City::factory()->create();
        $edarateShari = EdarateShahri::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id,
                'edarate_shahri_id' => $edarateShari->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step');

        $response->assertUnprocessable()
            ->assertInvalid([
                'activity_time' => __('validation.required', ['attribute' => 'ساعت فعالیت'])
            ]);
    }

    public function test_activity_time_should_match_the_format(): void
    {
        $city = City::factory()->create();
        $edarateShari = EdarateShahri::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id,
                'edarate_shahri_id' => $edarateShari->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step', [
            'activity_time' => $this->faker->date('d-m-Y-H-i-s')
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'activity_time' => __('validation.date_format', ['attribute' => 'ساعت فعالیت', 'format' => 'H:i'])
            ]);
    }

    public function test_recognize_picture_is_required(): void
    {
        $city = City::factory()->create();
        $edarateShari = EdarateShahri::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id,
                'edarate_shahri_id' => $edarateShari->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step');

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

    public function test_recognize_picture_should_be_image(): void
    {
        $city = City::factory()->create();
        $edarateShari = EdarateShahri::factory()->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id,
                'edarate_shahri_id' => $edarateShari->id
            ]);

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step', [
            'recognize_picture' => $this->faker->name
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'recognize_picture' => __('validation.image', ['attribute' => 'recognize picture'])
            ]);
    }

    public function test_new_safety_and_privacy_can_be_stored(): void
    {
        $this->withOutExceptionHandling();

        $infoItem = InfoItem::factory()->create([
            'item' => 17
        ]);
        $city = City::factory()->create();
        $edarateShari = EdarateShahri::factory()->create();
        $logList = LogList::factory()->create();
        $userActivityLog = UserActivityLog::factory()->create();
        $image = UploadedFile::fake()->create('image.jpg', 10);

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'add-safety-and-privacy'
            ]))
            ->create([
                'city_id' => $city->id,
                'edarate_shahri_id' => $edarateShari->id
            ]);

        $data = [
            'point' => '12,12',
            'info_id' => $infoItem->id,
            'activity_date' => $this->faker->date('Y-m-d'),
            'activity_time' => $this->faker->date('H:i'),
            'recognize_picture' => $image
        ];

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

        $response = $this->actingAs($user)->postJson('v2/safety_and_privacy/operator/first_step', $data);

        $response->assertOk();

        $this->assertDatabaseHas('safety_and_privacy', [
            'lat' => 12,
            'lon' => 12,
            'operator_id' => $user->id,
            'operator_name' => $user->name,
            'province_id' => $user->province_id,
            'province_fa' => $user->province_fa,
            'info_id' => $infoItem->id,
            'city_id' => $city->id,
            'city_fa' => $user->city_fa,
            'activity_date_time' => $data['activity_date']." ".$data['activity_time'],
            'edare_shahri_id' => $edarateShari->id,
            'edare_shahri_name' => $edarateShari->name,
            'way_id' => 0
        ]);
    }
}
