
<?php
require_once __DIR__ . '/../../config/webrtc_config.php';
webrtc_require_login();
if (!webrtc_is_csr()) webrtc_json(['ok'=>false,'error'=>'CSR access only'],403);
$db = webrtc_db(); ensureWebRtcCallWorkflow($db);
$room = trim((string)($_POST['room_token'] ?? ''));
$notes = trim((string)($_POST['notes'] ?? ''));
if ($room === '' || $notes === '') webrtc_json(['ok'=>false,'error'=>'Missing room/notes'],400);
$stmt = $db->prepare('SELECT id, request_id, customer_id, customer_email, accepted_by FROM call_requests WHERE room_token=:room LIMIT 1');
$stmt->execute([':room'=>$room]);
$call = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$call) webrtc_json(['ok'=>false,'error'=>'Call not found'],404);
$db->prepare('INSERT INTO call_logs (call_request_id, request_id, customer_id, customer_email, csr_id, result, notes, created_at) VALUES (:call_id,:request_id,:customer_id,:email,:csr_id,"ended",:notes,NOW())')
   ->execute([':call_id'=>(int)$call['id'], ':request_id'=>$call['request_id'] ?: null, ':customer_id'=>$call['customer_id'] ?: null, ':email'=>$call['customer_email'] ?: null, ':csr_id'=>(int)$_SESSION['csr_id'], ':notes'=>$notes]);
webrtc_json(['ok'=>true]);
