<?php

namespace Tests\Feature\V3\Azmayesh;

use App\Models\Azmayesh;
use App\Models\AzmayeshType;
use App\Models\Permission;
use App\Models\Province;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class UpdateTest extends TestCase
{
    use RefreshDatabase, WithFaker;
    /**
     * A basic feature test example.
     */
    public function test_user_should_have_azmayesh_management_permission(): void
    {
        $user = User::factory()->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]));

        $response->assertForbidden();
    }

    public function test_lat_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]));

        $response->assertUnprocessable()
            ->assertInvalid([
                'lat' => __('validation.required', ['attribute' => 'طول جغرافیایی'])
            ]);
    }

    public function test_lat_attribute_should_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'lat' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'lat' => __('validation.string', ['attribute' => 'طول جغرافیایی'])
            ]);
    }

    public function test_lng_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]));

        $response->assertUnprocessable()
            ->assertInvalid([
                'lng' => __('validation.required', ['attribute' => 'عرض جغرافیایی'])
            ]);
    }

    public function test_lng_attribute_should_be_string(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'lng' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'lng' => __('validation.string', ['attribute' => 'عرض جغرافیایی'])
            ]);
    }

    public function test_azmayesh_type_id_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]));

        $response->assertUnprocessable()
            ->assertInvalid([
                'azmayesh_type_id' => __('validation.required', ['attribute' => 'نوع آزمایش'])
            ]);
    }

    public function test_azmayesh_type_id_attribute_should_exist_on_azmayesh_types_table(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'azmayesh_type_id' => $this->faker->numberBetween(10, 100)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'azmayesh_type_id' => __('validation.exists', ['attribute' => 'نوع آزمایش'])
            ]);
    }

    public function test_request_date_attribute_should_be_date_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'request_date' => $this->faker->lexify
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'request_date' => __('validation.date', ['attribute' => 'تاریخ درخواست'])
            ]);
    }

    public function test_report_date_attribute_should_be_date_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'report_date' => $this->faker->lexify
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'report_date' => __('validation.date', ['attribute' => 'تاریخ گزارش'])
            ]);
    }

    public function test_request_number_attribute_should_be_string_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'request_number' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'request_number' => __('validation.string', ['attribute' => 'شماره درخواست'])
            ]);
    }

    public function test_employer_attribute_should_be_string_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'employer' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'employer' => __('validation.string', ['attribute' => 'کارفرما'])
            ]);
    }

    public function test_contractor_attribute_should_be_string_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'contractor' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'contractor' => __('validation.string', ['attribute' => 'پیمانکار'])
            ]);
    }

    public function test_work_number_attribute_should_be_string_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'work_number' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'work_number' => __('validation.string', ['attribute' => 'شماره کار'])
            ]);
    }

    public function test_project_name_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]));

        $response->assertUnprocessable()
            ->assertInvalid([
                'project_name' => __('validation.required', ['attribute' => 'نام پروژه'])
            ]);
    }

    public function test_project_name_attribute_should_be_string_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'project_name' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'project_name' => __('validation.string', ['attribute' => 'نام پروژه'])
            ]);
    }

    public function test_consultant_attribute_should_be_string_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'consultant' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'consultant' => __('validation.string', ['attribute' => 'مشاور'])
            ]);
    }

    public function test_applicant_attribute_should_be_string_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'applicant' => $this->faker->numberBetween(1, 10)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'applicant' => __('validation.string', ['attribute' => 'متقاضی'])
            ]);
    }

    public function test_province_id_attribute_is_required(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]));

        $response->assertUnprocessable()
            ->assertInvalid([
                'province_id' => __('validation.required', ['attribute' => 'استان'])
            ]);
    }

    public function test_province_id_attribute_should_exists_on_province_table(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $azmayesh = Azmayesh::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), [
            'province_id' => $this->faker->numberBetween(10, 100)
        ]);

        $response->assertUnprocessable()
            ->assertInvalid([
                'province_id' => __('validation.exists', ['attribute' => 'استان'])
            ]);
    }

    public function test_user_can_update_the_azmayesh(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'azmayesh-management'
            ]))
            ->create();
        $province = Province::factory()->create();
        $azmayeshType = AzmayeshType::factory()->create();
        $azmayesh = Azmayesh::factory()->create();

        $data = [
            'lat' => $this->faker->numerify,
            'lng' => $this->faker->numerify,
            'azmayesh_type_id' => $azmayeshType->id,
            'request_date' => $this->faker->date,
            'report_date' => $this->faker->date,
            'request_number' => $this->faker->numerify,
            'employer' => $this->faker->name,
            'contractor' => $this->faker->name,
            'work_number' => $this->faker->numerify,
            'project_name' => $this->faker->name,
            'consultant' => $this->faker->name,
            'applicant' => $this->faker->name,
            'province_id' => $province->id,
        ];

        $response = $this->actingAs($user)->postJson(route('v3.azmayeshes.update', [$azmayesh->id]), $data);

        $response->assertOk()
            ->assertExactJson([
                'message' => __('messages.successful')
            ]);
    }
}
