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

const rootDir = 'C:\\Users\\Marcos\\.gemini\\antigravity\\scratch\\gpi\\src';

const replacements = [
    { from: /clerkId/g, to: 'externalId' },
    { from: /clerkUserId/g, to: 'userId' },
    { from: /x-clerk-user-id/g, to: 'x-auth-user-id' },
    { from: /Clerk/g, to: 'Auth' }, // Use with caution, but mostly it's ClerkProvider or Clerk-related
    // Add more if needed
];

function walk(dir) {
    const files = fs.readdirSync(dir);
    for (const file of files) {
        const fullPath = path.join(dir, file);
        const stat = fs.statSync(fullPath);
        if (stat.isDirectory()) {
            walk(fullPath);
        } else if (file.endsWith('.ts') || file.endsWith('.tsx') || file.endsWith('.js') || file.endsWith('.html')) {
            processFile(fullPath);
        }
    }
}

function processFile(filePath) {
    let content = fs.readFileSync(filePath, 'utf8');
    let modified = false;

    for (const r of replacements) {
        if (r.from.test(content)) {
            content = content.replace(r.from, r.to);
            modified = true;
        }
    }

    if (modified) {
        console.log(`Updated: ${filePath}`);
        fs.writeFileSync(filePath, content, 'utf8');
    }
}

console.log('Starting global refactor...');
walk(rootDir);
// Also check index.html
processFile('C:\\Users\\Marcos\\.gemini\\antigravity\\scratch\\gpi\\index.html');
console.log('Refactor complete.');
