<?php

namespace App\Controllers;

use App\Core\Controller;
use App\Helpers\Auth;
use App\Models\User;
use App\Models\AuditLog;
use App\Models\CalendarEvent;
use App\Models\Announcement;
use App\Models\Event;

class DashboardController extends Controller
{
    public function charts()
    {
        Auth::requireLogin();
        $this->view('dashboard/charts');
    }

    public function tables()
    {
        Auth::requireLogin();
        $userModel = new User();
        $users = $userModel->all();
        $this->view('dashboard/tables', ['users' => $users]);
    }

    public function index()
    {
        Auth::requireLogin();

        $userModel = new User();
        $auditModel = new AuditLog();
        $eventModel = new CalendarEvent();
        $announcementModel = new Announcement();
        $eventModel2 = new Event();

        $stats = (object) [
            'totalUsers' => $userModel->count(),
            'activeUsers' => $userModel->getActiveCount(),
            'totalAuditLogs' => $auditModel->count(),
            'upcomingEvents' => count($eventModel->getUpcoming(10)),
        ];

        $recentUsers = $userModel->getRecent(5);
        $recentLogs = $auditModel->getRecent(10);
        $upcomingEvents = $eventModel2->getUpcoming(5);
        $pendingCount = $userModel->getPendingCount();

        $this->view('dashboard/index', [
            'stats' => $stats,
            'recentUsers' => $recentUsers,
            'recentLogs' => $recentLogs,
            'upcomingEvents' => $upcomingEvents,
            'pendingCount' => $pendingCount,
        ]);
    }
}
