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

$base_dir = __DIR__;
$templates_dir = "$base_dir/templates";
$landingpages_dir = "$base_dir/landingpages";

// Extract modal dependencies from landing1.php
$landing1 = file_get_contents("$templates_dir/landing1.php");
preg_match('/<!-- ===================== PAYMENT MODAL ===================== -->[\s\S]*?(?=<script>)/', $landing1, $m_modal);
$modal_html = $m_modal[0] ?? '';
preg_match('/<script>[\s\S]*?const SITE[\s\S]*?<\/script>/', $landing1, $m_script);
$modal_script = $m_script[0] ?? '';
$modal_css = file_get_contents("$base_dir/modal_css.txt"); // Let's just define the css dynamically

$modal_css = "
    <style>
        /* --- 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; }
        .modal-overlay { z-index: 999999; } /* Ensure absolute top */
    </style>
";

$php_header = <<<'PHP'
<?php
// templates/landing{ID}.php
$site_settings = get_site_settings($pdo);
$landing_title = $site_settings['landing_title'] ?? 'UTAMU';
$favicon = $site_settings['site_favicon'] ?? 'assets/images/favicon.png';

if (!$featured && !empty($videos)) {
    $featured = $videos[0];
}
?>

PHP;

function fixAssets($html) {
    // Replace assets paths if needed
    $html = str_replace('../cdnjs.cloudflare.com', 'https://cdnjs.cloudflare.com', $html);
    return $html;
}

// -----------------------------------------------------
// MIGRATION: LANDING 4
// -----------------------------------------------------
$html4 = file_get_contents("$landingpages_dir/landing4.html");
$html4 = fixAssets($html4);

// 1. Slice everything before <div class="neon-grid">
$parts4 = explode('<div class="neon-grid">', $html4);
$top4 = $parts4[0] . '<div class="neon-grid">' . "\n";

// 2. Generate the dynamic foreach for Neon Cards
$loop4 = <<<'PHP'
            <?php foreach ($videos as $vid): ?>
            <div class="neon-card" onclick="openPaymentModal('<?= htmlspecialchars($vid['id']) ?>', '<?= (int)$vid['price'] ?>', '<?= htmlspecialchars(addslashes($vid['title'])) ?>'); return false;">
                <div class="card-inner">
                    <img src="<?= htmlspecialchars($vid['thumbnail_url'] ?: SITE_URL.'/assets/placeholder.jpg') ?>" alt="">
                    <div style="position: absolute; top: 15px; left: 15px; background: rgba(0,0,0,0.8); padding: 5px 12px; border-radius: 8px; font-size: 0.7rem;">
                        4K HDR
                    </div>
                </div>
                <div class="card-meta">
                    <div>
                        <h3 style="font-size: 1rem; margin-bottom: 5px;"><?= htmlspecialchars($vid['title']) ?></h3>
                        <p style="font-size: 0.7rem; color: #666;"><?= number_format($vid['views'] ?? rand(5,50)) ?> VIEWS</p>
                    </div>
                    <div class="lock-shield">
                        <i class="fas fa-fingerprint"></i>
                    </div>
                </div>
                <div style="position: absolute; bottom: -10px; left: 50%; transform: translateX(-50%); background: var(--neon-pink); font-size: 0.6rem; padding: 4px 15px; border-radius: 4px; font-weight: 800;">
                    <?= number_format($vid['price']) ?> TSH
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </main>
PHP;

$final4 = str_replace('{ID}', '4', $php_header) . $top4 . $loop4 . "\n" . $modal_css . "\n" . $modal_html . "\n" . $modal_script . "\n</body>\n</html>";

// Fix static elements in Top4
$final4 = preg_replace('/<title>.*?<\/title>/s', '<title><?= htmlspecialchars($landing_title) ?> | Premium</title>', $final4);
$final4 = str_replace('<div class="logo">UTAMU<span>X</span></div>', '<div class="logo"><?= htmlspecialchars($landing_title) ?><span>X</span></div>', $final4);

// Fix featured hero
$hero4 = <<<'PHP'
        <?php if ($featured): ?>
        <section class="spotlight">
            <div class="spotlight-content">
                <span style="color: var(--neon-pink); font-weight: 700; font-size: 0.8rem; letter-spacing: 2px;">EXCLUSIVE PREMIERE</span>
                <h1 style="font-size: 3rem; font-family: 'Syncopate'; margin: 10px 0 20px;"><?= htmlspecialchars($featured['title']) ?></h1>
                <button class="btn-neon" onclick="openPaymentModal('<?= htmlspecialchars($featured['id']) ?>', '<?= (int)$featured['price'] ?>', '<?= htmlspecialchars(addslashes($featured['title'])) ?>'); return false;">UNLOCK ACCESS - <?= number_format($featured['price']) ?> TSH</button>
            </div>
            <div class="spotlight-visual" style="background: url('<?= htmlspecialchars($featured['thumbnail_url'] ?: SITE_URL.'/assets/landing3.jpg') ?>') center/cover;"></div>
        </section>
        <?php endif; ?>
PHP;
$final4 = preg_replace('/<section class="spotlight">.*?<\/section>/s', $hero4, $final4);
file_put_contents("$templates_dir/landing4.php", $final4);


// -----------------------------------------------------
// MIGRATION: LANDING 5
// -----------------------------------------------------
$html5 = file_get_contents("$landingpages_dir/landing5.html");
$html5 = fixAssets($html5);

$parts5 = explode('<div class="video-list">', $html5);
$top5 = $parts5[0] . '<div class="video-list">' . "\n";

$loop5 = <<<'PHP'
            <?php foreach ($videos as $vid): ?>
            <div class="video-row" onclick="openPaymentModal('<?= htmlspecialchars($vid['id']) ?>', '<?= (int)$vid['price'] ?>', '<?= htmlspecialchars(addslashes($vid['title'])) ?>'); return false;">
                <img src="<?= htmlspecialchars($vid['thumbnail_url'] ?: SITE_URL.'/assets/placeholder.jpg') ?>" class="video-img" alt="Video">
                <div class="video-info">
                    <div>
                        <h3><?= htmlspecialchars($vid['title']) ?></h3>
                        <span class="price-chip">TSH <?= number_format($vid['price']) ?></span>
                    </div>
                    <div class="stats">
                        <i class="fas fa-eye"></i> <?= number_format($vid['views'] ?? rand(10,100)) ?> • <i class="fas fa-clock"></i> Leo
                    </div>
                </div>
                <div style="display: flex; align-items: center;">
                    <i class="fas fa-chevron-right" style="color: #333;"></i>
                </div>
            </div>
            <?php endforeach; ?>
        </div>

        <div class="bottom-nav">
            <a href="#" class="nav-link active"><i class="fas fa-home"></i>Nyumbani</a>
            <a href="#" class="nav-link"><i class="fas fa-fire"></i>Moto</a>
            <a href="#" class="nav-link"><i class="fas fa-play-circle"></i>Video Zangu</a>
            <a href="#" class="nav-link"><i class="fas fa-user"></i>Akaunti</a>
        </div>
PHP;

$final5 = str_replace('{ID}', '5', $php_header) . $top5 . $loop5 . "\n" . $modal_css . "\n" . $modal_html . "\n" . $modal_script . "\n</body>\n</html>";
$final5 = preg_replace('/<title>.*?<\/title>/s', '<title><?= htmlspecialchars($landing_title) ?> | Premium</title>', $final5);
$final5 = str_replace('<div class="logo">UTAMU<span>+</span></div>', '<div class="logo"><?= htmlspecialchars($landing_title) ?><span>+</span></div>', $final5);

$hero5 = <<<'PHP'
    <?php if ($featured): ?>
    <div class="promo-banner" onclick="openPaymentModal('<?= htmlspecialchars($featured['id']) ?>', '<?= (int)$featured['price'] ?>', '<?= htmlspecialchars(addslashes($featured['title'])) ?>'); return false;" style="cursor: pointer;">
        <span class="badge-live">HOT NEW</span>
        <h2 style="margin-top: 10px;"><?= htmlspecialchars($featured['title']) ?></h2>
        <p>Watch full exclusive leaks.</p>
        <button style="background: white; color: black; border: none; padding: 8px 15px; border-radius: 6px; font-weight: 800; font-size: 0.7rem; pointer-events: none;">ANGALIA TSH <?= number_format($featured['price']) ?></button>
    </div>
    <?php endif; ?>
PHP;
$final5 = preg_replace('/<div class="promo-banner">.*?<\/div>/s', $hero5, $final5);
file_put_contents("$templates_dir/landing5.php", $final5);


// -----------------------------------------------------
// MIGRATION: LANDING 6
// -----------------------------------------------------
$html6 = file_get_contents("$landingpages_dir/landing6.html");
$html6 = fixAssets($html6);

$parts6 = explode('<div class="movie-grid">', $html6);
$top6 = $parts6[0] . '<div class="movie-grid">' . "\n";

$loop6 = <<<'PHP'
            <?php foreach ($videos as $vid): ?>
            <div class="movie-card" onclick="openPaymentModal('<?= htmlspecialchars($vid['id']) ?>', '<?= (int)$vid['price'] ?>', '<?= htmlspecialchars(addslashes($vid['title'])) ?>'); return false;">
                <div class="card-poster">
                    <img src="<?= htmlspecialchars($vid['thumbnail_url'] ?: SITE_URL.'/assets/placeholder.jpg') ?>" alt="">
                    <div class="card-badges">
                        <span class="badge badge-new">NEW</span>
                        <span class="badge badge-hd">HD</span>
                    </div>
                    <div class="card-overlay">
                        <div class="overlay-buttons">
                            <div class="circle-btn play-btn"><i class="fas fa-play"></i></div>
                            <div class="circle-btn"><i class="fas fa-plus"></i></div>
                            <div class="circle-btn"><i class="fas fa-thumbs-up"></i></div>
                        </div>
                        <div class="overlay-meta">
                            <span class="match">98% Match</span>
                            <span>2024</span>
                            <span style="border: 1px solid #aaa; padding: 0 4px; border-radius: 2px;">18+</span>
                        </div>
                    </div>
                    <div class="card-price">TSH <?= number_format($vid['price']) ?></div>
                </div>
                <div class="card-info">
                    <h3><?= htmlspecialchars($vid['title']) ?></h3>
                    <p><?= rand(1, 3) ?> Parts • Exclusive</p>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
PHP;

$final6 = str_replace('{ID}', '6', $php_header) . $top6 . $loop6 . "\n" . $modal_css . "\n" . $modal_html . "\n" . $modal_script . "\n</body>\n</html>";
$final6 = preg_replace('/<title>.*?<\/title>/s', '<title><?= htmlspecialchars($landing_title) ?> | Premium</title>', $final6);
$final6 = str_replace('<a href="#" class="logo">utamu+</a>', '<a href="#" class="logo"><?= htmlspecialchars($landing_title) ?>+</a>', $final6);

$hero6 = <<<'PHP'
    <?php if ($featured): ?>
    <section class="hero" style="background: linear-gradient(to right, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0.3) 50%, rgba(0, 0, 0, 0.1) 100%), linear-gradient(to top, #141414 0%, transparent 30%), url('<?= htmlspecialchars($featured['thumbnail_url'] ?: SITE_URL.'/assets/landing2.jpg') ?>') center/cover no-repeat; background-color: #141414;">
        <div class="hero-content">
            <span class="hero-tag">#1 IN TRENDING</span>
            <h1 class="hero-title"><?= htmlspecialchars($featured['title']) ?></h1>
            <div class="hero-meta">
                <span class="match">99% Match</span>
                <span class="year">2024</span>
                <span class="rating">18+</span>
                <span class="duration">1h 24m</span>
                <span style="border: 1px solid #aaa; padding: 0 4px; border-radius: 2px;">HD</span>
            </div>
            <p class="hero-desc">Watch the most trending piece and unreleased scenes globally. Action, Drama and more...</p>
            <div class="hero-buttons">
                <button class="btn btn-white" onclick="openPaymentModal('<?= htmlspecialchars($featured['id']) ?>', '<?= (int)$featured['price'] ?>', '<?= htmlspecialchars(addslashes($featured['title'])) ?>'); return false;">
                    <i class="fas fa-play"></i> Unlock TSH <?= number_format($featured['price']) ?>
                </button>
                <button class="btn btn-gray">
                    <i class="fas fa-info-circle"></i> More Info
                </button>
            </div>
        </div>
        <div class="hero-fade"></div>
    </section>
    <?php endif; ?>
PHP;
$final6 = preg_replace('/<section class="hero".*?<\/section>/s', $hero6, $final6);
file_put_contents("$templates_dir/landing6.php", $final6);

echo "SUCCESS";

