<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Fake product rating (deterministic per product).
 * Returns a float between 4.2 and 5.0 (step 0.1).
 */
if ( ! function_exists( 'nikan_fake_product_rating' ) ) {
    function nikan_fake_product_rating( $product_id ) : float {
        $product_id = absint( $product_id );
        if ( $product_id <= 0 ) {
            return 4.8;
        }
        $min = 42; // 4.2
        $max = 50; // 5.0
        $hash = abs( (int) crc32( (string) $product_id ) );
        $val_i = $min + ( $hash % ( $max - $min + 1 ) );
        return round( $val_i / 10, 1 );
    }
}

if ( ! function_exists( 'nikan_render_fake_rating' ) ) {
    function nikan_render_fake_rating( $product_id, string $class = '' ) : void {
        $rating = nikan_fake_product_rating( $product_id );
        $class_attr = $class ? ' ' . esc_attr( $class ) : '';
        echo '<span class="nikan-fake-rating' . $class_attr . '">';
        echo '<span class="nikan-fake-rating-star" aria-hidden="true">★</span> ';
        echo '<span class="nikan-fake-rating-num">' . esc_html( number_format( $rating, 1 ) ) . '</span>';
        echo '</span>';
    }
}
