<?php
namespace App\Controllers\Admin;

use Core\View;
use App\Models\Incident;
use App\Models\Responder;
use App\Models\User;

class DashboardController
{
    public function index()
    {
        // Fetch quick statistics for the Admin Dashboard
        $stats = [
            'total_incidents' => count(Incident::all() ?: []),
            'pending_incidents' => count(Incident::whereAll('status', 'pending') ?: []),
            'active_responders' => count(Responder::whereAll('is_available', 1) ?: []),
            'total_users' => count(User::all() ?: [])
        ];

        // Fetch recent pending or verified emergencies that need attention
        $sql = "
            SELECT i.*, c.name as category_name, v.full_name as victim_name 
            FROM incidents i
            LEFT JOIN categories c ON i.category_id = c.id
            LEFT JOIN users v ON i.victim_id = v.id
            WHERE i.status IN ('pending', 'verified')
            ORDER BY i.created_at DESC LIMIT 10
        ";
        $recentIncidents = Incident::fetchAll($sql);

        View::make('admin/dashboard')
            ->setLayout('admin_layout')
            ->with([
                'title' => 'Admin Command Center - ERCS',
                'stats' => $stats,
                'recentIncidents' => $recentIncidents
            ])
            ->render();
    }
}