// services/pipeline/jobs/transcribeLatest.ts
import path from 'path';
import fs from 'fs';
// @ts-ignore: module has no type declarations or cannot be resolved in current TS config
import assembly from '../../modules/transcription/assembly';

/**
 * Finds the most recently modified .wav file in storage/audio/
 */
function getLatestWav(): string {
  const audioDir = path.join(process.cwd(), 'storage', 'audio');
  const files = fs.readdirSync(audioDir).filter(f => f.toLowerCase().endsWith('.wav'));
  if (files.length === 0) throw new Error('⚠️ No .wav file found in storage/audio');

  const newest = files
    .map(f => ({ f, t: fs.statSync(path.join(audioDir, f)).mtimeMs }))
    .sort((a, b) => b.t - a.t)[0].f;

  return path.join(audioDir, newest);
}

/**
 * Full transcription pipeline according to the defined workflow:
 * 1. Audio Upload → AssemblyAI
 * 2. Job Creation (transcript_id)
 * 3. Polling Status (queued → processing → completed)
 * 4. Download Transcript JSON
 * 5. Storage: /transcripts/{session_id}.json
 */
async function main() {
  const audioPath = getLatestWav();

  console.log('1️⃣ Audio Upload → AssemblyAI');
  console.log('   Source:', audioPath);

  console.log('2️⃣ Job Creation (transcript_id)');
  console.log('3️⃣ Polling Status (queued → processing → completed)');
  console.log('4️⃣ Download Transcript JSON');
  console.log('5️⃣ Storage: /transcripts/{session_id}.json');

  // Execute the transcription process via the AssemblyAI module
  const result = await assembly.run(audioPath);

  console.log('✅ Transcription completed successfully');
  console.log('🆔 Transcript ID:', result.id);
  console.log('📁 Transcript file saved under: storage/transcripts/');
}

// Entry point
main().catch((err) => {
  console.error('❌ Transcription pipeline failed:', err.message || err);
  process.exit(1);
});
