/**
 * GET /api/telephony/recordings/transcribe-status?file=…
 * Poll target for a transcription started via `transcribe` (202 pending):
 * 200 + transcript when the cache file exists, 202 while the job runs,
 * 422 when whisper heard nothing, 502 when the job failed, 404 when nothing
 * was ever started. Reads the disk state, so it works on either PM2 instance.
 */
import { gateFromEvent } from '../../../utils/chatAccess'
import { transcriptionStatus, validRecordingName } from '../../../utils/telephony/recordings'
import { statusResponse } from '../../../utils/telephony/transcriptionResponse'

export default defineEventHandler(async (event) => {
  try { await gateFromEvent(event) } catch (err: any) { return { status: err?.statusCode || 403, message: 'Forbidden' } }
  const file = validRecordingName(getQuery(event)?.file)
  if (!file) return { status: 400, message: 'Invalid recording name' }
  return statusResponse(file, await transcriptionStatus(file))
})
