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

function countLines(filePath) {
    try {
        const content = fs.readFileSync(filePath, 'utf8');
        return content.split('\n').length;
    } catch (e) {
        return 0;
    }
}

function walk(dir, results = []) {
    const list = fs.readdirSync(dir);
    list.forEach(file => {
        const fullPath = path.join(dir, file);
        const stat = fs.statSync(fullPath);
        if (stat.isDirectory()) {
            if (!['node_modules', 'target', 'dist', '.git', '.gemini', '.brain'].includes(file) && !file.startsWith('.')) {
                walk(fullPath, results);
            }
        } else {
            const ext = path.extname(fullPath);
            if (['.rs', '.js', '.ts', '.html', '.css'].includes(ext)) {
                results.push({
                    path: path.relative(process.cwd(), fullPath),
                    loc: countLines(fullPath)
                });
            }
        }
    });
    return results;
}

const srcDir = process.cwd();
const files = walk(srcDir);
files.sort((a, b) => b.loc - a.loc);

console.log('--- Source Code Line Counts ---');
files.forEach(f => {
    if (f.loc === 0) return;
    const status = f.loc >= 600 ? '[REFACTOR REQUIRED]' : ' ';
    console.log(`${f.path.padEnd(60)} : ${f.loc.toString().padStart(5)} lines ${status}`);
});
