#!/usr/bin/env node

/**
 * Final CSS Polish - Clean up remaining syntax issues
 */

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

const CSS_DIR = './assets/css';

const cssFiles = [
    'hvac-common.css',
    'hvac-dashboard.css',
    'hvac-registration.css',
    'hvac-event-summary.css',
    'hvac-email-attendees.css'
];

function polishCSS(content) {
    // Remove semicolons after comments
    content = content.replace(/\*\/\s*;/g, '*/');
    
    // Fix duplicate property values on same line
    content = content.replace(/([a-z-]+):\s*([^;]+);\s*\1:\s*([^;]+);\s*\/\*\s*IE fallback\s*\*\//g, '$1: $2; /* IE fallback */');
    
    // Fix property values with duplicate values
    content = content.replace(/([a-z-]+):\s*([^;]+);\s*([a-z-]+):\s*\2;\s*\/\*\s*IE fallback\s*\*\//g, '$1: $2; /* IE fallback */');
    
    // Clean up duplicate transform properties
    content = content.replace(/text-transform:\s*uppercase;\s*text-transform:\s*uppercase;/g, 'text-transform: uppercase;');
    
    // Fix spacing issues
    content = content.replace(/;\s*}/g, ';\n}');
    content = content.replace(/{\s*}/g, '{ }');
    
    // Remove empty lines within :root
    content = content.replace(/(:root\s*{[^}]+)}/, (match, rootContent) => {
        const cleaned = rootContent.replace(/\n\s*\n/g, '\n');
        return cleaned + '}';
    });
    
    // Fix malformed selectors
    content = content.replace(/\.filter-group;\s*input/g, '.filter-group input');
    
    // Ensure proper closing braces
    content = content.replace(/}\s*}\s*}\s*}\s*}/g, '}');
    
    // Clean up excessive closing braces at end
    content = content.replace(/}\s*$/, '}');
    content = content.replace(/(}\s*){5,}$/g, '}');
    
    return content;
}

function main() {
    console.log('HVAC Community Events - Final CSS Polish');
    console.log('='.repeat(60));
    
    let totalPolished = 0;
    
    for (const file of cssFiles) {
        const filePath = path.join(CSS_DIR, file);
        
        if (!fs.existsSync(filePath)) {
            console.log(`⚠️  File not found: ${file}`);
            continue;
        }
        
        try {
            console.log(`\n🔧 Polishing: ${file}`);
            
            // Read content
            let content = fs.readFileSync(filePath, 'utf8');
            const originalLength = content.length;
            
            // Polish the CSS
            content = polishCSS(content);
            
            // Write back
            fs.writeFileSync(filePath, content, 'utf8');
            
            const newLength = content.length;
            const diff = originalLength - newLength;
            
            console.log(`  ✅ Polished (${diff} chars removed)`);
            totalPolished++;
            
        } catch (error) {
            console.error(`  ❌ Error: ${error.message}`);
        }
    }
    
    console.log('\n' + '='.repeat(60));
    console.log(`✅ Successfully polished: ${totalPolished} files`);
}

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