// Send a chat message AS the mitra of the given session via the dev-only
// /internal/_test/send-chat-message-as-mitra endpoint. Goes through the
// real chat.service.sendMessage code path. Returns which transport
// carried the message — `websocket` if the customer's WS was alive at
// dispatch time, `fcm` otherwise.
//
// Env:
//   SESSION_ID            (required)
//   CONTENT               (required, message body)
//   BACKEND_INTERNAL_URL  (optional, default http://localhost:3001)
const sessionId = SESSION_ID
const content = CONTENT
const url = BACKEND_INTERNAL_URL || 'http://localhost:3001'
if (!sessionId) throw new Error('SESSION_ID env not set')
if (!content) throw new Error('CONTENT env not set')

const resp = http.post(`${url}/internal/_test/send-chat-message-as-mitra`, {
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ session_id: sessionId, content }),
})
if (resp.status !== 200) {
  throw new Error(`send-chat-message-as-mitra failed (${resp.status}): ${resp.body}`)
}
const data = json(resp.body)
output.MESSAGE_ID = data.message_id
output.DELIVERED_VIA = data.delivered_via
