<?php
$file = 'c:\xampp\htdocs\inveetaire\app\views\public\identity.php';
$content = file_get_contents($file);

$sections = [
    'hero' => ['<!-- 1. HERO COVER', '<!-- 2. OUR LOVE STORY'],
    'story' => ['<!-- 2. OUR LOVE STORY', '<!-- COUNTDOWN TO OUR SPECIAL DAY'],
    'countdown' => ['<!-- COUNTDOWN TO OUR SPECIAL DAY', '<!-- 3. CEREMONY AGENDA'],
    'ceremony' => ['<!-- 3. CEREMONY AGENDA', '<!-- OUR VENUE'],
    'venue' => ['<!-- OUR VENUE', '<!-- 4. GALLERY SLIDER'],
    'gallery' => ['<!-- 4. GALLERY SLIDER', '<!-- 5. RSVP'],
    'rsvp' => ['<!-- 5. RSVP', '<!-- 6. LEAVE YOUR WISHES'],
    'wishes' => ['<!-- 6. LEAVE YOUR WISHES', '<!-- 7. GIFT'],
    'gift' => ['<!-- 7. GIFT', '<!-- 8. STORY GALLERY'],
    'story_gallery' => ['<!-- 8. STORY GALLERY', '<!-- 9. WARM WISHES'],
    'warm_wishes' => ['<!-- 9. WARM WISHES', '<!-- 10. DIGITAL ENTRY PASS'],
    'entry_pass' => ['<!-- 10. DIGITAL ENTRY PASS', '</div> <!-- Close z-10 wrapper -->'],
];

$partials = [
    'footer' => ['<!-- Elegant Branded Footer', '<!-- MODALS & POPUPS'],
];

$border = '<!-- ================================================================= -->';

foreach ($sections as $name => $bounds) {
    $start = strpos($content, $bounds[0]);
    $end = strpos($content, $bounds[1]);
    
    $realStart = strrpos(substr($content, 0, $start), $border);
    if ($realStart !== false && ($start - $realStart) < 100) {
        $start = $realStart;
    }

    $realEnd = strrpos(substr($content, 0, $end), $border);
    if ($realEnd !== false && ($end - $realEnd) < 100) {
        $end = $realEnd;
    }

    if ($start !== false && $end !== false) {
        $chunk = substr($content, $start, $end - $start);
        file_put_contents("c:\\xampp\\htdocs\\inveetaire\\app\\views\\public\\identity\\sections\\{$name}.php", trim($chunk) . "\n");
        // Replace chunk with require
        $content = substr_replace($content, "<?php require __DIR__ . '/identity/sections/{$name}.php'; ?>\n            ", $start, $end - $start);
    } else {
        echo "Failed to find bounds for section {$name}\n";
    }
}

// Now handle partials
$footerStart = strpos($content, $partials['footer'][0]);
$footerEnd = strpos($content, $partials['footer'][1]);
if ($footerStart !== false && $footerEnd !== false) {
    $footerEndBorder = strrpos(substr($content, 0, $footerEnd), $border);
    if ($footerEndBorder !== false && ($footerEnd - $footerEndBorder) < 100) {
        $footerEnd = $footerEndBorder;
    }

    $chunk = substr($content, $footerStart, $footerEnd - $footerStart);
    file_put_contents("c:\\xampp\\htdocs\\inveetaire\\app\\views\\public\\identity\\partials\\footer.php", trim($chunk) . "\n");
    $content = substr_replace($content, "<?php require __DIR__ . '/identity/partials/footer.php'; ?>\n    ", $footerStart, $footerEnd - $footerStart);
}

// Modals are split into pass_modal and qris_modal
$passModalStart = strpos($content, '<!-- DIGITAL ENTRY PASS MODAL');
$qrisModalStart = strpos($content, '<!-- QRIS MODAL');
$jsEngineStart = strpos($content, '<!-- JAVASCRIPT HELPERS');

if ($passModalStart !== false && $qrisModalStart !== false && $jsEngineStart !== false) {
    $passModalChunk = substr($content, $passModalStart, $qrisModalStart - $passModalStart);
    file_put_contents("c:\\xampp\\htdocs\\inveetaire\\app\\views\\public\\identity\\partials\\pass_modal.php", trim($passModalChunk) . "\n");
    $content = substr_replace($content, "<?php require __DIR__ . '/identity/partials/pass_modal.php'; ?>\n    ", $passModalStart, $qrisModalStart - $passModalStart);
    
    // Refetch the qris Modal Start because content shifted!
    $qrisModalStart = strpos($content, '<!-- QRIS MODAL');
    $jsEngineStart = strpos($content, '<!-- JAVASCRIPT HELPERS');
    
    $jsEngineBorder = strrpos(substr($content, 0, $jsEngineStart), $border);
    if ($jsEngineBorder !== false && ($jsEngineStart - $jsEngineBorder) < 100) {
        $jsEngineStart = $jsEngineBorder;
    }
    
    $qrisModalChunk = substr($content, $qrisModalStart, $jsEngineStart - $qrisModalStart);
    file_put_contents("c:\\xampp\\htdocs\\inveetaire\\app\\views\\public\\identity\\partials\\qris_modal.php", trim($qrisModalChunk) . "\n");
    $content = substr_replace($content, "<?php require __DIR__ . '/identity/partials/qris_modal.php'; ?>\n    ", $qrisModalStart, $jsEngineStart - $qrisModalStart);
}

file_put_contents('c:\xampp\htdocs\inveetaire\app\views\public\identity.php', $content);
echo "Done!\n";
