<?php

namespace Tests\Feature\V2\Dashboard\RoadObservation;

use App\Models\City;
use App\Models\Permission;
use App\Models\Province;
use App\Models\RoadObserved;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDataBase;
use Tests\TestCase;

class CountTest extends TestCase
{
    use RefreshDataBase;
    /**
     * A basic feature test example.
     */
    public function test_user_should_have_fastreact_permission_type(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'type' => $this->faker->name
            ]))
            ->create();

        $response = $this->actingAs($user)->getJson("/v2/road_observations/count");

        $response->assertForbidden();
    }

    public function test_user_can_see_data_for_fast_react_permission(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'show-fast-react',
                'type' => 'fastreact'
            ]))
            ->create();

        $roadObserved = RoadObserved::factory()
            ->sequence(
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
            )
            ->count(7)
            ->create();

        $response = $this->actingAs($user)->getJson("/v2/road_observations/count");

        $response->assertOk()
            ->assertJsonStructure([
                "data" => [
                        '*' => [
                            "done",
                            "pending"
                        ]
                ],
                "status"
            ]);
    }

    public function test_user_can__not_see_data_for_fast_react_province_without_province_id(): void
    {
        $permission = Permission::factory()
            ->create([
                'name' => 'show-fast-react'
            ]);

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'show-fast-react-province',
                'type' => 'fastreact',
            ]))
            ->create();

        $roadObserved = RoadObserved::factory()
            ->sequence(
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
            )
            ->count(7)
            ->create();

        $response = $this->actingAs($user)->getJson("/v2/road_observations/count");

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

    public function test_user_can_see_data_for_fast_react_province_permission(): void
    {
        $permission = Permission::factory()
            ->create([
                'name' => 'show-fast-react'
            ]);

        $province = Province::factory()->create();
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'show-fast-react-province',
                'type' => 'fastreact',
            ]))
            ->create([
                'province_id' => $province->id
            ]);

        $roadObserved = RoadObserved::factory()
            ->sequence(
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
            )
            ->count(7)
            ->create();

        $response = $this->actingAs($user)->getJson("/v2/road_observations/count");

        $response->assertOk()
            ->assertJsonStructure([
                "data" => [
                        '*' => [
                            "done",
                            "pending"
                        ]
                ],
                "status"
            ]);
    }

    public function test_user_can__not_see_data_for_fast_react_city_without_city_id(): void
    {
        $permission = Permission::factory()
            ->sequence(
                ['name' => 'show-fast-react'],
                ['name' => 'show-fast-react-province'],
            )
            ->count(2)
            ->create();

        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'show-fast-react-city',
                'type' => 'fastreact',
            ]))
            ->create();

        $roadObserved = RoadObserved::factory()
            ->sequence(
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
            )
            ->count(7)
            ->create();

        $response = $this->actingAs($user)->getJson("/v2/road_observations/count");

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

    public function test_user_can_see_data_for_fast_react_city_permission(): void
    {
        $permission = Permission::factory()
            ->sequence(
                ['name' => 'show-fast-react'],
                ['name' => 'show-fast-react-province'],
            )
            ->count(2)
            ->create();

        $city = City::factory()->create();
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'show-fast-react-city',
                'type' => 'fastreact',
            ]))
            ->create([
                'city_id' => $city->id
            ]);

        $roadObserved = RoadObserved::factory()
            ->sequence(
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
                ['rms_status' => $this->faker->numberBetween(0, 1)],
            )
            ->count(7)
            ->create();

        $response = $this->actingAs($user)->getJson("/v2/road_observations/count");

        $response->assertOk()
            ->assertJsonStructure([
                "data" => [
                        '*' => [
                            "done",
                            "pending"
                        ]
                ],
                "status"
            ]);
    }
}
