<?php
/**
 * Script to convert Cartzilla HTML pages to PHP
 * This script will:
 * 1. Copy HTML files
 * 2. Replace .html links with .php
 * 3. Add PHP includes for header/footer
 * 4. Keep 100% of the original markup
 */

$cartzillaDir = __DIR__ . '/../Cartzilla';
$outputDir = __DIR__ . '/..';

// Pages to convert
$pages = [
    'home-electronics.html' => 'index.php',
    'shop-catalog-electronics.html' => 'shop-catalog-electronics.php',
    'shop-categories-electronics.html' => 'shop-categories-electronics.php',
    'shop-product-details-electronics.html' => 'shop-product-details-electronics.php',
    'shop-product-general-electronics.html' => 'shop-product-general-electronics.php',
    'shop-product-reviews-electronics.html' => 'shop-product-reviews-electronics.php',
    'checkout-v1-cart.html' => 'checkout-v1-cart.php',
    'checkout-v1-delivery-1.html' => 'checkout-v1-delivery-1.php',
    'checkout-v1-delivery-2.html' => 'checkout-v1-delivery-2.php',
    'checkout-v1-shipping.html' => 'checkout-v1-shipping.php',
    'checkout-v1-payment.html' => 'checkout-v1-payment.php',
    'checkout-v1-thankyou.html' => 'checkout-v1-thankyou.php',
    'account-signin.html' => 'account-signin.php',
    'account-signup.html' => 'account-signup.php',
    'account-info.html' => 'account-info.php',
    'account-orders.html' => 'account-orders.php',
    'account-wishlist.html' => 'account-wishlist.php',
    'account-addresses.html' => 'account-addresses.php',
    'account-payment.html' => 'account-payment.php',
    'account-reviews.html' => 'account-reviews.php',
    'account-notifications.html' => 'account-notifications.php',
    'account-password-recovery.html' => 'account-password-recovery.php',
    'about-v1.html' => 'about-v1.php',
    'contact-v3.html' => 'contact-v3.php',
    'blog-list.html' => 'blog-list.php',
    'blog-single-v1.html' => 'blog-single-v1.php',
    '404-electronics.html' => '404-electronics.php',
];

// Link replacements
$linkReplacements = [
    'index.html' => 'index.php',
    'shop-catalog-electronics.html' => 'shop-catalog-electronics.php',
    'shop-categories-electronics.html' => 'shop-categories-electronics.php',
    'shop-product-details-electronics.html' => 'shop-product-details-electronics.php',
    'shop-product-general-electronics.html' => 'shop-product-general-electronics.php',
    'shop-product-reviews-electronics.html' => 'shop-product-reviews-electronics.php',
    'checkout-v1-cart.html' => 'checkout-v1-cart.php',
    'checkout-v1-delivery-1.html' => 'checkout-v1-delivery-1.php',
    'checkout-v1-delivery-2.html' => 'checkout-v1-delivery-2.php',
    'checkout-v1-shipping.html' => 'checkout-v1-shipping.php',
    'checkout-v1-payment.html' => 'checkout-v1-payment.php',
    'checkout-v1-thankyou.html' => 'checkout-v1-thankyou.php',
    'account-signin.html' => 'account-signin.php',
    'account-signup.html' => 'account-signup.php',
    'account-info.html' => 'account-info.php',
    'account-orders.html' => 'account-orders.php',
    'account-wishlist.html' => 'account-wishlist.php',
    'account-addresses.html' => 'account-addresses.php',
    'account-payment.html' => 'account-payment.php',
    'account-reviews.html' => 'account-reviews.php',
    'account-notifications.html' => 'account-notifications.php',
    'account-password-recovery.html' => 'account-password-recovery.php',
    'about-v1.html' => 'about-v1.php',
    'contact-v3.html' => 'contact-v3.php',
    'blog-list.html' => 'blog-list.php',
    'blog-single-v1.html' => 'blog-single-v1.php',
    '404-electronics.html' => '404-electronics.php',
];

foreach ($pages as $htmlFile => $phpFile) {
    $inputPath = $cartzillaDir . '/' . $htmlFile;
    $outputPath = $outputDir . '/' . $phpFile;
    
    if (!file_exists($inputPath)) {
        echo "Skipping $htmlFile (not found)\n";
        continue;
    }
    
    $content = file_get_contents($inputPath);
    
    // Replace all .html links with .php
    foreach ($linkReplacements as $oldLink => $newLink) {
        $content = str_replace($oldLink, $newLink, $content);
        $content = str_replace('"' . $oldLink, '"' . $newLink, $content);
        $content = str_replace("'" . $oldLink, "'" . $newLink, $content);
    }
    
    // Extract title for pageTitle variable
    $title = extractTitle($content);
    
    // Create PHP file structure
    // Remove everything before <body> and after </body>
    $bodyStart = strpos($content, '<body');
    $bodyEnd = strrpos($content, '</body>');
    
    if ($bodyStart !== false && $bodyEnd !== false) {
        // Extract body content
        $bodyTagEnd = strpos($content, '>', $bodyStart) + 1;
        $bodyContent = substr($content, $bodyTagEnd, $bodyEnd - $bodyTagEnd);
        
        // Build PHP file
        $phpContent = "<?php\n";
        $phpContent .= "/**\n";
        $phpContent .= " * " . ucfirst(str_replace('-', ' ', basename($phpFile, '.php'))) . " Page\n";
        $phpContent .= " * Converted from {$htmlFile}\n";
        $phpContent .= " */\n\n";
        $phpContent .= "\$pageTitle = '" . addslashes($title) . "';\n";
        $phpContent .= "\$pageDescription = 'Cartzilla - Multipurpose Bootstrap E-Commerce HTML Template';\n\n";
        $phpContent .= "require_once __DIR__ . '/includes/header.php';\n";
        $phpContent .= "?>\n\n";
        $phpContent .= $bodyContent;
        $phpContent .= "\n\n<?php\n";
        $phpContent .= "require_once __DIR__ . '/includes/footer.php';\n";
        $phpContent .= "?>\n";
        
        $content = $phpContent;
    }
    
    file_put_contents($outputPath, $content);
    echo "Converted $htmlFile to $phpFile\n";
}

function extractTitle($content) {
    if (preg_match('/<title>(.*?)<\/title>/i', $content, $matches)) {
        return $matches[1];
    }
    return 'Cartzilla | Electronics Store';
}

echo "Conversion complete!\n";
?>

