<?php

namespace Tests\Feature\V2\Dashboard\RoadPatrolProject;

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\RoadPatrol;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDataBase;
use Tests\TestCase;

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

        $roadPatrol = RoadPatrol::factory()->create();

        $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}");

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

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

        $roadPatrol = RoadPatrol::factory()->create();

        $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}", [
            'type' => 1
        ]);

        $response->assertForbidden();
    }

    public function test_user_can_partial_delete_a_observed_item_and_road_patrol(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'partial-delete-road-patrol'
            ]))
            ->create();

        $roadPatrol = RoadPatrol::factory()->create();
        $observedItems = ObservedItem::factory()->create([
            'road_patrol_id' => $roadPatrol->id
        ]);

        $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}", [
            'type' => 1
        ]);

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

        $this->assertDatabaseMissing('road_patrols', [
            'id' => $roadPatrol->id,
            'operator_name' => $roadPatrol->operator_name,
            'operator_id' => $roadPatrol->operator_id,
        ]);

        $this->assertDatabaseMissing('observed_items', [
            'item_name' => $observedItems->item_name,
            'road_patrol_id' => $observedItems->road_patrol_id,
        ]);
    }

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

        $roadPatrol = RoadPatrol::factory()->create();

        $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}", [
            'type' => 2
        ]);

        $response->assertForbidden();
    }

    public function test_user_can_complete_delete_a_observed_item_and_road_patrol(): void
    {
        $user = User::factory()
            ->has(Permission::factory([
                'name' => 'complete-delete-road-patrol'
            ]))
            ->create();

        $roadPatrol = RoadPatrol::factory()->create();
        $logList = LogList::factory()->create([
            'log_unique_code' => 1144
        ]);
        $observedItems = ObservedItem::factory()->create([
            'road_patrol_id' => $roadPatrol->id
        ]);

        $response = $this->actingAs($user)->postJson("v2/road_patrols/supervisor/cartable/delete/{$roadPatrol->id}", [
            'type' => 2
        ]);

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

        $this->assertDatabaseMissing('road_patrols', [
            'id' => $roadPatrol->id,
            'operator_name' => $roadPatrol->operator_name,
            'operator_id' => $roadPatrol->operator_id,
        ]);

        $this->assertDatabaseMissing('observed_items', [
            'item_name' => $observedItems->item_name,
            'road_patrol_id' => $observedItems->road_patrol_id,
        ]);
    }
}
