<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Kriteria;
use App\Models\SubKriteria;
use App\Models\PengajuanUkt;

class HomeController extends Controller
{
     public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('admin'); 
    }

    public function showAdminDashboard()
    {
        try {
            $data = [
                'totalMahasiswa' => User::where('role', 'mahasiswa')->count(),
                'totalKriteria' => Kriteria::count(),
                'totalSubkriteria' => SubKriteria::count(),
                'totalPengajuan' => PengajuanUkt::count(),
                'penurunanUKT' => PengajuanUkt::where('jenis_pengajuan', 'penurunan')->count(),
                'pengangsuranUKT' => PengajuanUkt::where('jenis_pengajuan', 'pengangsuran')->count(),
            ];

            // @dd($data);

            return view('admin.home', $data);
            
        } catch (\Exception $e) {
            // Log the error for debugging
            \Log::error('Dashboard Error: '.$e->getMessage());
            
            return redirect()->back()->with('error', "Gagal memuat dashboard. Silakan coba lagi.");
        }
    }
}