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

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

try {
    $pdo = hw_db();
    $limit = (int)($_GET['limit'] ?? 5);

    // Recent bookings
    $stmt = $pdo->prepare('
        SELECT 
            a.id,
            a.starts_at,
            a.status,
            u.first_name,
            u.last_name,
            c.display_name as coach_name
        FROM appointments a
        JOIN users u ON a.user_id = u.id
        JOIN coaches c ON a.coach_id = c.id
        ORDER BY a.starts_at DESC
        LIMIT ?
    ');
    $stmt->execute([$limit]);
    $bookings = $stmt->fetchAll();

    hw_json([
        'success' => true,
        'data' => $bookings
    ]);
} catch (Throwable $e) {
    hw_json(['success' => false, 'message' => 'Server error', 'error' => $e->getMessage()], 500);
}
