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

function fixCSSProperly(filePath) {
    console.log(`Fixing CSS in ${filePath}...`);
    
    let css = fs.readFileSync(filePath, 'utf8');
    
    // First, remove all the excessive closing braces that are on their own lines
    css = css.replace(/^\s*}\s*$/gm, '');
    
    // Now rebuild the CSS with proper structure
    let lines = css.split('\n');
    let output = [];
    let inRule = false;
    let inComment = false;
    let currentRule = [];
    let braceDepth = 0;
    
    for (let i = 0; i < lines.length; i++) {
        let line = lines[i];
        let trimmed = line.trim();
        
        // Track comments
        if (trimmed.includes('/*') && !trimmed.includes('*/')) {
            inComment = true;
        }
        if (trimmed.includes('*/')) {
            inComment = false;
        }
        
        // Skip empty lines
        if (trimmed === '') {
            if (!inRule) {
                output.push(line);
            }
            continue;
        }
        
        // Count braces
        if (!inComment) {
            for (let char of line) {
                if (char === '{') {
                    braceDepth++;
                    inRule = true;
                } else if (char === '}') {
                    braceDepth--;
                    if (braceDepth === 0) {
                        inRule = false;
                    }
                }
            }
        }
        
        // Handle CSS rules
        if (inRule) {
            currentRule.push(line);
            
            // If we're back at depth 0, output the complete rule
            if (braceDepth === 0) {
                output.push(...currentRule);
                output.push('}'); // Add closing brace
                output.push(''); // Add blank line after rule
                currentRule = [];
            }
        } else if (trimmed.startsWith('/*') || trimmed.startsWith('*') || trimmed.startsWith('*/')) {
            // Comments outside rules
            output.push(line);
        } else if (trimmed.includes('{')) {
            // Start of a new rule
            currentRule = [line];
            inRule = true;
        } else if (trimmed.startsWith('@')) {
            // At-rules like @media, @keyframes
            output.push(line);
        } else if (trimmed !== '') {
            // Other content
            output.push(line);
        }
    }
    
    // Clean up the output
    let finalCSS = output.join('\n');
    
    // Remove multiple consecutive empty lines
    finalCSS = finalCSS.replace(/\n{3,}/g, '\n\n');
    
    // Ensure closing braces are properly formatted
    finalCSS = finalCSS.replace(/}\s*}/g, '}\n}');
    
    // Save the fixed file
    fs.writeFileSync(filePath, finalCSS);
    console.log(`Fixed ${filePath}`);
}

// Fix the dashboard CSS files
const dashboardCSS = path.join(__dirname, 'assets/css/hvac-dashboard.css');
fixCSSProperly(dashboardCSS);

const enhancedCSS = path.join(__dirname, 'assets/css/hvac-dashboard-enhanced.css');
if (fs.existsSync(enhancedCSS)) {
    fixCSSProperly(enhancedCSS);
}

console.log('CSS fixing complete!');