const express = require('express');
const router  = express.Router();
const axios   = require('axios');
const { synthesizeWithGoogleTTS, isGoogleTTSConfigured } = require('../utils/ttsGoogle');
const { authMiddleware } = require('../middleware/auth');
var logger = require('../utils/logger');
var { gatewayUrl } = require('../utils/errors');

// Provider priority (auto-detect):
// TTS_PROVIDER=google     → Google Cloud TTS direct (HIPAA eligible)
// TTS_PROVIDER=litellm    → LiteLLM proxy
// TTS_PROVIDER=elevenlabs → ElevenLabs (not HIPAA)
// Auto: google > litellm > elevenlabs
function getTTSProvider() {
  var env = process.env.TTS_PROVIDER;
  if (env === 'google')     return 'google';
  if (env === 'litellm')    return 'litellm';
  if (env === 'elevenlabs') return 'elevenlabs';
  // Auto-detect: litellm > google > elevenlabs
  // LiteLLM Vertex TTS works correctly via alias (tts-1)
  if (process.env.LITELLM_API_BASE)   return 'litellm';
  if (isGoogleTTSConfigured())        return 'google';
  if (process.env.ELEVENLABS_API_KEY) return 'elevenlabs';
  return 'none';
}

var ttsProvider = getTTSProvider();
console.log('🔊 TTS provider:', ttsProvider +
  (ttsProvider === 'google' ? ' (voice: ' + (process.env.GOOGLE_TTS_VOICE || 'en-US-Journey-F') + ')' : ''));

router.post('/text-to-speech', authMiddleware, async (req, res) => {
  try {
    var text = (req.body.text || '').substring(0, 5000);
    if (!text) return res.status(400).json({ error: 'No text provided' });

    // Get user's preferred TTS voice (if set), then fall back to DB admin default, then env
    var db = require('../db/database');
    var userPrefs = await db.get('SELECT tts_voice FROM users WHERE id = ?', [req.user.id]);
    var userVoice = userPrefs?.tts_voice;
    var adminVoice = await db.getSetting('tts.voice') || '';
    var adminModel = await db.getSetting('tts.model') || '';
    // tts.voice may contain a model ID (from discovery "Set" button) — detect and use as model
    if (!adminModel && adminVoice && (adminVoice.indexOf('/') !== -1 || /^(openai-tts|tts-1|elevenlabs|vertex-gemini.*tts|openai-gpt.*tts)/i.test(adminVoice))) { adminModel = adminVoice; adminVoice = ''; }

    // Auto-detect model from voice — ensures model matches the voice's provider
    var VERTEX_VOICES = ['Puck','Charon','Kore','Fenrir','Aoede','Leda','Orus','Zephyr'];
    var resolvedVoice = userVoice || adminVoice || '';
    if (resolvedVoice) {
      if (VERTEX_VOICES.indexOf(resolvedVoice) !== -1) adminModel = 'vertex-gemini-2.5-flash-tts';
      else if (resolvedVoice.length > 15 && /^[a-zA-Z0-9]+$/.test(resolvedVoice)) adminModel = 'elevenlabs-v3';
    }

    if (ttsProvider === 'google') {
      var voice = resolvedVoice || process.env.GOOGLE_TTS_VOICE || 'en-US-Journey-F';
      var buffer = await synthesizeWithGoogleTTS(text, voice);
      res.set('Content-Type', 'audio/mpeg');
      res.set('X-TTS-Provider', 'google-tts/' + voice);
      logger.audit(req.user.id, 'text_to_speech', 'TTS generated', req, { category: 'clinical' });
      return res.send(buffer);
    }

    if (ttsProvider === 'litellm') {
      if (!process.env.LITELLM_API_BASE) return res.status(400).json({ error: 'LITELLM_API_BASE not set.' });
      var rawModel   = adminModel || process.env.LITELLM_TTS_MODEL || 'tts-1';
      var ttsModel   = rawModel.indexOf('/') !== -1 || rawModel.indexOf('-tts-') !== -1 || rawModel.startsWith('openai-') || rawModel.startsWith('elevenlabs-') || rawModel.startsWith('gemini-') || rawModel.startsWith('vertex-') ? rawModel : 'openai/' + rawModel;
      var ttsVoice   = resolvedVoice || process.env.LITELLM_TTS_VOICE || 'alloy';
      var ttsHeaders = { 'Content-Type': 'application/json' };
      if (process.env.LITELLM_API_KEY) ttsHeaders['Authorization'] = 'Bearer ' + process.env.LITELLM_API_KEY;
      var ttsResp = await axios.post(gatewayUrl('/audio/speech'),
        { model: ttsModel, voice: ttsVoice, input: text },
        { headers: ttsHeaders, responseType: 'arraybuffer', timeout: 60000 }
      );
      res.set('Content-Type', 'audio/mpeg');
      res.set('X-TTS-Provider', 'litellm/' + ttsModel);
      logger.audit(req.user.id, 'text_to_speech', 'TTS generated', req, { category: 'clinical' });
      return res.send(Buffer.from(ttsResp.data));
    }

    if (ttsProvider === 'elevenlabs') {
      if (!process.env.ELEVENLABS_API_KEY) return res.status(400).json({ error: 'ElevenLabs not configured' });
      var resp = await axios({
        method: 'POST',
        url: 'https://api.elevenlabs.io/v1/text-to-speech/pNInz6obpgDQGcFmaJgB',
        headers: { 'xi-api-key': process.env.ELEVENLABS_API_KEY, 'Content-Type': 'application/json' },
        data: { text: text, model_id: 'eleven_turbo_v2_5', voice_settings: { stability: 0.5, similarity_boost: 0.75 } },
        responseType: 'arraybuffer'
      });
      res.set('Content-Type', 'audio/mpeg');
      res.set('X-TTS-Provider', 'elevenlabs');
      logger.audit(req.user.id, 'text_to_speech', 'TTS generated', req, { category: 'clinical' });
      return res.send(Buffer.from(resp.data));
    }

    res.status(400).json({ error: 'TTS not configured. Set GOOGLE_VERTEX_PROJECT, LITELLM_API_BASE, or ELEVENLABS_API_KEY.' });
  } catch (err) {
    var detail = err.response && err.response.data
      ? JSON.stringify(err.response.data).substring(0, 500)
      : err.message;
    console.error('[TTS] Error (' + ttsProvider + '):', detail);
    res.status(500).json({ error: 'TTS failed: ' + detail });
  }
});

module.exports = router;
