<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Attendance;
use App\Models\Certificate;
use App\Models\EmailLog;
use App\Models\Participant;
use App\Models\Program;
use App\Models\QuestionnaireResponse;

class DashboardController extends Controller
{
    public function index()
    {
        $stats = [
            'total_programs'      => Program::count(),
            'active_programs'     => Program::where('status', 'published')->count(),
            'total_participants'  => Participant::count(),
            'total_attendances'   => Attendance::count(),
            'total_certificates'  => Certificate::count(),
            'generated_certs'     => Certificate::whereIn('status', ['generated', 'emailed', 'downloaded'])->count(),
            'downloaded_certs'    => Certificate::where('status', 'downloaded')->count(),
            'total_responses'     => QuestionnaireResponse::count(),
            'pending_emails'      => EmailLog::where('status', 'pending')->count(),
        ];

        $recentPrograms = Program::with('creator')
            ->latest()
            ->limit(5)
            ->get();

        return view('admin.dashboard', compact('stats', 'recentPrograms'));
    }
}
