<?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 Maatwebsite\Excel\Facades\Excel;
use Tests\TestCase;

class ReportFlowTest extends TestCase
{
    use RefreshDatabase;

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

        $response = $this->actingAs($sekretariat)->get(route('reports.index'));

        $response->assertOk();
    }

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

        $this->actingAs($sekretariat)->get(route('reports.export.excel', [
            'report_type' => 'borrowing',
        ]));

        Excel::assertDownloaded('Laporan_Borrowing_' . now()->format('d-m-Y') . '.xlsx');
    }

    public function test_reports_pdf_export_downloads_file(): void
    {
        $sekretariat = User::factory()->create(['role' => 'sekretariat']);
        $divisi = Divisi::create(['nama' => 'Divisi Report']);
        $item = Item::create([
            'nama' => 'Monitor',
            'sku' => 'MON-001',
            'jumlah' => 5,
            'min_stok' => 1,
            'divisi_id' => $divisi->id,
        ]);
        BorrowRequest::create([
            'user_id' => $sekretariat->id,
            'item_id' => $item->id,
            'jumlah' => 1,
            'tanggal_pinjam' => now()->subDay(),
            'tanggal_kembali' => now()->addDay(),
            'status' => 'approved',
        ]);

        $response = $this->actingAs($sekretariat)->get(route('reports.export.pdf', [
            'report_type' => 'borrowing',
        ]));

        $response->assertOk();
        $response->assertHeader('content-type', 'application/pdf');
    }
}
