<?php
define('DEFAULT_TEMPLATE_PATH', '/local/templates/.default');
define('SITE_TEMPLATE_PATH', '/local/templates/EraSeptik');

function debug($data) {
    echo "<pre>" . print_r($data, true) . "</pre>";
}

function getAverageRating($reviews): float
{
    $totalRating = 0;
    $count = 0;

    if (!$reviews) return 0;

    foreach ($reviews as $review) {
        if (!empty($review['SUB_VALUES']['REV_RATING']['VALUE'])) {
            $rating = (float)$review['SUB_VALUES']['REV_RATING']['VALUE'];
            $totalRating += $rating;
            $count++;
        }
    }

    return $count > 0 ? round($totalRating / $count, 1) : 0;
}

function getReviewWordForm($reviews): string {

    if (!$reviews) return 'отзыв';

    $count = count($reviews);

    $mod10 = $count % 10;
    $mod100 = $count % 100;

    if ($mod10 == 1 && $mod100 != 11) {
        return 'отзыв';
    } elseif (in_array($mod10, [2, 3, 4]) && !in_array($mod100, [12, 13, 14])) {
        return 'отзыва';
    } else {
        return 'отзывов';
    }
}