<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$base_dir = __DIR__ . '/templates';
$landing1 = file_get_contents("$base_dir/landing1.php");

// 1. Extract the script block
preg_match('/<script>[\s\S]*?const SITE = \'<\?= SITE_URL \?>\';[\s\S]*?<\/script>/', $landing1, $script_matches);
$script_block = $script_matches[0] ?? '';

// 2. Extract the modal HTML
preg_match('/<!-- ===================== PAYMENT MODAL ===================== -->[\s\S]*?(?=<script>)/', $landing1, $modal_matches);
$modal_html = trim($modal_matches[0] ?? '');

// 3. Define the new CSS
$new_css = "
        /* --- Payment Modal Styles --- */
        .modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.95); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); display: none; justify-content: center; align-items: center; z-index: 2000; padding: max(20px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-left, 0px)) max(20px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-right, 0px)); }
        .modal-content { background: var(--card-bg, #1e1e1e); width: 100%; max-width: min(450px, 95vw); border-radius: 16px; position: relative; border: 1px solid rgba(255,255,255,0.1); animation: modalSlideUp 0.3s cubic-bezier(0.4,0,0.2,1); max-height: 90vh; overflow-y: auto; -webkit-overflow-scrolling: touch; color: var(--text-main, #fff); }
        @keyframes modalSlideUp { from { transform: translateY(50px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
        .modal-header { padding: clamp(20px, 4vw, 25px); border-bottom: 1px solid rgba(255,255,255,0.1); text-align: center; position: sticky; top: 0; background: var(--card-bg, #1e1e1e); z-index: 1; border-radius: 16px 16px 0 0; }
        .modal-body { padding: clamp(20px, 4vw, 25px); }
        .close-modal { position: absolute; top: 16px; right: 16px; color: var(--text-dim, #aaa); cursor: pointer; font-size: 1.5rem; width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; border-radius: 50%; transition: background-color 0.2s; z-index: 2; }
        .close-modal:hover, .close-modal:active { background-color: rgba(255,255,255,0.1); }
        .input-group { margin-top: 20px; text-align: left; }
        .input-group label { display: block; font-size: clamp(0.75rem, 2vw, 0.85rem); color: var(--text-dim, #aaa); margin-bottom: 8px; font-weight: 600; }
        .input-group input { width: 100%; background: rgba(0,0,0,0.2); border: 1px solid rgba(255,255,255,0.2); padding: clamp(12px, 3vw, 16px); color: var(--text-main, #fff); border-radius: 8px; outline: none; font-size: clamp(0.9rem, 2vw, 1rem); transition: border-color 0.2s; -webkit-appearance: none; box-sizing: border-box; }
        .input-group input:focus { border-color: var(--primary); }
        .btn-full { width: 100%; margin-top: 20px; background: var(--primary); color: white; font-size: clamp(0.9rem, 2vw, 1rem); padding: clamp(14px, 3vh, 16px); border-radius: 10px; border: none; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 10px; font-weight: 700; transition: all 0.3s ease; min-height: 52px; box-sizing: border-box; }
        .btn-full:hover:not(:disabled) { filter: brightness(1.2); transform: translateY(-2px); }
        .btn-full:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }
        .operator-badges { display: flex; gap: 8px; justify-content: center; margin-bottom: 20px; flex-wrap: wrap; }
        .op-badge { padding: 4px 10px; border-radius: 6px; font-size: 0.65rem; font-weight: 700; color: white; opacity: 0.35; transition: all 0.3s; border: 1px solid transparent; }
        .op-badge.active { opacity: 1; transform: scale(1.1); border-color: rgba(255,255,255,0.5); box-shadow: 0 0 10px rgba(0,0,0,0.5); }
        .op-voda { background: #e60000; } .op-tigo { background: #003366; } .op-airtel { background: #ff0000; } .op-halo { background: #ff6600; }
        .payment-steps { margin: 15px 0; padding: 12px; background: rgba(255,255,255,0.05); border-radius: 8px; font-size: 0.75rem; color: var(--text-dim, #aaa); text-align: left; }
        .step { display: flex; gap: 10px; margin-bottom: 8px; align-items: flex-start; }
        .step:last-child { margin-bottom: 0; }
        .step-num { width: 18px; height: 18px; background: var(--primary); color: white; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 0.6rem; font-weight: bold; flex-shrink: 0; margin-top: 2px; }
        .error-box { background: rgba(229,9,20,0.15); border: 1px solid rgba(229,9,20,0.4); border-radius: 8px; padding: 10px 14px; font-size: 0.8rem; color: #ff6b6b; margin-top: 12px; display: none; text-align: left; }
        #paymentStatus { text-align: center; margin-top: 15px; font-size: 0.85rem; font-weight: 600; min-height: 1.2em; }
        .status-pulse { animation: statusPulse 1.5s infinite; color: var(--primary); }
        @keyframes statusPulse { 0% { opacity: 1; } 50% { opacity: 0.5; } 100% { opacity: 1; } }
        .modal-content::-webkit-scrollbar { width: 4px; }
        .modal-content::-webkit-scrollbar-thumb { background: var(--primary); border-radius: 2px; }
";

if (!$script_block || !$modal_html) {
    die("Failed to parse landing1.php dependencies");
}

for ($i = 2; $i <= 7; $i++) {
    $file = "$base_dir/landing$i.php";
    if (!file_exists($file)) continue;

    $content = file_get_contents($file);
    
    // Remove old modal overlay HTML to the end of the body
    $content = preg_replace('/<div class="modal-overlay"[\s\S]*?<\/html>/i', "<!-- MODAL_INJECT -->\n</body>\n</html>", $content);
    $content = preg_replace('/<div class="modal"[\s\S]*?<\/html>/i', "<!-- MODAL_INJECT -->\n</body>\n</html>", $content);
    $content = preg_replace('/<!-- Payment Modal -->\s*<div id="paymentModal" class="modal"[\s\S]*?<\/html>/i', "<!-- MODAL_INJECT -->\n</body>\n</html>", $content);
    $content = preg_replace('/<div id="paymentModal" class="modal"[\s\S]*?<\/html>/i', "<!-- MODAL_INJECT -->\n</body>\n</html>", $content);

    // Remove old modal CSS
    $content = preg_replace('/\/\* Operator Badges \*\/[\s\S]*?<\/style>/i', "</style>", $content);
    $content = preg_replace('/\.modal-overlay\s*\{[\s\S]*?var\(--primary\);\s*\}/i', "", $content);
    $content = preg_replace('/\.btn-buy\s*\{[\s\S]*?\}/i', "", $content);
    $content = preg_replace('/\.op-voda\s*\{[\s\S]*?keyframes statusPulse[\s\S]*?\}/i', "", $content);
    // Landing 5/6 specific
    $content = preg_replace('/\.modal\s*\{\s*position:\s*fixed;[\s\S]*?backdrop-filter:\s*blur\(10px\);\s*\}/i', "", $content);
    $content = preg_replace('/\.btn-p\s*\{\s*width:\s*100%;[\s\S]*?cursor:\s*pointer;\s*\}/i', "", $content);
    
    // Landing 7 specific CSS
    $content = preg_replace('/\/\* --- MODERN MODAL ---\*\/[\s\S]*?transform: rotate\(360deg\);\s*\}\s*\}/i', "", $content);
    $content = preg_replace('/\/\* Payment Modal Enhancements \*\/[\s\S]*?keyframes spin\s*\{\s*to\s*\{\s*transform:\s*rotate\(360deg\);\s*\}\s*\}/i', "", $content);

    // Insert new CSS
    $content = str_replace('</style>', $new_css . "\n    </style>", $content);

    // Replace the HTML inject
    $content = str_replace('<!-- MODAL_INJECT -->', $modal_html . "\n" . $script_block, $content);

    // Fix openPaymentModal buttons
    $content = preg_replace_callback('/onclick="openPaymentModal\([^"]*?\)"/i', function($m) {
        $str = $m[0];
        
        // Attempt to find variable usages that indicate loop scope
        if (strpos($str, '$featured') !== false) {
            return "onclick=\"openPaymentModal('<?= htmlspecialchars(\$featured['id'] ?? '') ?>', '<?= (int)(\$featured['price'] ?? 0) ?>', '<?= htmlspecialchars(addslashes(\$featured['title'] ?? '')) ?>'); return false;\"";
        }
        if (strpos($str, '$vid') !== false) {
            return "onclick=\"openPaymentModal('<?= htmlspecialchars(\$vid['id'] ?? '') ?>', '<?= (int)(\$vid['price'] ?? 0) ?>', '<?= htmlspecialchars(addslashes(\$vid['title'] ?? '')) ?>'); return false;\"";
        }
        if (strpos($str, '$video') !== false) {
            return "onclick=\"openPaymentModal('<?= htmlspecialchars(\$video['id'] ?? '') ?>', '<?= (int)(\$video['price'] ?? 0) ?>', '<?= htmlspecialchars(addslashes(\$video['title'] ?? '')) ?>'); return false;\"";
        }
        
        return $str; // fallback
    }, $content);

    file_put_contents($file, $content);
    echo "Updated landing$i.php\n";
}

