<?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 ReturnVerificationPagesTest extends TestCase
{
    use RefreshDatabase;

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

        $response = $this->actingAs($sekretariat)->get(route('verification.returns'));

        $response->assertOk();
    }

    public function test_legacy_return_approve_route_restores_stock(): void
    {
        $divisi = Divisi::create(['nama' => 'Divisi Return Legacy']);
        $sekretariat = User::factory()->create(['role' => 'sekretariat']);
        $borrower = User::factory()->create(['role' => 'user', 'divisi_id' => $divisi->id]);
        $item = Item::create([
            'nama' => 'Mixer',
            'sku' => 'MXR-001',
            'jumlah' => 0,
            'min_stok' => 1,
            'divisi_id' => $divisi->id,
        ]);
        $borrow = BorrowRequest::create([
            'user_id' => $borrower->id,
            'item_id' => $item->id,
            'jumlah' => 1,
            'tanggal_pinjam' => now()->subDay(),
            'tanggal_kembali' => now(),
            'status' => 'return_pending',
        ]);

        $response = $this->actingAs($sekretariat)->post(route('verification.returns.approve', $borrow->id));

        $response->assertSessionHas('success');
        $this->assertSame('returned', $borrow->fresh()->status);
        $this->assertSame(1, $item->fresh()->jumlah);
    }
}
