<?php
// api/send_consultation_link.php - Send email to patient with consultation link
header('Content-Type: application/json');
require_once '../include/config.php';

$data = json_decode(file_get_contents('php://input'), true);
$consult_id = $data['consult_id'] ?? 0;

if (!$consult_id) {
    echo json_encode(['success' => false, 'message' => 'Invalid consultation ID']);
    exit();
}

// Get consultation details
$query = "SELECT c.*, p.first_name, p.last_name, p.email, d.name as doctor_name 
          FROM consultations c 
          JOIN patients p ON c.patient_id = p.patient_id 
          JOIN doctors d ON c.doctor_id = d.id 
          WHERE c.id = $consult_id";
$result = $conn->query($query);
$consult = $result->fetch_assoc();

if (!$consult) {
    echo json_encode(['success' => false, 'message' => 'Consultation not found']);
    exit();
}

$patient_email = $consult['email'];
$patient_name = $consult['first_name'] . ' ' . $consult['last_name'];
$doctor_name = $consult['doctor_name'];
$meeting_link = $consult['meeting_link'];
$scheduled_time = date('F j, Y \a\t g:i A', strtotime($consult['scheduled_time']));

// Email content
$subject = "UHD Virtual Consultation with Dr. $doctor_name";
$message = "
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; }
        .container { max-width: 600px; margin: 0 auto; padding: 20px; }
        .header { background: #064e3b; color: white; padding: 20px; text-align: center; }
        .content { padding: 20px; background: #f9f9f9; }
        .button { background: #064e3b; color: white; padding: 12px 24px; text-decoration: none; border-radius: 25px; display: inline-block; }
        .footer { text-align: center; padding: 20px; font-size: 12px; color: #666; }
    </style>
</head>
<body>
    <div class='container'>
        <div class='header'>
            <h2>🏥 UHD Virtual Consultation</h2>
        </div>
        <div class='content'>
            <h3>Dear $patient_name,</h3>
            <p>Your virtual consultation with <strong>Dr. $doctor_name</strong> has been scheduled for:</p>
            <p style='font-size: 18px; text-align: center;'><strong>$scheduled_time</strong></p>
            <p>Click the button below to join your consultation at the scheduled time:</p>
            <p style='text-align: center;'>
                <a href='$meeting_link' class='button'>Join Consultation</a>
            </p>
            <p><strong>Important Notes:</strong></p>
            <ul>
                <li>No download required - works in any browser</li>
                <li>Please ensure you have a stable internet connection</li>
                <li>Allow camera and microphone access when prompted</li>
                <li>Join a few minutes before your scheduled time</li>
            </ul>
            <p>If you have any issues, please contact the hospital at support@uhd.com</p>
        </div>
        <div class='footer'>
            <p>© " . date('Y') . " University Hospital Dashboard. All rights reserved.</p>
        </div>
    </div>
</body>
</html>
";

// Send email (configure your mail server)
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: UHD Virtual Care <noreply@uhd.com>" . "\r\n";

if (mail($patient_email, $subject, $message, $headers)) {
    echo json_encode(['success' => true, 'message' => 'Email sent successfully']);
} else {
    // Log error but don't fail - you can still share the link manually
    echo json_encode(['success' => true, 'message' => 'Consultation ready. Share link manually: ' . $meeting_link]);
}
?>