<?php

namespace Tests\Feature\V3\AzmayeshSample;

use App\Models\Azmayesh;
use App\Models\AzmayeshSample;
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 StoreTest extends TestCase
{
    use RefreshDatabase, WithFaker;
    /**
     * A basic feature test example.
     */
    public function test_user_should_have_azmayesh_management_permission(): void
    {
        $user = User::factory()->create();

        $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store'));

        $response->assertForbidden();
    }

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

        $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store'));

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

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

        $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store'), [
            'azmayesh_id' => $this->faker->numberBetween(10, 100)
        ]);

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

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

        $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store'));

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

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

        $data = [
            'azmayesh_id' => $azmayesh->id,
            'data' => json_encode([$this->faker->numerify]),
        ];

        $response = $this->actingAs($user)->postJson(route('v3.azmayesh_samples.store'), $data);

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

        $this->assertDatabaseHas('azmayesh_samples', [
            'azmayesh_id' => $data['azmayesh_id'],
//            'data' => $data['data'],
        ]);

        $this->assertEquals(AzmayeshSample::first()->data, $data['data']);
    }
}
