<?php
$file = __DIR__ . '/add_results_midterm.php';
$content = file_get_contents($file);

// Find the old section boundaries
$sectionStart = strrpos(substr($content, 0, strpos($content, 'content-header">')), '<section');
$sectionEnd = strpos($content, '</section>', $sectionStart) + strlen('</section>');

$oldSection = substr($content, $sectionStart, $sectionEnd - $sectionStart);

$newSection = '<section class="content-header premium-header">
                <div class="container-fluid">
                    <div class="header-glass-card">
                        <div class="row align-items-center">
                            <div class="col-md-8">
                                <div class="title-section">
                                    <div class="icon-box bg-primary-gradient shadow-sm">
                                        <i class="fas fa-clipboard-list"></i>
                                    </div>
                                    <div>
                                        <h1 class="premium-title text-primary">
                                            Result Management
                                            <span class="premium-subtitle text-secondary">Mid Term Results - <?= htmlspecialchars($subject_name) ?></span>
                                        </h1>
                                        <div class="mt-1">
                                            <?php if ($currentSessionName): ?>
                                                <span class="badge badge-primary mr-1">
                                                    <i class="fas fa-calendar-alt"></i> <?= htmlspecialchars($currentSessionName) ?>
                                                </span>
                                            <?php endif; ?>
                                            <?php if ($currentTermName): ?>
                                                <span class="badge badge-success">
                                                    <i class="fas fa-bookmark"></i> <?= htmlspecialchars($currentTermName) ?>
                                                </span>
                                            <?php else: ?>
                                                <span class="badge badge-warning">
                                                    <i class="fas fa-exclamation-triangle"></i> No active term
                                                </span>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <nav aria-label="breadcrumb">
                                    <ol class="breadcrumb premium-breadcrumb">
                                        <li class="breadcrumb-item"><a href="teacher_dashboard.php" class="text-primary">Home</a></li>
                                        <li class="breadcrumb-item active" aria-current="page">Mid Term Results - <?= htmlspecialchars($subject_name) ?></li>
                                    </ol>
                                </nav>
                            </div>
                        </div>
                    </div>
                </div>
            </section>';

// Replace using str_replace on the exact extracted block
$newContent = str_replace($oldSection, $newSection, $content, $count);

if ($count === 0) {
    echo "FAILED: old section not found\n";
    echo "Old section was:\n" . htmlspecialchars($oldSection) . "\n";
} else {
    file_put_contents($file, $newContent);
    echo "SUCCESS: premium header applied ($count replacement(s))\n";
}
