let API_KEY
const BASE_URL = 'https://api.assemblyai.com/v2';

//---------------------------------------------------Upload audio---------------------------------------------------

async function uploadAudio(audioPath) {
  const audioData = fs.readFileSync(audioPath);

  const response = await axios.post(`${BASE_URL}/upload`, audioData, {
    headers: {
      authorization: API_KEY,
      'content-type': 'application/octet-stream'
    }
  });

  return response.data.upload_url;
}

////---------------------------------------------------Extract session id---------------------------------------------------

function getSessionId(inputPath) {
  try {
    const parsed = new URL(inputPath);
    const base = path.basename(parsed.pathname);
    return base.replace(/\.[^.]+$/, '');
  } catch {
    return path.basename(inputPath, path.extname(inputPath));
  }
}

//---------------------------------------------------Create transcript---------------------------------------------------

async function createTranscript(audioUrl) {
  const response = await axios.post(
    `${BASE_URL}/transcript`,
    {
      audio_url: audioUrl,
      speaker_labels: true,
      language_detection: true
    },
    {
      headers: {
        authorization: API_KEY,
        'content-type': 'application/json'
      }
    }
  );

  return response.data.id;
}

//---------------------------------------------------Poll transcript---------------------------------------------------

async function pollTranscript(transcriptId) {
  while (true) {
    const response = await axios.get(`${BASE_URL}/transcript/${transcriptId}`, {
      headers: { authorization: API_KEY }
    });

    const status = response.data.status;

    if (status === 'completed') return response.data;
    if (status === 'error') throw new Error(`Transcription failed: ${response.data.error}`);

    await new Promise(res => setTimeout(res, 3000));
  }
}

//---------------------------------------------------Save transcript---------------------------------------------------

function saveTranscript(transcript, sessionId) {
  const outputDir = path.join(__dirname, '../../../storage/transcripts');

  if (!fs.existsSync(outputDir)) {
    fs.mkdirSync(outputDir, { recursive: true });
  }

  const outputPath = path.join(outputDir, `${sessionId}.json`);
  fs.writeFileSync(outputPath, JSON.stringify(transcript, null, 2));

  // console.log(`Transcript saved: ${outputPath}`);

  return outputPath;
}

//---------------------------------------------------Modul---------------------------------------------------

module.exports = {
  name: 'assembly',
  type: 'transcription',
  displayname: 'AssemblyAI',
  audioformat: "mp3",

  async function(audioFileName) {
    API_KEY = process.env.ASSEMBLYAI_API_KEY;
    return new Promise(async (resolve, reject) => {
      try {
        // audioFileName ist nur "datei.mp3"
        const audioPath = audioFileName;

        let audioUrl;

        if (/^https?:\/\//i.test(audioFileName)) {
          audioUrl = audioFileName;
        } else {
          if (!fs.existsSync(audioPath)) {
            throw new Error(`Audio file not found: ${audioPath}`);
          }
          audioUrl = await uploadAudio(audioPath);
        }

        const transcriptId = await createTranscript(audioUrl);
        const transcript = await pollTranscript(transcriptId);

        const sessionId = getSessionId(audioFileName);
        
        resolve(saveTranscript(transcript, sessionId));

      } catch (error) {
        // console.error('Transcription error:', error.message);
        reject(error);
        return
      }
    })
  }
};
