<?php

namespace Tests\Feature;

use App\Models\Divisi;
use App\Models\Item;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class ItemManagementTest extends TestCase
{
    use RefreshDatabase;

    public function test_staff_can_store_inventory_item(): void
    {
        $divisi = Divisi::create(['nama' => 'Gudang']);
        $staff = User::factory()->create(['role' => 'staff_bidang', 'divisi_id' => $divisi->id]);

        $response = $this->actingAs($staff)->post(route('items.store'), [
            'nama' => 'Printer',
            'sku' => 'PRN-001',
            'deskripsi' => 'Printer kantor',
            'divisi_id' => $divisi->id,
            'jumlah' => 4,
            'min_stok' => 1,
        ]);

        $response->assertRedirect(route('items.index'));
        $this->assertDatabaseHas('items', [
            'nama' => 'Printer',
            'sku' => 'PRN-001',
            'jumlah' => 4,
            'divisi_id' => $divisi->id,
        ]);
    }

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

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

        $response->assertForbidden();
    }

    public function test_inventory_item_photo_upload_is_stored_on_public_disk(): void
    {
        Storage::fake('public');

        $divisi = Divisi::create(['nama' => 'Gudang Foto']);
        $staff = User::factory()->create(['role' => 'staff_bidang', 'divisi_id' => $divisi->id]);

        $response = $this->actingAs($staff)->post(route('items.store'), [
            'nama' => 'Kamera Upload',
            'sku' => 'KUP-001',
            'deskripsi' => 'Kamera dengan foto',
            'divisi_id' => $divisi->id,
            'jumlah' => 2,
            'min_stok' => 1,
            'foto' => UploadedFile::fake()->image('kamera.jpg', 800, 600),
        ]);

        $response->assertRedirect(route('items.index'));

        $item = Item::where('sku', 'KUP-001')->firstOrFail();
        $this->assertNotNull($item->image);
        Storage::disk('public')->assertExists($item->image);
    }

    public function test_inventory_item_photo_upload_must_not_exceed_two_megabytes(): void
    {
        Storage::fake('public');

        $divisi = Divisi::create(['nama' => 'Gudang Limit Foto']);
        $staff = User::factory()->create(['role' => 'staff_bidang', 'divisi_id' => $divisi->id]);

        $response = $this->actingAs($staff)->from(route('items.create'))->post(route('items.store'), [
            'nama' => 'Kamera Besar',
            'sku' => 'KBR-001',
            'deskripsi' => 'Foto terlalu besar',
            'divisi_id' => $divisi->id,
            'jumlah' => 2,
            'min_stok' => 1,
            'foto' => UploadedFile::fake()->image('kamera-besar.jpg', 800, 600)->size(2049),
        ]);

        $response->assertRedirect(route('items.create'));
        $response->assertSessionHasErrors('foto');
        $this->assertDatabaseMissing('items', ['sku' => 'KBR-001']);
    }
}
