<?php

namespace Tests\Feature;

use App\Models\BorrowRequest;
use App\Models\Divisi;
use App\Models\Item;
use App\Models\ItemRequest;
use App\Models\RequestItem;
use App\Models\Room;
use App\Models\RoomBooking;
use App\Models\User;
use App\Models\Vehicle;
use App\Models\VehicleBooking;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class HistoryFlowTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_history_page_only_shows_own_final_histories(): 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,
        ]);
        $pendingItem = Item::create([
            'nama' => 'Router Pending',
            'sku' => 'RTR-001',
            'jumlah' => 5,
            'min_stok' => 1,
            'divisi_id' => $divisi->id,
        ]);

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

        BorrowRequest::create([
            'user_id' => $user->id,
            'item_id' => $item->id,
            'jumlah' => 2,
            'tanggal_pinjam' => now()->subDays(3),
            'tanggal_kembali' => now()->subDay(),
            'status' => 'returned',
        ]);

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

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

        $response->assertOk();
        $response->assertSee('Sudah Kembali');
        $response->assertDontSee('Router Pending');
        $response->assertDontSee($otherUser->name);
    }

    public function test_history_page_groups_final_records_from_all_supported_features(): void
    {
        $divisi = Divisi::create(['nama' => 'Operasional']);
        $user = User::factory()->create(['role' => 'user', 'divisi_id' => $divisi->id]);
        $item = Item::create([
            'nama' => 'Kamera Dokumentasi',
            'sku' => 'CAM-001',
            'jumlah' => 4,
            'min_stok' => 1,
            'divisi_id' => $divisi->id,
        ]);
        $requestItem = RequestItem::create([
            'short_name' => 'ATK',
            'full_name' => 'Alat Tulis Kantor',
            'display_name' => 'Alat Tulis Kantor',
            'unit' => 'pcs',
            'stock' => 10,
            'min_stock' => 1,
            'is_active' => true,
        ]);
        $room = Room::create([
            'name' => 'Ruang Rapat Utama',
            'building' => 'Gedung A',
            'floor' => '2',
            'capacity' => 20,
            'type' => 'meeting',
            'manual_status' => 'available',
        ]);
        $vehicle = Vehicle::create([
            'name' => 'Mobil Operasional',
            'type' => 'mobil',
            'plate_number' => 'B 1234 SIP',
            'brand' => 'Toyota',
            'model' => 'Avanza',
            'year' => 2024,
            'color' => 'Hitam',
            'fuel_type' => 'Bensin',
            'capacity' => 7,
            'status' => 'available',
            'divisi_id' => $divisi->id,
        ]);

        BorrowRequest::create([
            'user_id' => $user->id,
            'item_id' => $item->id,
            'jumlah' => 1,
            'tanggal_pinjam' => now()->subDays(4),
            'tanggal_kembali' => now()->subDays(2),
            'status' => 'returned',
        ]);
        ItemRequest::create([
            'user_id' => $user->id,
            'request_item_id' => $requestItem->id,
            'quantity' => 3,
            'unit_snapshot' => 'pcs',
            'item_name_snapshot' => 'Alat Tulis Kantor',
            'request_date' => now()->subDays(3)->toDateString(),
            'realization_date' => now()->subDay()->toDateString(),
            'purpose' => 'Kebutuhan rapat',
            'status' => 'approved',
        ]);
        RoomBooking::create([
            'user_id' => $user->id,
            'room_id' => $room->id,
            'title' => 'Rapat Koordinasi',
            'start_time' => now()->subDays(2),
            'end_time' => now()->subDays(2)->addHours(2),
            'status' => 'approved',
        ]);
        VehicleBooking::create([
            'user_id' => $user->id,
            'vehicle_id' => $vehicle->id,
            'purpose' => 'Kunjungan Lapangan',
            'destination' => 'Kantor Cabang',
            'start_time' => now()->subDays(2),
            'end_time' => now()->subDays(2)->addHours(3),
            'status' => 'approved',
        ]);

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

        $response->assertOk();
        $response->assertSee('Pinjam Barang');
        $response->assertSee('Permintaan Barang');
        $response->assertSee('Pinjam Ruangan');
        $response->assertSee('Pinjam Kendaraan');
        $response->assertSee('Kamera Dokumentasi');
        $response->assertSee('Alat Tulis Kantor');
        $response->assertSee('Ruang Rapat Utama');
        $response->assertSee('Mobil Operasional');
    }

    public function test_sekretariat_can_open_admin_history_page_and_see_user_owner(): void
    {
        $sekretariat = User::factory()->create(['role' => 'sekretariat']);
        $owner = User::factory()->create(['role' => 'user', 'name' => 'Pemilik Riwayat']);
        $divisi = Divisi::create(['nama' => 'Divisi Admin Histori']);
        $item = Item::create([
            'nama' => 'Laptop Arsip',
            'sku' => 'LAP-001',
            'jumlah' => 5,
            'min_stok' => 1,
            'divisi_id' => $divisi->id,
        ]);

        BorrowRequest::create([
            'user_id' => $owner->id,
            'item_id' => $item->id,
            'jumlah' => 1,
            'tanggal_pinjam' => now()->subDays(3),
            'tanggal_kembali' => now()->subDay(),
            'status' => 'returned',
        ]);

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

        $response->assertOk();
        $response->assertSee('Pemilik Riwayat');
        $response->assertSee('Laptop Arsip');
    }

    public function test_sekretariat_can_filter_history_by_owner(): void
    {
        $sekretariat = User::factory()->create([
            'role' => 'sekretariat',
            'name' => 'Admin Sekretariat',
        ]);
        $owner = User::factory()->create([
            'role' => 'user',
            'name' => 'User Dipilih',
        ]);
        $divisi = Divisi::create(['nama' => 'Divisi Filter Histori']);
        $adminItem = Item::create([
            'nama' => 'Barang Milik Admin',
            'sku' => 'ADM-001',
            'jumlah' => 5,
            'min_stok' => 1,
            'divisi_id' => $divisi->id,
        ]);
        $userItem = Item::create([
            'nama' => 'Barang Milik User',
            'sku' => 'USR-001',
            'jumlah' => 5,
            'min_stok' => 1,
            'divisi_id' => $divisi->id,
        ]);

        BorrowRequest::create([
            'user_id' => $sekretariat->id,
            'item_id' => $adminItem->id,
            'jumlah' => 1,
            'tanggal_pinjam' => now()->subDays(4),
            'tanggal_kembali' => now()->subDays(2),
            'status' => 'returned',
        ]);
        BorrowRequest::create([
            'user_id' => $owner->id,
            'item_id' => $userItem->id,
            'jumlah' => 1,
            'tanggal_pinjam' => now()->subDays(4),
            'tanggal_kembali' => now()->subDays(2),
            'status' => 'returned',
        ]);

        $mineResponse = $this->actingAs($sekretariat)->get(route('history.index', ['owner' => 'mine']));
        $mineResponse->assertOk();
        $mineResponse->assertSee('Barang Milik Admin');
        $mineResponse->assertDontSee('Barang Milik User');

        $selectedUserResponse = $this->actingAs($sekretariat)->get(route('history.index', ['owner' => 'user:' . $owner->id]));
        $selectedUserResponse->assertOk();
        $selectedUserResponse->assertSee('Barang Milik User');
        $selectedUserResponse->assertDontSee('Barang Milik Admin');
        $selectedUserResponse->assertSee('User Dipilih - ' . $owner->email);
    }
}
