<?php

namespace Tests\Feature\V3\AzmayeshType;

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

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

        $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.delete', [$azmayeshType->id]));

        $response->assertForbidden();
    }

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

        $azmayeshType = AzmayeshType::factory()->create();
        $azmayeshField = AzmayeshField::factory()->create();

        $azmayeshType->azmayeshFields()->attach([$azmayeshField->id]);

        $response = $this->actingAs($user)->postJson(route('v3.azmayesh_types.delete', [$azmayeshType->id]));

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

        $this->assertDatabaseMissing('azmayesh_types', [
            'name' => $azmayeshType->name,
        ]);

        $this->assertDatabaseMissing('azmayesh_field_azmayesh_type', [
            'azmayesh_type_id' => $azmayeshType->id,
            'azmayesh_field_id' => $azmayeshField->id,
        ]);
    }
}
