<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\PilihanSekolah;
use App\Models\Siswa;
use Illuminate\Http\JsonResponse;

class DashboardController extends Controller
{
    public function index(): JsonResponse
    {
        $totalSiswa        = Siswa::count();
        $lulus             = Siswa::where('status_kelulusan', 'lulus')->count();
        $tidakLulus        = Siswa::where('status_kelulusan', 'tidak_lulus')->count();
        $sudahPilihSekolah = PilihanSekolah::count();

        $rekapJenjang = PilihanSekolah::selectRaw('jenjang, count(*) as total')
            ->groupBy('jenjang')
            ->get()
            ->map(fn($r) => ['jenjang' => $r->jenjang, 'total' => $r->total]);

        return response()->json([
            'success'            => true,
            'total_siswa'        => $totalSiswa,
            'lulus'              => $lulus,
            'tidak_lulus'        => $tidakLulus,
            'sudah_pilih_sekolah'=> $sudahPilihSekolah,
            'rekap_jenjang'      => $rekapJenjang,
        ]);
    }
}
