<?php

namespace App\Http\Controllers;

use App\Services\DummyDataService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class KatalogController extends Controller
{
    public function index(Request $request)
    {
        $user = Auth::user();

        $isBanned = false;
        if ($user && $user->role === 'siswa') {
            $bukuPinjam = DummyDataService::getBukuPinjamOffline($user);
            $isTelat = collect($bukuPinjam)->contains(fn($b) => $b['sisa_hari'] < 0);
            $isBannedManual = $user->is_banned ?? false;
            $isBanned = $isTelat || $isBannedManual;
        }

        $filters = $request->only(['search', 'kategori', 'tahun', 'penulis']);
        $semuaBuku = DummyDataService::getKatalogBuku($filters);
        $filterOptions = DummyDataService::getFilterOptions();

        return view('katalog.index', [
            'semuaBuku' => $semuaBuku,
            'filterOptions' => $filterOptions,
            'input' => $filters,
            'pageTitle' => 'Katalog Buku',
            'mode' => 'umum',
            'isBanned' => $isBanned,
        ]);
    }
}