<?php
require_once __DIR__ . '/../../cors.php'; // Enable CORS for React Native
// GET /api/admin/stats — dashboard summary stats
require_once __DIR__ . '/../../../config/database.php';
require_once __DIR__ . '/../../../middleware/auth.php';

requireAdmin();

if ($_SERVER['REQUEST_METHOD'] !== 'GET') jsonError('Method not allowed', 405);

$db = getDB();

$totalOrders     = (int)$db->query('SELECT COUNT(*) FROM orders')->fetchColumn();
$totalCustomers  = (int)$db->query("SELECT COUNT(*) FROM users WHERE role = 'customer'")->fetchColumn();
$totalProducts   = (int)$db->query('SELECT COUNT(*) FROM products')->fetchColumn();
$pendingOrders   = (int)$db->query("SELECT COUNT(*) FROM orders WHERE status = 'pending'")->fetchColumn();
$revenue         = (float)$db->query('SELECT COALESCE(SUM(total), 0) FROM orders')->fetchColumn();
$unreadMessages  = (int)$db->query('SELECT COUNT(*) FROM messages WHERE is_read = 0')->fetchColumn();

jsonResponse([
    'totalOrders'       => $totalOrders,
    'totalCustomers'    => $totalCustomers,
    'totalProducts'     => $totalProducts,
    'pendingOrders'     => $pendingOrders,
    'estimatedRevenue'  => $revenue,
    'unreadMessages'    => $unreadMessages,
]);
