<?php
declare(strict_types=1);
namespace App\Controllers;

use App\Core\Controller;
use App\Core\Database;
use App\Core\SessionAuth;

final class DashboardController extends Controller {
    public function index(): void {
        app()->get(SessionAuth::class)->requireLogin();
        $db = Database::instance();
        $stats = [
            'customers' => (int)$db->query("SELECT COUNT(*) FROM customers")->fetchColumn(),
            'items' => (int)$db->query("SELECT COUNT(*) FROM items WHERE is_active=1")->fetchColumn(),
            'invoices' => (int)$db->query("SELECT COUNT(*) FROM invoices")->fetchColumn(),
            'queue_pending' => (int)$db->query("SELECT COUNT(*) FROM queue_tickets WHERE ticket_status='pending'")->fetchColumn(),
        ];
        $recentInvoices = $db->query("SELECT i.id, i.invoice_no, i.invoice_status, i.grand_total, c.full_name customer_name FROM invoices i JOIN customers c ON c.id=i.customer_id ORDER BY i.id DESC LIMIT 8")->fetchAll();
        $this->view('dashboard/index', compact('stats','recentInvoices'));
    }
}
