// Ensure ffmpeg binary is available
if (!ffmpegPath) {
    throw new Error('FFmpeg binary not found!');
}
ffmpeg.setFfmpegPath(ffmpegPath);

// Prepare output directory (always storage/audio under project root)
const outputDir = `${__dirname}/../../../storage/audio`;
if (!fs.existsSync(outputDir)) {
    fs.mkdirSync(outputDir, { recursive: true });
}



module.exports = {
    name:"extraction-video-to-audio", // Unique name for our function that will later be used to get the function from the map via "mapFunctions.get("example").function()"
    type:"extractor", // value used to differentiate each module to order them in the UI
    displayname:"Default extractor", // The displayname used within the UI
    async function(parameter){
        /*
        parameter structure:
        {
            inputVideoPath: String, // Path to the file
            outputType: String      // Audio file output format
        }
        */
        try {
            return new Promise((resolve, reject) => {
                this.extractAudioFromVideo(parameter.inputVideoPath, parameter.outputType)
                    .then((resp) => resolve(resp))
                    .catch((err) => {reject(err)});
            })
        } catch (error) {
            console.log(parameter.outputType);
            
        }

    },
    // Derive input and output paths
    // const inputVideoPath = process.argv[2];
    // console.log(process.argv);

    /**
     * Extracts audio from a video using ffmpeg.
     * - Converts video to WAV (16 kHz, Mono, PCM optional if needed)
     * - Shows CLI progress bar
     * - Handles errors gracefully (without errors)
     */
    extractAudioFromVideo: async function (videoFilePath, outputType){
        let inputVideoName = path.basename(videoFilePath, path.extname(videoFilePath));
        let outputAudioPath = path.join(outputDir, `${inputVideoName}.${outputType}`);

        return new Promise((resolve, reject) => {
            try {
                ffmpeg(videoFilePath)
                .outputFormat(outputType)
                // .audioCodec('pcm_s16le')
                .audioChannels(1)
                .audioFrequency(16000)
                .on('end', () => {
                    resolve(outputAudioPath);
                })
                .on('error', (err) => {
                    // console.error(`failed_audio_extraction on type ${outputType}: ${err.message}`);
                    reject(err);
                })
                .save(outputAudioPath);

            } catch (error) {
                // console.log(error);
            }
        });
    }
}