<?php
/**
 * GET /api/admin/overview.php
 * Admin-only. Platform-wide counts for the dashboard's landing tab.
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/response.php';
require_once __DIR__ . '/../includes/auth.php';

require_login();
require_role(['admin']);

$pdo = Database::connect();

$counts = [];
$counts['personal_users']  = (int) $pdo->query("SELECT COUNT(*) FROM users WHERE account_type = 'personal'")->fetchColumn();
$counts['business_users']  = (int) $pdo->query("SELECT COUNT(*) FROM users WHERE account_type = 'business'")->fetchColumn();
$counts['suspended_users'] = (int) $pdo->query("SELECT COUNT(*) FROM users WHERE status = 'suspended'")->fetchColumn();
$counts['active_listings'] = (int) $pdo->query("SELECT COUNT(*) FROM products WHERE status = 'active'")->fetchColumn();
$counts['total_listings']  = (int) $pdo->query('SELECT COUNT(*) FROM products')->fetchColumn();
$counts['total_orders']    = (int) $pdo->query('SELECT COUNT(*) FROM orders')->fetchColumn();
$counts['open_reports']    = (int) $pdo->query("SELECT COUNT(*) FROM reports WHERE status = 'open'")->fetchColumn();

send_json(true, 'OK', ['counts' => $counts]);
