<?php
require_once __DIR__ . '/../lib.php';

$user = hw_get_auth_user();
if (!$user) {
    hw_json(['success' => false, 'message' => 'Unauthorized'], 401);
}

$id = (int)($_GET['id'] ?? 0);
if (!$id) {
    hw_json(['success' => false, 'message' => 'Appointment ID required'], 400);
}

try {
    $pdo = hw_db();
    $stmt = $pdo->prepare("
        SELECT a.*, 
               CONCAT(u.first_name, ' ', u.last_name) as user_name,
               u.email as user_email,
               c.display_name as coach_name
        FROM appointments a
        LEFT JOIN users u ON a.user_id = u.id
        LEFT JOIN coaches c ON a.coach_id = c.id
        WHERE a.id = ?
    ");
    $stmt->execute([$id]);
    $appointment = $stmt->fetch();

    if (!$appointment) {
        hw_json(['success' => false, 'message' => 'Appointment not found'], 404);
    }

    hw_json(['success' => true, 'appointment' => $appointment]);
} catch (Exception $e) {
    hw_json(['success' => false, 'message' => 'Server error'], 500);
}
?>