<?php
/**
 * Template View: Testimonial Card
 *
 * Customer testimonial display.
 *
 * @package    BoostBot
 * @subpackage Templates/Views
 * @since      1.6.0
 *
 * @var string $quote  The testimonial quote.
 * @var string $author Author name.
 * @var string $role   Author role/title (optional).
 * @var string $image  Author image URL (optional).
 * @var float  $rating Rating 1-5 (optional).
 */

// phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template-scope variables set by the includer, not true globals.

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

// Validate required data
if ( empty( $quote ) || empty( $author ) ) {
    return;
}

$has_rating = ! empty( $rating ) && $rating > 0;

// Generate initials if no image
$initials = '';
if ( empty( $image ) ) {
    $name_parts = explode( ' ', $author );
    $initials = mb_substr( $name_parts[0], 0, 1 );
    if ( count( $name_parts ) > 1 ) {
        $initials .= mb_substr( end( $name_parts ), 0, 1 );
    }
}
?>

<div class="bb-testimonial-card">
    <div class="bb-testimonial-quote">
        <span class="bb-quote-mark">"</span>
        <p class="bb-quote-text"><?php echo esc_html( $quote ); ?></p>
    </div>
    
    <?php if ( $has_rating ) : ?>
        <div class="bb-testimonial-rating">
            <?php for ( $i = 1; $i <= 5; $i++ ) : ?>
                <span class="bb-star <?php echo $i <= $rating ? 'bb-star-filled' : 'bb-star-empty'; ?>">★</span>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
    
    <div class="bb-testimonial-author">
        <div class="bb-author-avatar">
            <?php if ( ! empty( $image ) ) : ?>
                <img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $author ); ?>">
            <?php else : ?>
                <span class="bb-author-initials"><?php echo esc_html( $initials ); ?></span>
            <?php endif; ?>
        </div>
        <div class="bb-author-info">
            <div class="bb-author-name"><?php echo esc_html( $author ); ?></div>
            <?php if ( ! empty( $role ) ) : ?>
                <div class="bb-author-role"><?php echo esc_html( $role ); ?></div>
            <?php endif; ?>
        </div>
    </div>
</div>
