<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Koleksi;
use App\Models\Member;
use App\Models\BorrowTransaction;
use Illuminate\Http\Request;

class StatistikController extends Controller
{
    public function index()
    {
        $totalBuku = Koleksi::count('kode_eksemplar'); 
        $totalAnggota = Member::count();
        $totalPeminjaman = BorrowTransaction::where('status', 'dipinjam')->count();
        $totalPengembalian = BorrowTransaction::where('status', 'dikembalikan')->count();

        $peminjamanPerBulan = BorrowTransaction::selectRaw('MONTH(tanggal_pinjam) as bulan, COUNT(*) as total')
            ->groupBy('bulan')
            ->orderBy('bulan')
            ->pluck('total', 'bulan');

        $bulanLabels = ['Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Agu', 'Sep', 'Okt', 'Nov', 'Des'];
        $dataPeminjaman = [];

        for ($i = 1; $i <= 12; $i++) {
            $dataPeminjaman[] = $peminjamanPerBulan[$i] ?? 0;
        }

        return view('admin.statistik', compact(
            'totalBuku',
            'totalAnggota',
            'totalPeminjaman',
            'totalPengembalian',
            'bulanLabels',
            'dataPeminjaman'
        ));
    }
}
