<?php
/**
 * ITSWEBER CMS – Default Theme: Rating Highlight Section Template
 * Page Builder section template (3.2.0)
 *
 * Variables: $data (array), $section (DB row), $sectionId (string)
 *
 * @author  Benny Weber
 * @copyright ITSWEBER, www.itsweber.de
 */

$title            = $data['title'] ?? '';
$subtitle         = $data['subtitle'] ?? '';
$sourceName       = $data['source_name'] ?? '';
$score            = $data['score'] ?? '';
$maxScore         = $data['max_score'] ?? '5';
$reviewCount      = $data['review_count'] ?? '';
$reviewLabel      = $data['review_label'] ?? '';
$summaryText      = $data['summary_text'] ?? '';
$summaryText2     = $data['summary_text_2'] ?? '';
$asOfDate         = $data['as_of_date'] ?? '';
$linkUrl          = $data['link_url'] ?? '';
$linkLabel        = $data['link_label'] ?? '';
// Calculate filled and empty stars based on score/max_score
$numScore = (float) str_replace(',', '.', $score);
$numMax   = (float) str_replace(',', '.', $maxScore);
$filledStars = $numMax > 0 ? floor($numScore / $numMax * 5) : 0;
$filledStars = max(0, min(5, $filledStars));
?>
<section class="<?= build_section_classes('rating-highlight', $data) ?>" id="<?= e($sectionId) ?>">
    <div class="container">
        <?php if ($title !== '' || $subtitle !== ''): ?>
        <div class="rating-section-header reveal">
            <?php if ($subtitle !== ''): ?>
            <div class="rating-section-subtitle"><?= e($subtitle) ?></div>
            <?php endif; ?>
            <?php if ($title !== ''): ?>
            <h2 class="rating-section-title"><?= e($title) ?></h2>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <div class="rating-card reveal">
            <!-- Score Side (Left) -->
            <div class="rating-score-side">
                <div class="rating-score-number"><?= e($score) ?></div>

                <!-- Star visualization -->
                <div class="rating-stars">
                    <?php for ($i = 0; $i < 5; $i++): ?>
                        <span class="rating-star">
                            <?= ($i < $filledStars) ? '★' : '☆' ?>
                        </span>
                    <?php endfor; ?>
                </div>

                <!-- Review count + source -->
                <?php if ($reviewCount !== '' || $sourceName !== ''): ?>
                <div class="rating-count">
                    <?php if ($reviewCount !== ''): ?>
                        <?= e($reviewCount) ?> <?= e($reviewLabel) ?>
                    <?php endif; ?>
                </div>
                <?php endif; ?>

                <?php if ($sourceName !== ''): ?>
                <div class="rating-source"><?= e($sourceName) ?></div>
                <?php endif; ?>
            </div>

            <!-- Body Side (Right) -->
            <div class="rating-body-side">
                <!-- Summary text(s) -->
                <?php if ($summaryText !== ''): ?>
                <div class="rating-summary">
                    <?= nl2br(e($summaryText)) ?>
                    <?php if ($sourceName !== ''): ?>
                    <div class="rating-summary-source"><?= e($sourceName) ?></div>
                    <?php endif; ?>
                </div>
                <?php endif; ?>

                <?php if ($summaryText2 !== ''): ?>
                <div class="rating-summary">
                    <?= nl2br(e($summaryText2)) ?>
                    <?php if ($sourceName !== ''): ?>
                    <div class="rating-summary-source"><?= e($sourceName) ?></div>
                    <?php endif; ?>
                </div>
                <?php endif; ?>

                <!-- Date -->
                <?php if ($asOfDate !== ''): ?>
                <div class="rating-date"><?= e($asOfDate) ?></div>
                <?php endif; ?>

                <!-- Link -->
                <?php if ($linkUrl !== ''): ?>
                <a href="<?= e($linkUrl) ?>" class="rating-link">
                    <?= e($linkLabel) ?> →
                </a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</section>
