#!/usr/bin/env node

/**
 * Fix CSS Brace Issues - Balance opening and closing braces
 */

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

const CSS_DIR = './assets/css';

function fixBraces(filePath) {
    try {
        let content = fs.readFileSync(filePath, 'utf8');
        const fileName = path.basename(filePath);
        
        console.log(`\n🔧 Fixing braces: ${fileName}`);
        
        // Fix specific patterns that broke selectors
        
        // 1. Fix tbody; } pattern
        content = content.replace(/tbody;\s*\}\s*\n\s*\.tr:hover/g, 'tbody tr:hover');
        
        // 2. Fix malformed pseudo-selectors 
        content = content.replace(/\.hvac-modal-close:\s*focus/g, '.hvac-modal-close:focus');
        content = content.replace(/button:\s*focus/g, 'button:focus');
        content = content.replace(/\.button:\s*focus/g, '.button:focus');
        content = content.replace(/a:\s*focus/g, 'a:focus');
        content = content.replace(/a:\s*hover/g, 'a:hover');
        content = content.replace(/input:\s*focus/g, 'input:focus');
        content = content.replace(/textarea:\s*focus/g, 'textarea:focus');
        content = content.replace(/select:\s*focus/g, 'select:focus');
        content = content.replace(/:\s*hover/g, ':hover');
        content = content.replace(/:\s*focus/g, ':focus');
        content = content.replace(/:\s*active/g, ':active');
        content = content.replace(/:\s*:after/g, '::after');
        content = content.replace(/:\s*:before/g, '::before');
        
        // 3. Fix .hvac-login-link; } pattern
        content = content.replace(/\.hvac-login-link;\s*\}\s*\n\s*\.a:hover/g, '.hvac-login-link a:hover');
        
        // 4. Fix .hvac-event-summary-attendees tbody; } pattern
        content = content.replace(/\.hvac-transactions-table tbody;\s*\}\s*\n\s*\.tr:hover/g, '.hvac-transactions-table tbody tr:hover');
        
        // 5. Fix column-actions; } pattern
        content = content.replace(/\.column-actions;\s*\}\s*\n\s*\.a:hover/g, '.column-actions a:hover');
        
        // 6. Fix hvac-content; pattern
        content = content.replace(/\.hvac-content;\s*\n\s*a:\s*not/g, '.hvac-content a:not');
        content = content.replace(/\.hvac-content;\s*\n\s*a:focus/g, '.hvac-content a:focus');
        
        // 7. Fix @supports patterns
        content = content.replace(/@supports\s*\(;\s*\n\s*display:\s*flex\)/g, '@supports (display: flex)');
        content = content.replace(/@supports\s*\(;\s*\n\s*display:\s*grid\)/g, '@supports (display: grid)');
        content = content.replace(/@supports\s*not\s*\(;\s*\n\s*display:\s*flex\)/g, '@supports not (display: flex)');
        content = content.replace(/@supports\s*not\s*\(;\s*\n\s*display:\s*grid\)/g, '@supports not (display: grid)');
        
        // 8. Fix .js-focus-visible pattern
        content = content.replace(/\.js-focus-visible\s*:;\s*\n\s*focus:\s*not/g, '.js-focus-visible :focus:not');
        
        // 9. Fix remaining pseudo-class spaces
        content = content.replace(/([a-zA-Z\-_]+)\s+:\s*([a-z]+)/g, '$1:$2');
        
        // 10. Remove lone closing braces
        content = content.replace(/^\s*\}\s*$/gm, '');
        
        // Clean up extra newlines
        content = content.replace(/\n{3,}/g, '\n\n');
        
        // Write the fixed content
        fs.writeFileSync(filePath, content, 'utf8');
        
        // Check if braces are now balanced
        const openBraces = (content.match(/\{/g) || []).length;
        const closeBraces = (content.match(/\}/g) || []).length;
        
        console.log(`  ✅ Fixed - Braces: ${openBraces} open, ${closeBraces} close`);
        
        return openBraces === closeBraces;
        
    } catch (error) {
        console.error(`Error fixing ${filePath}:`, error.message);
        return false;
    }
}

function main() {
    console.log('HVAC Community Events - Fix CSS Braces');
    console.log('='.repeat(50));
    
    if (!fs.existsSync(CSS_DIR)) {
        console.error(`CSS directory not found: ${CSS_DIR}`);
        process.exit(1);
    }
    
    const targetFiles = [
        'hvac-common.css',
        'hvac-dashboard.css', 
        'hvac-registration.css',
        'hvac-event-summary.css',
        'hvac-email-attendees.css',
        'hvac-certificates.css',
        'hvac-attendee-profile.css'
    ].map(file => path.join(CSS_DIR, file));
    
    const existingFiles = targetFiles.filter(filePath => fs.existsSync(filePath));
    
    let fixedCount = 0;
    existingFiles.forEach(filePath => {
        if (fixBraces(filePath)) {
            fixedCount++;
        }
    });
    
    console.log('\n' + '='.repeat(50));
    console.log(`✅ Fixed ${fixedCount}/${existingFiles.length} files`);
}

if (require.main === module) {
    main();
}

module.exports = { fixBraces };