prepare(" INSERT INTO call_tracking ( external_call_id, tracking_platform, caller_number, tracking_number, call_start_time, call_end_time, call_duration_seconds, call_status, answered_by, traffic_source, campaign_name, recording_url, was_ai_rescue, attributed_revenue, caller_name, caller_city, caller_state ) VALUES ( :external_call_id, :tracking_platform, :caller_number, :tracking_number, :call_start_time, :call_end_time, :call_duration_seconds, :call_status, :answered_by, :traffic_source, :campaign_name, :recording_url, :was_ai_rescue, :attributed_revenue, :caller_name, :caller_city, :caller_state ) "); $stmt->execute([ ':external_call_id' => $data['external_call_id'], ':tracking_platform' => $data['tracking_platform'] ?? null, ':caller_number' => $data['caller_number'] ?? null, ':tracking_number' => $data['tracking_number'] ?? null, ':call_start_time' => $data['call_start_time'], ':call_end_time' => $data['call_end_time'] ?? null, ':call_duration_seconds' => $data['call_duration_seconds'] ?? null, ':call_status' => $data['call_status'] ?? null, ':answered_by' => $data['answered_by'] ?? null, ':traffic_source' => $data['traffic_source'] ?? null, ':campaign_name' => $data['campaign_name'] ?? null, ':recording_url' => $data['recording_url'] ?? null, ':was_ai_rescue' => $data['was_ai_rescue'] ?? 0, ':attributed_revenue' => $data['attributed_revenue'] ?? null, ':caller_name' => $data['caller_name'] ?? null, ':caller_city' => $data['caller_city'] ?? null, ':caller_state' => $data['caller_state'] ?? null, ]); header('Content-Type: application/json'); echo json_encode(['success' => true, 'message' => 'Call tracked successfully.', 'id' => $pdo->lastInsertId()]); } catch (PDOException $e) { if ($e->errorInfo[1] == 1062) { // Duplicate entry log_and_exit(409, "Conflict: A call with the same external_call_id already exists."); } else { log_and_exit(500, "Database error: " . $e->getMessage()); } } } else { log_and_exit(405, "Method Not Allowed"); }