<?php
require_once __DIR__.'/../../includes/bootstrap.php'; require_login();
if(session_status()===PHP_SESSION_NONE) session_start();
require_once __DIR__.'/lib.php'; header('Content-Type: application/json; charset=utf-8');
try { if($_SERVER['REQUEST_METHOD']!=='POST') throw new RuntimeException('POST required.'); $orderId=(int)($_POST['order_id']??0); if($orderId<=0) throw new RuntimeException('Invalid booking.'); $booking=comm_booking($pdo,$orderId); $token=comm_issue_outlook_token($pdo,$orderId); comm_log($pdo,$orderId,'outlook','outlook_handoff_created',['status'=>'pending']); echo json_encode(['ok'=>true,'token'=>$token,'booking'=>$booking,'expires_minutes'=>10],JSON_UNESCAPED_SLASHES); }
catch(Throwable $e){http_response_code(400);echo json_encode(['ok'=>false,'message'=>$e->getMessage()]);}