<?php

namespace Tests\Feature;

use App\Models\RequestItem;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdminRequestItemCrudTest extends TestCase
{
    use RefreshDatabase;

    public function test_sekretariat_can_store_request_item(): void
    {
        $sekretariat = User::factory()->create(['role' => 'sekretariat']);

        $response = $this->actingAs($sekretariat)->post(route('admin.request-items.store'), [
            'short_name' => 'Kabel LAN',
            'full_name' => 'Local Area Network',
            'unit' => 'meter',
            'stock' => 50,
            'min_stock' => 10,
            'description' => 'Kabel jaringan',
            'is_active' => 1,
        ]);

        $response->assertRedirect(route('admin.request-items.index'));
        $this->assertDatabaseHas('request_items', [
            'short_name' => 'Kabel LAN',
            'full_name' => 'Local Area Network',
            'display_name' => 'Kabel LAN (Local Area Network)',
            'unit' => 'meter',
            'stock' => 50,
        ]);
    }

    public function test_non_sekretariat_cannot_store_request_item(): void
    {
        $user = User::factory()->create(['role' => 'user']);

        $response = $this->actingAs($user)->post(route('admin.request-items.store'), [
            'short_name' => 'Mouse',
            'full_name' => 'Mouse USB',
            'unit' => 'pcs',
            'stock' => 5,
            'min_stock' => 1,
        ]);

        $response->assertForbidden();
        $this->assertDatabaseCount('request_items', 0);
    }

    public function test_sekretariat_can_delete_request_item(): void
    {
        $sekretariat = User::factory()->create(['role' => 'sekretariat']);
        $item = RequestItem::create([
            'short_name' => 'RJ45',
            'full_name' => 'Konektor RJ45',
            'display_name' => 'RJ45 (Konektor RJ45)',
            'unit' => 'pcs',
            'stock' => 25,
            'min_stock' => 5,
            'is_active' => true,
        ]);

        $response = $this->actingAs($sekretariat)->delete(route('admin.request-items.destroy', $item));

        $response->assertRedirect(route('admin.request-items.index'));
        $this->assertDatabaseMissing('request_items', ['id' => $item->id]);
    }
}
