<?php
declare(strict_types=1);

require_once __DIR__ . '/../conexion.php';
require_once __DIR__ . '/../_helpers.php';
require_once __DIR__ . '/../auth/guard.php';

$u = require_login();
require_admin($u);

try {
  $k1 = (int)$pdo->query("SELECT COUNT(*) FROM sectorizacion")->fetchColumn();
  $k2 = (int)$pdo->query("SELECT COUNT(*) FROM sectores")->fetchColumn();
  $k3 = (int)$pdo->query("SELECT COUNT(*) FROM funcionario")->fetchColumn();

  json_response([
    "ok" => true,
    "data" => [
      "sectorizaciones" => $k1,
      "sectores" => $k2,
      "funcionarios" => $k3
    ]
  ]);
} catch (Throwable $e) {
  error_log("KPIS ERROR: ".$e->getMessage());
  json_response(["ok" => false, "message" => "Error obteniendo KPIs."], 500);
}
