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;

            try {
                const json = JSON.parse(content);
                if (json.page && json.page.columns !== 1) {
                    json.page.columns = 1;
                    // Also remove gutter if it exists
                    if (json.page.gutter) delete json.page.gutter;
                    content = JSON.stringify(json, null, 4);
                    changed = true;
                }
            } catch (e) {
                console.error(`Error parsing ${fullPath}: ${e.message}`);
                // Fallback to regex if JSON is slightly broken (though surgery should have fixed it)
                if (content.includes('"columns":')) {
                    content = content.replace(/"columns":\s*\d+/g, '"columns": 1');
                    changed = true;
                }
            }

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

walk(rootDir);
console.log('Finished removing columns from all templates.');
