<?php

namespace Tests\Feature\V3\AzmayeshSample;

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

class IndexTest 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)->getJson(route('v3.azmayesh_samples.index', [
            1,
            'start' => 0,
            'size' => 10,
            'filters' => json_encode([]),
            'sorting' => json_encode([]),
        ]));

        $response->assertForbidden();
    }

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

        Azmayesh::factory(5)
            ->sequence(
                ['id' => 1],
                ['id' => 2],
                ['id' => 3],
                ['id' => 4],
                ['id' => 5]
            )
            ->create();

        AzmayeshSample::factory(5)
            ->sequence(
                [
                    'azmayesh_id' => 1,
                    'azmayesh_project_name' => $this->faker->name,
                    'data' => json_encode([$this->faker->numerify]),
                ],
                [
                    'azmayesh_id' => 2,
                    'azmayesh_project_name' => $this->faker->name,
                    'data' => json_encode([$this->faker->numerify]),
                ],
                [
                    'azmayesh_id' => 3,
                    'azmayesh_project_name' => $this->faker->name,
                    'data' => json_encode([$this->faker->numerify]),
                ],
                [
                    'azmayesh_id' => 4,
                    'azmayesh_project_name' => $this->faker->name,
                    'data' => json_encode([$this->faker->numerify]),
                ],
                [
                    'azmayesh_id' => 1,
                    'azmayesh_project_name' => $this->faker->name,
                    'data' => json_encode([$this->faker->numerify]),
                ],
            )
            ->create();

        $response = $this->actingAs($user)->getJson(route('v3.azmayesh_samples.index', [
            1,
            'start' => 0,
            'size' => 10,
            'filters' => json_encode([]),
            'sorting' => json_encode([]),
        ]));

        $response->assertOk();

        $response->assertJsonStructure([
            'data' => [
                '*' => [
                    'id',
                    'azmayesh_id',
                    'azmayesh_project_name',
                    'data',
                ],
            ],
            "meta" => [
                "totalRowCount"
            ]
        ]);
    }
}
