<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| TechStackBase – Dashboard Summary
|--------------------------------------------------------------------------
*/

require_once __DIR__ . '/../system/bootstrap.php';
require_once __DIR__ . '/../system/response.php';
require_once __DIR__ . '/../system/auth.php';

/* =========================
   AUTH
   ========================= */
try {
    $user = require_auth($pdo);
} catch (Throwable $e) {
    error_unauthorized('Unauthorized');
}

/* =========================
   COUNTS
   ========================= */
try {

    // Users
    $users = (int)$pdo->query("
        SELECT COUNT(*) FROM users WHERE deleted_at IS NULL
    ")->fetchColumn();

    // Companies
    $companies = (int)$pdo->query("
        SELECT COUNT(*) FROM companies WHERE deleted_at IS NULL
    ")->fetchColumn();

    // Modules
    $modules = (int)$pdo->query("
        SELECT COUNT(*) FROM modules WHERE deleted_at IS NULL
    ")->fetchColumn();

    // Audit logs
    $auditLogs = (int)$pdo->query("
        SELECT COUNT(*) FROM audit_logs
    ")->fetchColumn();

} catch (Throwable $e) {
    error_server('Failed to load dashboard summary');
}

/* =========================
   RESPONSE
   ========================= */
success('Dashboard summary', [
    'users'       => $users,
    'companies'   => $companies,
    'modules'     => $modules,
    'audit_logs'  => $auditLogs
]);
