<?php
require __DIR__ . '/../bootstrap.php';

use Gestoreasy\Core\Tenant\ConnectionManager;

$tenant = $argv[1] ?? 'default';
putenv("TENANT_ID={$tenant}");

try {
    $pdo = ConnectionManager::forTenant();
    $stmt = $pdo->query('SELECT resource, COUNT(*) AS cnt FROM audit_logs GROUP BY resource ORDER BY cnt DESC LIMIT 50');
    $rows = $stmt->fetchAll();
    echo json_encode($rows, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
    echo json_encode(['error'=>$e->getMessage()]);
    exit(1);
}