<?php
session_start();
require_once '../config.php';
if(!isset($_SESSION['doctor'])) { die('Unauthorized'); }

$appointment_id = (int)$_GET['id'];
$action = $_GET['action'];

if($action == 'approve') {
    // Generate meeting link
    $roomName = 'opd_' . $appointment_id . '_' . time();
    $meetingLink = 'https://meet.jit.si/' . $roomName;
    $conn->query("UPDATE opd_appointments SET status='confirmed', meeting_link='$meetingLink' WHERE id=$appointment_id");
    
    // Send SMS to patient
    $appt = $conn->query("SELECT a.*, p.name, p.mobile FROM opd_appointments a JOIN opd_patients p ON a.patient_id=p.id WHERE a.id=$appointment_id")->fetch_assoc();
    if($appt) {
        $msg = "আপনার OPD অ্যাপয়েন্টমেন্ট অনুমোদিত হয়েছে।\nতারিখ: ".date('d/m/Y', strtotime($appt['appointment_date']))."\nভিডিও কল লিংক: $meetingLink";
        sendSMS($appt['mobile'], $msg);
    }
    $msg = "✅ Approved & meeting link sent.";
}
elseif($action == 'reject') {
    $conn->query("UPDATE opd_appointments SET status='cancelled' WHERE id=$appointment_id");
    $msg = "❌ Appointment rejected.";
}

header('Location: dashboard.php?msg='.urlencode($msg));
exit;
?>