<?php

namespace Tests\Feature;

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

class AdminRequestItemPagesTest extends TestCase
{
    use RefreshDatabase;

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

        $this->actingAs($sekretariat)->get(route('admin.request-items.index'))->assertOk();
        $this->actingAs($sekretariat)->get(route('admin.request-items.create'))->assertOk();
    }

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

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

        $response->assertRedirect(route('admin.request-items.edit', $item));
    }

    public function test_sekretariat_can_update_request_item(): void
    {
        $sekretariat = User::factory()->create(['role' => 'sekretariat']);
        $item = RequestItem::create([
            'short_name' => 'SSD',
            'full_name' => 'Solid State Drive',
            'display_name' => 'SSD (Solid State Drive)',
            'unit' => 'pcs',
            'stock' => 4,
            'min_stock' => 1,
            'is_active' => true,
        ]);

        $response = $this->actingAs($sekretariat)->put(route('admin.request-items.update', $item), [
            'short_name' => 'SSD Baru',
            'full_name' => 'Solid State Drive NVMe',
            'unit' => 'pcs',
            'stock' => 8,
            'min_stock' => 2,
            'description' => 'Versi baru',
            'is_active' => 1,
        ]);

        $response->assertRedirect(route('admin.request-items.index'));
        $this->assertDatabaseHas('request_items', [
            'id' => $item->id,
            'short_name' => 'SSD Baru',
            'display_name' => 'SSD Baru (Solid State Drive NVMe)',
            'stock' => 8,
        ]);
    }
}
