<?php

namespace App\Http\Controllers\SelfPaced\Dashboard;

use App\Http\Controllers\Controller;
use App\Models\SelfPaced\Course;
use App\Models\SelfPaced\Enrollment;
use App\Models\SelfPaced\ExamAttempt;
use App\Models\SelfPaced\SelfPacedCertificate;
use App\Models\SelfPaced\PaystackConfig;
use App\Models\SelfPaced\PaymentProof;

class DashboardController extends Controller
{
    /**
     * Show dashboard overview with key statistics.
     * Includes: course stats, enrollment stats, payment config status, pending proofs
     *
     * Requirements: 10.1, 10.3, 17.11
     */
    public function index()
    {
        $stats = [
            'total_courses'       => Course::count(),
            'published_courses'   => Course::where('status', 'published')->count(),
            'active_enrollments'  => Enrollment::where('status', 'active')->count(),
            'total_enrollments'   => Enrollment::count(),
            'exams_taken'         => ExamAttempt::where('status', 'submitted')->count(),
            'certificates_issued' => SelfPacedCertificate::where('is_revoked', false)->count(),
            'pending_proofs'      => PaymentProof::where('status', 'pending')->count(),
        ];

        // Paystack configuration status
        $paystackConfig = PaystackConfig::getActive();
        $paystackConfigured = $paystackConfig !== null && $paystackConfig->is_active;

        $recentEnrollments = Enrollment::with(['user', 'course'])
            ->latest()
            ->limit(10)
            ->get();

        $pendingProofs = PaymentProof::with(['enrollment.user', 'course'])
            ->where('status', 'pending')
            ->latest()
            ->limit(5)
            ->get();

        return view('self-paced.dashboard.index', compact(
            'stats', 'recentEnrollments', 'paystackConfigured', 'pendingProofs'
        ));
    }
}
