<?php

namespace App\Http\Controllers\Guru;

use App\Http\Controllers\Controller;
use App\Models\Mengajar;
use App\Models\Siswa;
use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
{
    public function index()
    {
        $guru = Auth::guard('guru')->user();

        try {
            $totalKelas = Mengajar::where('id_guru', $guru->id_guru)
                ->distinct('id_kelas')
                ->count('id_kelas');

            $totalMapel = Mengajar::where('id_guru', $guru->id_guru)
                ->distinct('id_mapel')
                ->count('id_mapel');

            $kelasIds = Mengajar::where('id_guru', $guru->id_guru)
                ->pluck('id_kelas')
                ->unique();

            $totalSiswa = Siswa::whereIn('id_kelas', $kelasIds)->count();

        } catch (\Exception $e) {
            $totalKelas = 0;
            $totalMapel = 0;
            $totalSiswa = 0;
        }

        return view('guru.dashboard', compact('totalKelas', 'totalMapel', 'totalSiswa'));
    }
}