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

const rootDir = 'd:\\gdfhbfgdbnbdfbdf\\typogenie\\src-tauri\\templates';

function walk(dir) {
    const files = fs.readdirSync(dir, { withFileTypes: true });
    files.forEach(dirent => {
        const fullPath = path.join(dir, dirent.name);
        if (dirent.isDirectory()) {
            walk(fullPath);
        } else if (dirent.name.endsWith('.json')) {
            let content = fs.readFileSync(fullPath, 'utf8');
            let changed = false;

            // Fix invalid "white" color
            if (content.includes('"color": "white"')) {
                content = content.replace(/"color": "white"/g, '"color": "background"');
                changed = true;
            }
            if (content.includes('"background": "white"')) {
                content = content.replace(/"background": "white"/g, '"background": "background"');
                changed = true;
            }

            // Fix border alignment from nested "border": { "bottom": ... } to "borderBottom": ...
            // This is a bit tricky with regex but let's try a simple one for the common cases
            const borderSides = ['top', 'bottom', 'left', 'right'];
            borderSides.forEach(side => {
                const searchStr = new RegExp(`"border":\\s*{\\s*"${side}":\\s*{([^}]*)}`, 'g');
                // Capture the content inside the side object
                if (searchStr.test(content)) {
                    content = content.replace(searchStr, (match, p1) => {
                        const capitalizedSide = side.charAt(0).toUpperCase() + side.slice(1);
                        return `"border${capitalizedSide}": {${p1}}`;
                    });
                    changed = true;
                }
            });

            if (changed) {
                fs.writeFileSync(fullPath, content, { encoding: 'utf8' });
                console.log(`Updated: ${fullPath}`);
            }
        }
    });
}

walk(rootDir);
console.log('Finished mass-correction of templates.');
