<?php
namespace App\Http\Controllers\Student;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
{
    public function index()
    {
        $student = Auth::user();
        $applications = $student->applications()
            ->with(['program.university', 'scholarship'])
            ->latest()
            ->take(5)
            ->get();
        $notifications = $student->notifications()->latest()->take(5)->get();
        $recentDocuments = $student->documents()
            ->with(['application.program.university'])
            ->latest()
            ->take(4)
            ->get();

        $statusBreakdown = $student->applications()
            ->selectRaw('status, COUNT(*) as aggregate')
            ->groupBy('status')
            ->pluck('aggregate', 'status');

        $stats = [
            'total' => $student->applications()->count(),
            'in_progress' => $student->applications()
                ->whereIn('status', ['submitted', 'under_review', 'visa_processing'])
                ->count(),
            'approved' => $student->applications()->where('status', 'approved')->count(),
            'documents' => $student->documents()->count(),
        ];

        return view('student.dashboard', compact(
            'applications',
            'notifications',
            'recentDocuments',
            'statusBreakdown',
            'stats'
        ));
    }
}
