<?php

namespace Tests\Feature\V3\Damage;

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

class ActivateTest extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function test_unauthenticated_user_cannot_activate_damage(): void
    {
        $response = $this->postJson(route('v3.damages.store'));
        $response->assertStatus(401);
    }

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

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


    public function test_user_with_permission_can_activate_damage(): void
    {
        $user = User::factory()
            ->has(Permission::factory()->state(['name' => 'manage-damage']))
            ->create();

        $damage = Damage::factory()->create(['status' => 0]);

        $response = $this->actingAs($user)->postJson(route('v3.damages.activate', [$damage->id]));

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

        $this->assertDatabaseHas('damages', [
            'id' => $damage->id,
            'status' => 1,
        ]);
    }

    public function test_user_with_permission_can_deactivate_damage(): void
    {
        $user = User::factory()
            ->has(Permission::factory()->state(['name' => 'manage-damage']))
            ->create();

        $damage = Damage::factory()->create(['status' => 1]);

        $response = $this->actingAs($user)->postJson(route('v3.damages.activate', [$damage->id]));

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

        $this->assertDatabaseHas('damages', [
            'id' => $damage->id,
            'status' => 0,
        ]);
    }

}
