<?php

namespace Tests\Feature;

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

class ItemManagementTest extends TestCase
{
    use RefreshDatabase;

    public function test_staff_can_store_inventory_item(): void
    {
        $divisi = Divisi::create(['nama' => 'Gudang']);
        $staff = User::factory()->create(['role' => 'staff_bidang', 'divisi_id' => $divisi->id]);

        $response = $this->actingAs($staff)->post(route('items.store'), [
            'nama' => 'Printer',
            'sku' => 'PRN-001',
            'deskripsi' => 'Printer kantor',
            'divisi_id' => $divisi->id,
            'jumlah' => 4,
            'min_stok' => 1,
        ]);

        $response->assertRedirect(route('items.index'));
        $this->assertDatabaseHas('items', [
            'nama' => 'Printer',
            'sku' => 'PRN-001',
            'jumlah' => 4,
            'divisi_id' => $divisi->id,
        ]);
    }

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

        $response = $this->actingAs($user)->get(route('items.create'));

        $response->assertForbidden();
    }
}
