/**
 * Whisper API routes
 * Control the local GPU-accelerated speech-to-text server
 */

import {
  startWhisperServer,
  stopWhisperServer,
  toggleWhisperServer,
  getWhisperState,
  getWhisperPort
} from '../services/whisper'

export async function handleWhisperRoutes(req: Request): Promise<Response | null> {
  const url = new URL(req.url)
  const path = url.pathname

  // GET /api/whisper/status - Get current state
  if (path === '/api/whisper/status' && req.method === 'GET') {
    const state = await getWhisperState()
    return Response.json(state)
  }

  // POST /api/whisper/start - Start Whisper server
  if (path === '/api/whisper/start' && req.method === 'POST') {
    const success = await startWhisperServer()
    const state = await getWhisperState()
    return Response.json({
      success,
      ...state,
      message: success ? 'Whisper server started' : 'Failed to start Whisper server'
    })
  }

  // POST /api/whisper/stop - Stop Whisper server
  if (path === '/api/whisper/stop' && req.method === 'POST') {
    const success = stopWhisperServer()
    const state = await getWhisperState()
    return Response.json({
      success,
      ...state,
      message: success ? 'Whisper server stopped' : 'Failed to stop Whisper server'
    })
  }

  // POST /api/whisper/toggle - Toggle Whisper on/off
  if (path === '/api/whisper/toggle' && req.method === 'POST') {
    const result = await toggleWhisperServer()
    const state = await getWhisperState()
    return Response.json({
      ...result,
      ...state,
      message: state.running ? 'Whisper GPU running' : 'Whisper GPU starting...'
    })
  }

  // GET /api/whisper/port - Get Whisper WebSocket port
  if (path === '/api/whisper/port' && req.method === 'GET') {
    return Response.json({
      port: getWhisperPort(),
      url: `ws://localhost:${getWhisperPort()}`
    })
  }

  return null
}
