<?php
/**
 * Classic terminal report template.
 *
 * Faithful reproduction of the original black-and-white print_report.php design,
 * generalised to render from the shared $data array in either 'print' or 'pdf'
 * mode. This is the default template — existing schools see no visual change.
 */

function renderReport_classic(array $data, string $mode = 'print'): string
{
    $settings        = $data['settings'];
    $student         = $data['student'];
    $classSize       = $data['classSize'];
    $subjects        = $data['subjects'];
    $scores          = $data['scores'];
    $totalMarks      = $data['totalMarks'];
    $gradeBoundaries = $data['gradeBoundaries'];
    $academic_year   = $data['academicYear'];
    $termFormatted   = $data['termFormatted'];

    $logo = reportImageSrc($data['logoPath'], $mode);
    $directorSig     = reportImageSrc($data['directorSignature'], $mode);
    $headteacherSig  = reportImageSrc($data['headteacherSignature'], $mode);
    $classTeacherSig = reportImageSrc($data['classTeacherSignature'], $mode);

    $schoolName = strtoupper($data['schoolName']);
    $studentName = strtoupper($student['first_name'] . ' ' . $student['last_name']);

    ob_start();
    ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Terminal Report - <?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Times New Roman', Times, serif; font-size: 11px; line-height: 1.3; color: #000; background: #fff; padding: 10px; }
        .report-card { max-width: 210mm; width: 210mm; min-height: 297mm; margin: 0 auto; background: #fff; padding: 10mm 8mm; border: 1px solid #000; position: relative; }
        .watermark { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); opacity: 0.03; z-index: 0; pointer-events: none; }
        .watermark img { width: 400px; height: 400px; object-fit: contain; }
        .report-header { text-align: center; margin-bottom: 5px; position: relative; z-index: 1; border-bottom: 2px solid #000; padding-bottom: 5px; }
        .school-logo { width: 70px; height: 70px; object-fit: contain; position: absolute; left: 0; top: 0; }
        .school-name { font-size: 18px; font-weight: bold; text-transform: uppercase; margin-bottom: 2px; letter-spacing: 1px; }
        .school-address { font-size: 9px; margin-bottom: 1px; font-weight: 500; }
        .school-contact { font-size: 8px; margin-bottom: 2px; }
        .school-email { font-size: 8px; font-style: italic; }
        .report-title { text-align: center; font-size: 14px; font-weight: bold; text-transform: uppercase; margin: 8px 0; padding: 4px; background: #f0f0f0; border: 1px solid #000; }
        .student-info-row { display: flex; justify-content: space-between; border: 1px solid #000; margin-bottom: 8px; background: #f9f9f9; }
        .info-item { padding: 4px 8px; border-right: 1px solid #000; flex: 1; }
        .info-item:last-child { border-right: none; }
        .info-label { font-weight: bold; display: inline; }
        .dates-row { display: flex; justify-content: space-between; border: 1px solid #000; margin-bottom: 8px; }
        .date-item { padding: 4px 8px; border-right: 1px solid #000; flex: 1; }
        .date-item:last-child { border-right: none; }
        .results-table { width: 100%; border-collapse: collapse; margin-bottom: 8px; font-size: 10px; }
        .results-table th, .results-table td { padding: 4px 3px; border: 1px solid #000; text-align: center; vertical-align: middle; }
        .results-table th { background: #333; color: #fff; font-weight: bold; font-size: 9px; text-transform: uppercase; }
        .results-table .subject-col { text-align: left; font-weight: 500; padding-left: 5px; }
        .results-table .grade-a { background: #d4edda; font-weight: bold; }
        .results-table .grade-b { background: #cce5ff; font-weight: bold; }
        .results-table .grade-c { background: #fff3cd; font-weight: bold; }
        .results-table .grade-d { background: #ffeeba; }
        .results-table .grade-e { background: #f8d7da; }
        .results-table .grade-w { background: #f5c6cb; }
        .total-row { font-weight: bold; background: #e9ecef; }
        .total-row td { border-top: 2px solid #000; }
        .info-section { margin-bottom: 8px; border: 1px solid #000; }
        .info-row { display: flex; border-bottom: 1px solid #000; }
        .info-row:last-child { border-bottom: none; }
        .info-cell { padding: 4px 8px; border-right: 1px solid #000; flex: 1; }
        .info-cell:last-child { border-right: none; }
        .info-cell strong { font-weight: bold; margin-right: 5px; }
        .remark-section { margin-bottom: 8px; border: 1px solid #000; }
        .remark-header { background: #f0f0f0; padding: 3px 8px; font-weight: bold; border-bottom: 1px solid #000; }
        .remark-content { padding: 6px 8px; min-height: 30px; }
        .signature-section { display: flex; justify-content: space-between; margin-bottom: 8px; }
        .signature-box { width: 24%; border: 1px solid #000; padding: 4px; text-align: center; background: #f9f9f9; }
        .signature-line { border-bottom: 1px solid #000; margin-bottom: 3px; height: 30px; }
        .signature-label { font-size: 7px; font-weight: bold; text-transform: uppercase; }
        .bottom-section { display: flex; justify-content: space-between; align-items: flex-start; margin-top: 5px; }
        .stamp-section { width: 30%; border: 1px solid #000; padding: 8px; text-align: center; background: #f9f9f9; }
        .stamp-label { font-weight: bold; font-size: 9px; margin-bottom: 10px; }
        .stamp-space { height: 40px; border-bottom: 1px dashed #000; margin: 10px 0; }
        .stamp-text { font-style: italic; font-size: 8px; }
        .grading-reference { width: 35%; border: 1px solid #000; font-size: 7px; }
        .grading-reference th, .grading-reference td { padding: 2px 3px; border: 1px solid #000; text-align: center; }
        .grading-reference th { background: #f0f0f0; font-weight: bold; }
        .school-footer { text-align: center; margin-top: 8px; font-weight: bold; font-size: 10px; border-top: 1px solid #000; padding-top: 5px; }
        @media print {
            body { padding: 0; background: #fff; margin: 0; }
            .report-card { box-shadow: none; padding: 5mm; margin: 0; width: 100%; min-height: auto; }
            .watermark img { opacity: 0.05; }
            @page { margin: 0.3cm; size: A4; }
        }
    </style>
</head>
<body>
    <?php if ($logo): ?>
    <div class="watermark"><img src="<?php echo $logo; ?>" alt="School Logo"></div>
    <?php endif; ?>

    <div class="report-card">
        <div class="report-header">
            <?php if ($logo): ?><img src="<?php echo $logo; ?>" alt="School Logo" class="school-logo"><?php endif; ?>
            <div class="school-name"><?php echo $schoolName; ?></div>
            <div class="school-address"><?php echo strtoupper($data['schoolAddress']); ?></div>
            <div class="school-contact"><?php echo htmlspecialchars($data['schoolPhone']); ?></div>
            <div class="school-email"><?php echo htmlspecialchars($data['schoolEmail']); ?></div>
        </div>

        <div class="report-title">TERMINAL REPORT (<?php echo strtoupper($student['class']); ?>)</div>

        <div class="student-info-row">
            <div class="info-item"><span class="info-label">NAME:</span> <?php echo $studentName; ?></div>
            <div class="info-item"><span class="info-label">NO ON ROLL:</span> <?php echo $classSize; ?></div>
            <div class="info-item"><span class="info-label">TERM:</span> <?php echo $termFormatted; ?></div>
            <div class="info-item"><span class="info-label">CLASS:</span> <?php echo htmlspecialchars($student['class']); ?></div>
        </div>

        <div class="dates-row">
            <div class="date-item"><span class="info-label">CLOSING DATE:</span> <?php echo date('jS F Y', strtotime($data['closingDate'])); ?></div>
            <div class="date-item"><span class="info-label">ACADEMIC YEAR:</span> <?php echo htmlspecialchars($academic_year); ?></div>
            <div class="date-item"><span class="info-label">RE-OPENING:</span> <?php echo date('jS F Y', strtotime($data['reopeningDate'])); ?></div>
            <div class="date-item"><span class="info-label">TOTAL MARKS:</span> <strong><?php echo round($totalMarks, 0); ?></strong></div>
            <div class="date-item"><span class="info-label">POSITION:</span> <strong><?php echo $data['positionLabel'] . ' OUT OF ' . $classSize; ?></strong></div>
        </div>

        <table class="results-table">
            <thead>
                <tr>
                    <th width="25%">SUBJECT</th>
                    <th width="12%">CLASS SCORE (50%)</th>
                    <th width="12%">EXAMS SCORE (50%)</th>
                    <th width="12%">TOTAL (100%)</th>
                    <th width="8%">GRADE</th>
                    <th width="31%">REMARKS</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($subjects as $subject):
                    $score = $scores[$subject['subject_id']] ?? null;
                    $classScore = $score ? $score['class_score'] : 0;
                    $examScore  = $score ? $score['exam_score'] : 0;
                    $totalScore = $score ? $score['total_score'] : 0;
                    $grade      = $score ? $score['grade'] : 'W';
                    $remark     = $score ? $score['remark'] : 'N/A';
                ?>
                <tr>
                    <td class="subject-col"><?php echo strtoupper($subject['subject_name']); ?></td>
                    <td><?php echo round($classScore, 0); ?></td>
                    <td><?php echo round($examScore, 0); ?></td>
                    <td><strong><?php echo round($totalScore, 0); ?></strong></td>
                    <td class="grade-<?php echo strtolower($grade); ?>"><?php echo htmlspecialchars($grade); ?></td>
                    <td><?php echo htmlspecialchars($remark); ?></td>
                </tr>
                <?php endforeach; ?>
                <tr class="total-row">
                    <td colspan="3" style="text-align: right;"><strong>TOTAL</strong></td>
                    <td><strong><?php echo round($totalMarks, 0); ?></strong></td>
                    <td colspan="2"></td>
                </tr>
            </tbody>
        </table>

        <div class="info-section">
            <div class="info-row">
                <div class="info-cell"><strong>ATTENDANCE:</strong> <?php echo htmlspecialchars((string) $data['attendance']); ?> OUT OF <?php echo $classSize; ?></div>
                <div class="info-cell"><strong>PROMOTED TO:</strong> <?php echo htmlspecialchars((string) $data['promotedTo']); ?></div>
            </div>
            <div class="info-row">
                <div class="info-cell"><strong>CONDUCT:</strong> <?php echo htmlspecialchars((string) $data['conduct']); ?></div>
                <div class="info-cell"><strong>INTEREST:</strong> <?php echo htmlspecialchars((string) $data['interest']); ?></div>
            </div>
        </div>

        <div class="remark-section">
            <div class="remark-header">CLASS TEACHER'S REMARKS:</div>
            <div class="remark-content"><?php echo htmlspecialchars((string) $data['teacherRemark']); ?></div>
        </div>

        <div class="signature-section">
            <div class="signature-box">
                <?php if ($directorSig): ?><img src="<?php echo $directorSig; ?>" style="max-height:40px;max-width:140px;display:block;margin:0 auto 4px;"><?php else: ?><div class="signature-line"></div><?php endif; ?>
                <div class="signature-label">DIRECTOR'S SIGNATURE</div>
            </div>
            <div class="signature-box">
                <?php if ($headteacherSig): ?><img src="<?php echo $headteacherSig; ?>" style="max-height:40px;max-width:140px;display:block;margin:0 auto 4px;"><?php else: ?><div class="signature-line"></div><?php endif; ?>
                <div class="signature-label">HEAD TEACHER'S SIGNATURE</div>
            </div>
            <div class="signature-box">
                <?php if ($classTeacherSig): ?><img src="<?php echo $classTeacherSig; ?>" style="max-height:40px;max-width:140px;display:block;margin:0 auto 4px;"><?php else: ?><div class="signature-line"></div><?php endif; ?>
                <div class="signature-label">CLASS TEACHER'S SIGNATURE</div>
            </div>
            <div class="signature-box">
                <div style="height: 25px;"></div>
                <div class="signature-label">NOTES</div>
            </div>
        </div>

        <div class="bottom-section">
            <div class="stamp-section">
                <div class="stamp-label">SCHOOL'S STAMP</div>
                <div class="stamp-space"></div>
                <div class="stamp-text"><?php echo $schoolName; ?></div>
            </div>

            <table class="grading-reference">
                <thead><tr><th>MARKS</th><th>GRADE</th><th>REMARK</th></tr></thead>
                <tbody>
                    <?php foreach ($gradeBoundaries as $gb):
                        $gbMax = (int) $gb['max_score'];
                        $gbMin = (int) $gb['min_score'];
                        $gbRange = $gbMin === 0 ? $gbMax . ' &amp; Below' : $gbMax . ' - ' . $gbMin;
                    ?>
                    <tr><td><?php echo $gbRange; ?></td><td><?php echo htmlspecialchars($gb['grade']); ?></td><td><?php echo htmlspecialchars($gb['remark']); ?></td></tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <div class="school-footer"><?php echo $schoolName; ?></div>
    </div>
</body>
</html>
    <?php
    return (string) ob_get_clean();
}
