<?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_allowed_roles_can_open_request_item_index_and_create_pages(): void
    {
        foreach (['staff_bidang', 'kepala_bidang'] as $role) {
            $user = User::factory()->create(['role' => $role]);

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

    public function test_request_item_form_shows_extended_unit_options(): void
    {
        $staff = User::factory()->create(['role' => 'staff_bidang']);

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

        $response->assertOk();
        foreach (['buah', 'set', 'box', 'rim', 'lembar', 'roll', 'kg', 'liter', 'botol', 'pasang', 'lusin', 'batang', 'keping'] as $unit) {
            $response->assertSee('value="' . $unit . '"', false);
        }
    }

    public function test_show_route_redirects_to_edit_page(): void
    {
        $staff = User::factory()->create(['role' => 'staff_bidang']);
        $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($staff)->get(route('admin.request-items.show', $item));

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

    public function test_allowed_role_can_update_request_item(): void
    {
        $staff = User::factory()->create(['role' => 'staff_bidang']);
        $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($staff)->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,
        ]);
    }

    public function test_regular_user_cannot_access_request_item_admin_pages_or_actions(): void
    {
        $user = User::factory()->create(['role' => 'user']);
        $item = RequestItem::create([
            'short_name' => 'Baterai',
            'full_name' => 'Baterai AA',
            'display_name' => 'Baterai (Baterai AA)',
            'unit' => 'pcs',
            'stock' => 10,
            'min_stock' => 2,
            'is_active' => true,
        ]);

        $this->actingAs($user)->get(route('admin.request-items.index'))->assertForbidden();
        $this->actingAs($user)->get(route('admin.request-items.create'))->assertForbidden();
        $this->actingAs($user)->get(route('admin.request-items.edit', $item))->assertForbidden();
        $this->actingAs($user)->get(route('admin.request-items.show', $item))->assertForbidden();

        $this->actingAs($user)->put(route('admin.request-items.update', $item), [
            'short_name' => 'Baterai Baru',
            'full_name' => 'Baterai AA Baru',
            'unit' => 'pcs',
            'stock' => 15,
            'min_stock' => 3,
            'is_active' => 1,
        ])->assertForbidden();

        $this->actingAs($user)->delete(route('admin.request-items.destroy', $item))->assertForbidden();

        $this->assertDatabaseHas('request_items', [
            'id' => $item->id,
            'short_name' => 'Baterai',
        ]);
    }

    public function test_sekretariat_cannot_access_request_item_admin_pages_or_actions(): void
    {
        $sekretariat = User::factory()->create(['role' => 'sekretariat']);
        $item = RequestItem::create([
            'short_name' => 'Kertas',
            'full_name' => 'Kertas HVS',
            'display_name' => 'Kertas (Kertas HVS)',
            'unit' => 'rim',
            'stock' => 10,
            'min_stock' => 2,
            'is_active' => true,
        ]);

        $this->actingAs($sekretariat)->get(route('admin.request-items.index'))->assertForbidden();
        $this->actingAs($sekretariat)->get(route('admin.request-items.create'))->assertForbidden();
        $this->actingAs($sekretariat)->get(route('admin.request-items.edit', $item))->assertForbidden();
        $this->actingAs($sekretariat)->get(route('admin.request-items.show', $item))->assertForbidden();

        $this->actingAs($sekretariat)->put(route('admin.request-items.update', $item), [
            'short_name' => 'Kertas Baru',
            'full_name' => 'Kertas HVS Baru',
            'unit' => 'rim',
            'stock' => 15,
            'min_stock' => 3,
            'is_active' => 1,
        ])->assertForbidden();

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

        $this->assertDatabaseHas('request_items', [
            'id' => $item->id,
            'short_name' => 'Kertas',
        ]);
    }
}
