const fs = require('fs');
const path = require('path');

const module_exports = {
    name: "replace_speaker",
    type: "processor",
    displayname: "Speaker Name Replacer",
    description: "Replaces speaker placeholder names with actual names based on a mapping in HTML files",

    async function(speakerMapping) {
        // Relativ von dieser Datei aus
        const documentsDir = path.resolve(__dirname, '../../../storage/documents');
        const inputHtmlPath = await this.getNewestFile(documentsDir, '.html');
        
        if (!inputHtmlPath) {
            throw new Error(`No HTML files found in ${documentsDir}`);
        }
        
        return await this.replaceNames(inputHtmlPath, speakerMapping);
    },

    getNewestFile: async function(dirPath, extension) {
        try {
            const files = await fs.promises.readdir(dirPath);
            const filtered = files.filter(f => f.endsWith(extension));
            
            if (filtered.length === 0) return null;
            
            const filesWithStats = await Promise.all(
                filtered.map(async (f) => {
                    const fullPath = path.join(dirPath, f);
                    const stats = await fs.promises.stat(fullPath);
                    return { path: fullPath, time: stats.mtimeMs };
                })
            );
            
            return filesWithStats.reduce((newest, curr) => 
                curr.time > newest.time ? curr : newest
            ).path;
        } catch (error) {
            console.error("Error reading directory:", error);
            throw error;
        }
    },

    replaceNames: async function(inputHtmlPath, speakerMapping) {
        try {
            const htmlContent = await fs.promises.readFile(inputHtmlPath, "utf-8");

            let outputContent = htmlContent;
            Object.entries(speakerMapping).forEach(([placeholder, value]) => {
                // Extract name if value is an object
                const displayName = typeof value === 'string' ? value : value.name;
                
                // Normalize placeholder for matching (remove case sensitivity)
                const normalizedPlaceholder = placeholder.toLowerCase();
                
                // Replace all variations: speakerA, SpeakerA, SPEAKERA, speaker_a, Speaker A, etc.
                // Matches with optional spaces, underscores, and parentheses
                const regex = new RegExp(
                    `\\b[Ss]peaker\\s*[_-]?\\s*${placeholder.charAt(placeholder.length - 1)}\\b|\\b${placeholder}\\b`,
                    'gi'
                );
                
                outputContent = outputContent.replace(regex, displayName);
            });

            await fs.promises.writeFile(inputHtmlPath, outputContent, "utf-8");

            return inputHtmlPath;

        } catch (error) {
            console.error("Error replacing speaker names:", error);
            throw error;
        }
    }
};

module.exports = module_exports;