<?php
/**
 * CSS Consolidation Build Script
 * 
 * Combines frequently used HVAC CSS files into a single consolidated file
 * for improved performance by reducing HTTP requests.
 */

// Ensure this runs only from CLI
if (php_sapi_name() !== 'cli') {
    die('This script must be run from command line');
}

// Define base paths
$plugin_dir = dirname(__DIR__);
$css_dir = $plugin_dir . '/assets/css';
$consolidated_file = $css_dir . '/hvac-consolidated.css';

// Core CSS files to consolidate (in order of dependency)
$core_files = [
    'hvac-community-events.css',
    'hvac-page-templates.css', 
    'hvac-layout.css',
    'hvac-common.css'
];

// Optional CSS files that are commonly loaded together
$common_files = [
    'hvac-dashboard.css',
    'hvac-trainer-profile.css',
    'hvac-certificates.css'
];

/**
 * Consolidate CSS files
 */
function consolidate_css_files($css_dir, $files, $output_file) {
    $consolidated_content = "/* HVAC Consolidated CSS - Generated on " . date('Y-m-d H:i:s') . " */\n\n";
    
    $total_size = 0;
    $files_processed = 0;
    
    foreach ($files as $file) {
        $file_path = $css_dir . '/' . $file;
        
        if (!file_exists($file_path)) {
            echo "Warning: File not found: $file\n";
            continue;
        }
        
        $content = file_get_contents($file_path);
        if ($content === false) {
            echo "Error: Could not read file: $file\n";
            continue;
        }
        
        // Add file header comment
        $consolidated_content .= "/* === $file === */\n";
        $consolidated_content .= $content;
        $consolidated_content .= "\n\n";
        
        $size = filesize($file_path);
        $total_size += $size;
        $files_processed++;
        
        echo "Processed: $file (" . number_format($size) . " bytes)\n";
    }
    
    // Write consolidated file
    $result = file_put_contents($output_file, $consolidated_content);
    
    if ($result === false) {
        echo "Error: Could not write consolidated file: $output_file\n";
        return false;
    }
    
    $final_size = filesize($output_file);
    $savings = max(0, $total_size - $final_size);
    $compression_ratio = $total_size > 0 ? (($savings / $total_size) * 100) : 0;
    
    echo "\n=== Consolidation Complete ===\n";
    echo "Files processed: $files_processed\n";
    echo "Original total size: " . number_format($total_size) . " bytes\n";
    echo "Consolidated size: " . number_format($final_size) . " bytes\n";
    echo "Space savings: " . number_format($savings) . " bytes (" . number_format($compression_ratio, 1) . "%)\n";
    echo "Output file: $output_file\n";
    
    return true;
}

// Main execution
echo "Starting CSS consolidation...\n\n";

// Consolidate core files
$all_files = array_merge($core_files, $common_files);
$success = consolidate_css_files($css_dir, $all_files, $consolidated_file);

if ($success) {
    echo "\nCSS consolidation completed successfully!\n";
    echo "To use consolidated CSS, ensure no HVAC_CSS_DEBUG constant is defined.\n";
    echo "The system will automatically detect and use the consolidated file.\n";
} else {
    echo "\nCSS consolidation failed!\n";
    exit(1);
}