<?php
/**
 * HTML to PHP Converter Script
 * Converts Cartzilla and Falcon HTML pages to PHP while preserving 100% of the markup
 * 
 * Usage: php build/convert-html-to-php.php [source_dir] [target_dir] [template_type]
 * Example: php build/convert-html-to-php.php Cartzilla . cartzilla
 */

$sourceDir = $argv[1] ?? 'Cartzilla';
$targetDir = $argv[2] ?? '.';
$templateType = $argv[3] ?? 'cartzilla'; // 'cartzilla' or 'falcon'

$htmlFiles = glob($sourceDir . '/*.html');

if (empty($htmlFiles)) {
    echo "No HTML files found in {$sourceDir}\n";
    exit(1);
}

foreach ($htmlFiles as $htmlFile) {
    $fileName = basename($htmlFile, '.html');
    $phpFile = $targetDir . '/' . $fileName . '.php';
    
    echo "Converting: {$htmlFile} -> {$phpFile}\n";
    
    $content = file_get_contents($htmlFile);
    
    // Extract head section (before <body>)
    preg_match('/<head>.*?<\/head>/s', $content, $headMatches);
    $head = $headMatches[0] ?? '';
    
    // Extract body content (between <body> and </body>)
    preg_match('/<body[^>]*>(.*?)<\/body>/s', $content, $bodyMatches);
    $bodyContent = $bodyMatches[1] ?? '';
    
    // Find main content area (between header and footer)
    // For Cartzilla: look for <main> or content after header
    // For Falcon: look for <main class="main">
    
    if ($templateType === 'cartzilla') {
        // Cartzilla structure: header is in navbar, main content starts after
        // We'll use includes/header.php and includes/footer.php
        $phpContent = "<?php\n";
        $phpContent .= "/**\n";
        $phpContent .= " * " . ucfirst(str_replace('-', ' ', $fileName)) . " Page\n";
        $phpContent .= " * Converted from {$fileName}.html\n";
        $phpContent .= " */\n\n";
        $phpContent .= "\$pageTitle = '" . htmlspecialchars($fileName, ENT_QUOTES) . "';\n";
        $phpContent .= "\$pageDescription = 'Cartzilla - Multipurpose Bootstrap E-Commerce HTML Template';\n\n";
        $phpContent .= "require_once __DIR__ . '/includes/header.php';\n";
        $phpContent .= "?>\n\n";
        
        // Extract main content (everything between header and footer)
        // For now, we'll include the body content and let header/footer handle the rest
        $phpContent .= $bodyContent;
        
        $phpContent .= "\n\n<?php\n";
        $phpContent .= "require_once __DIR__ . '/includes/footer.php';\n";
        $phpContent .= "?>\n";
        
    } else {
        // Falcon structure
        $phpContent = "<?php\n";
        $phpContent .= "/**\n";
        $phpContent .= " * Admin: " . ucfirst(str_replace('-', ' ', $fileName)) . " Page\n";
        $phpContent .= " * Converted from {$fileName}.html\n";
        $phpContent .= " */\n\n";
        $phpContent .= "require_once __DIR__ . '/../includes/init.php';\n";
        $phpContent .= "\$auth->requireAdmin();\n\n";
        $phpContent .= "require_once __DIR__ . '/includes/admin-header.php';\n";
        $phpContent .= "?>\n\n";
        
        $phpContent .= $bodyContent;
        
        $phpContent .= "\n\n<?php\n";
        $phpContent .= "require_once __DIR__ . '/includes/admin-footer.php';\n";
        $phpContent .= "?>\n";
    }
    
    // Replace .html links with .php (for internal links)
    $phpContent = preg_replace('/href="([^"]+)\.html"/', 'href="$1.php"', $phpContent);
    $phpContent = preg_replace("/href='([^']+)\.html'/", "href='$1.php'", $phpContent);
    
    // Write PHP file
    file_put_contents($phpFile, $phpContent);
    echo "  ✓ Created: {$phpFile}\n";
}

echo "\nConversion complete!\n";
?>

