// ============================================================
// TRANSCRIBE GOOGLE — Vertex AI / Gemini audio transcription
// Sends audio inline to Gemini which returns spoken text.
// Supports: gemini-2.0-flash (default), gemini-2.5-flash
// Requires: GOOGLE_VERTEX_PROJECT, @google-cloud/vertexai installed
// ============================================================

async function transcribeWithGemini(buffer, mimeType, modelName) {
  var VertexModule = require('@google-cloud/vertexai');
  var project  = process.env.GOOGLE_VERTEX_PROJECT;
  var location = process.env.GOOGLE_VERTEX_LOCATION || 'us-central1';
  if (!project) throw new Error('GOOGLE_VERTEX_PROJECT not set');

  var vertex = new VertexModule.VertexAI({ project: project, location: location });
  var model  = vertex.getGenerativeModel({
    model: modelName || process.env.GOOGLE_STT_MODEL || 'gemini-2.0-flash'
  });

  var base64   = buffer.toString('base64');
  var safeMime = mimeType || 'audio/webm';

  var result = await model.generateContent({
    contents: [{
      role: 'user',
      parts: [
        { inlineData: { mimeType: safeMime, data: base64 } },
        { text: 'Transcribe this audio. Output the spoken words only, exactly as heard. No commentary, no formatting, no explanation.' }
      ]
    }]
  });

  var text = '';
  if (result.response && result.response.candidates && result.response.candidates[0]) {
    var parts = result.response.candidates[0].content.parts || [];
    parts.forEach(function(p) { if (p.text) text += p.text; });
  }
  return text.trim();
}

function isGoogleSTTConfigured() {
  if (!process.env.GOOGLE_VERTEX_PROJECT) return false;
  try { require('@google-cloud/vertexai'); return true; } catch(e) { return false; }
}

module.exports = { transcribeWithGemini, isGoogleSTTConfigured };
