<?php
declare(strict_types=1);
require_once __DIR__ . '/../lib.php';

hw_require_method(['GET']);
hw_require_auth(['admin']);

try {
    $pdo = hw_db();

    // Analytics metrics
    $stmt = $pdo->query('SELECT COUNT(*) as count FROM appointments WHERE DATE(starts_at) = CURDATE()');
    $bookingsToday = (int)($stmt->fetch()['count'] ?? 0);

    $stmt = $pdo->query('SELECT COUNT(*) as count FROM orders WHERE status = "delivered"');
    $completedOrders = (int)($stmt->fetch()['count'] ?? 0);

    $stmt = $pdo->query('SELECT COUNT(*) as count FROM users WHERE status = "active"');
    $activeUsers = (int)($stmt->fetch()['count'] ?? 0);

    hw_json([
        'success' => true,
        'data' => [
            'bookings_today' => $bookingsToday,
            'completed_orders' => $completedOrders,
            'active_users' => $activeUsers,
        ]
    ]);
} catch (Throwable $e) {
    hw_json(['success' => false, 'message' => 'Server error', 'error' => $e->getMessage()], 500);
}
