<?php

namespace Tests\Feature\V2\Dashboard\RoadItemsProject;

use App\Models\City;
use App\Models\EdarateOstani;
use App\Models\EdarateShahri;
use App\Models\InfoItem;
use App\Models\LogList;
use App\Models\ObservedItem;
use App\Models\Permission;
use App\Models\Province;
use App\Models\RoadItemsProject;
use App\Models\User;
use App\Models\UserActivityLog;
use Illuminate\Foundation\Testing\RefreshDataBase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Tests\TestCase;

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => $this->faker->numberBetween(3, 6),
            'user_id' => $user2->id,
        ]);

        $response = $this->actingAs($user1)->postJson("/v2/road_items/operator/update/{$roadItem->id}");

        $response->assertForbidden();
    }

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => 2,
            'user_id' => $user->id
        ]);

        $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}");

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

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => 2,
            'user_id' => $user->id
        ]);

        $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}");

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

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => 2,
            'user_id' => $user->id
        ]);

        $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [
            'amount' => $this->faker->name
        ]);

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

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => 2,
            'user_id' => $user->id
        ]);

        $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [
            'before_image' => $this->faker->randomNumber()
        ]);

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

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => 2,
            'user_id' => $user->id
        ]);

        $image = UploadedFile::fake()->create('image.jpg', 5096);

        $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [
            'before_image' => $image
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'before_image' => __('validation.max.file', ['attribute' => 'before image', 'max' => 4096])
            ]);
    }

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => 2,
            'user_id' => $user->id
        ]);

        $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [
            'after_image' => $this->faker->randomNumber()
        ]);

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

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => 2,
            'user_id' => $user->id
        ]);

        $image = UploadedFile::fake()->create('image.jpg', 5096);

        $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [
            'after_image' => $image
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'after_image' => __('validation.max.file', ['attribute' => 'after image', 'max' => 4096])
            ]);
    }

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => 2,
            'user_id' => $user->id
        ]);

        $infoItem = InfoItem::factory()->create([
            'item' => $roadItem->item,
            'sub_item' => $roadItem->sub_item,
            'needs_end_point' => $this->faker->numberBetween(1, 10),
        ]);

        $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [
            'start_point' => $this->faker->numberBetween(1, 10),
            'amount' => $this->faker->numberBetween(1, 10),
        ]);

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

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

        $roadItem = RoadItemsProject::factory()->create([
            'status' => 2,
            'user_id' => $user->id
        ]);

        $infoItem = InfoItem::factory()->create([
            'item' => $roadItem->item,
            'sub_item' => $roadItem->sub_item,
        ]);

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

        $userActivityLog = UserActivityLog::factory()->create();

        Storage::fake();

        $amount = $this->faker->numberBetween(1, 10);

        $response = $this->actingAs($user)->postJson("/v2/road_items/operator/update/{$roadItem->id}", [
            'start_point' => '12,12',
            'amount' => $amount,
        ]);

        $response->assertOk()
            ->assertExactJson([
                'message' => 'succussful'
            ]);

        $this->assertDatabaseHas('road_items_projects', [
            'start_lat' => 12,
            'start_lng' => 12,
            'sub_item_data' => $amount,
            'status' => 0,
            'status_fa' => 'در حال بررسی',
        ]);
    }
}
