<?php

namespace Tests\Feature;

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

class HistoryFlowTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_history_page_only_shows_own_requests(): void
    {
        $divisi = Divisi::create(['nama' => 'Divisi Histori']);
        $user = User::factory()->create(['role' => 'user']);
        $otherUser = User::factory()->create(['role' => 'user']);
        $item = Item::create([
            'nama' => 'Tablet',
            'sku' => 'TAB-001',
            'jumlah' => 5,
            'min_stok' => 1,
            'divisi_id' => $divisi->id,
        ]);

        BorrowRequest::create([
            'user_id' => $user->id,
            'item_id' => $item->id,
            'jumlah' => 1,
            'tanggal_pinjam' => now()->subDay(),
            'tanggal_kembali' => now()->addDay(),
            'status' => 'pending',
        ]);

        BorrowRequest::create([
            'user_id' => $otherUser->id,
            'item_id' => $item->id,
            'jumlah' => 1,
            'tanggal_pinjam' => now()->subDay(),
            'tanggal_kembali' => now()->addDay(),
            'status' => 'approved',
        ]);

        $response = $this->actingAs($user)->get(route('history.index'));

        $response->assertOk();
        $response->assertSee('pending');
        $response->assertDontSee('approved');
    }

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

        $response = $this->actingAs($sekretariat)->get(route('history.admin'));

        $response->assertOk();
    }
}
