<?php
/**
 * Render Reviews [Author Pro] Block
 */

$block_id = isset($attributes['blockId']) ? $attributes['blockId'] : '';
if (empty($block_id)) {
    $block_id = 'author-pro-reviews-' . uniqid();
}

$anchor = isset($attributes['anchor']) ? $attributes['anchor'] : '';
$element_id = !empty($anchor) ? $anchor : $block_id;

$section_title = isset($attributes['sectionTitle']) ? $attributes['sectionTitle'] : 'What Readers Say';
$posts_to_show = isset($attributes['postsToShow']) ? $attributes['postsToShow'] : 3;
$order = isset($attributes['order']) ? $attributes['order'] : 'DESC';

$bg_color = isset($attributes['bgColor']) ? $attributes['bgColor'] : '#0f172a';
$card_bg_color = isset($attributes['cardBgColor']) ? $attributes['cardBgColor'] : 'rgba(255, 255, 255, 0.1)';
$accent_color = isset($attributes['accentColor']) ? $attributes['accentColor'] : '#ea580c';
$text_color = isset($attributes['textColor']) ? $attributes['textColor'] : '#d1d5db';
$author_color = isset($attributes['authorColor']) ? $attributes['authorColor'] : '#ffffff';

$padding_top = isset($attributes['paddingTop']) ? $attributes['paddingTop'] : 96;
$padding_bottom = isset($attributes['paddingBottom']) ? $attributes['paddingBottom'] : 96;

// Dynamic CSS
$custom_css = "
    #{$element_id} {
        background-color: {$bg_color} !important;
    }
    #{$element_id} .awt-review-card {
        background-color: {$card_bg_color} !important;
        border-color: rgba(255, 255, 255, 0.05);
    }
    #{$element_id} .awt-stars {
        color: {$accent_color} !important;
    }
    #{$element_id} .awt-review-text {
        color: {$text_color} !important;
    }
    #{$element_id} .awt-review-author {
        color: {$author_color} !important;
    }
";

$style = "padding-top: {$padding_top}px; padding-bottom: {$padding_bottom}px;";

$wrapper_attributes = get_block_wrapper_attributes([
    'class' => 'awt-author-pro-reviews relative',
    'style' => $style,
    'id'    => $element_id
]);

// Query Arguments
$args = array(
    'post_type'      => 'book_reviews',
    'posts_per_page' => $posts_to_show,
    'post_status'    => 'publish',
    'order'          => $order,
    'orderby'        => 'date',
);

$selected_book_id = isset($attributes['selectedBookId']) ? $attributes['selectedBookId'] : '0';

if ($selected_book_id && $selected_book_id !== '0') {
    $args['meta_query'] = array(
        array(
            'key'     => '_rswpbs_reviewed_book',
            'value'   => $selected_book_id,
            'compare' => '='
        )
    );
}

$reviews_query = new WP_Query($args);
?>

<?php if (!empty($custom_css)) : ?>
    <style><?php echo $custom_css; ?></style>
<?php endif; ?>

<section <?php echo $wrapper_attributes; ?>>
    <div class="awt-container text-center">
        <?php if ($section_title): ?>
            <h2 class="text-3xl font-serif font-bold mb-12 text-white"><?php echo esc_html($section_title); ?></h2>
        <?php endif; ?>

        <?php if ($reviews_query->have_posts()) : ?>
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 text-left">
                <?php while ($reviews_query->have_posts()) : $reviews_query->the_post(); 
                    $rating = get_post_meta(get_the_ID(), '_rswpbs_rating', true);
                    $rating = $rating ? intval($rating) : 5;
                    $reviewer_name = get_post_meta(get_the_ID(), '_rswpbs_reviewer_name', true) ?: get_the_title();
                    $content = get_the_content();
                    if (empty($content)) {
                        $content = get_the_excerpt();
                    }
                ?>
                    <div class="awt-review-card p-8 rounded-xl border backdrop-blur-sm">
                        <div class="awt-stars mb-4 text-sm">
                            <?php for ($i = 1; $i <= 5; $i++) {
                                echo ($i <= $rating) ? '★' : '☆';
                            } ?>
                        </div>
                        <p class="awt-review-text italic mb-6">"<?php echo wp_trim_words(esc_html(strip_tags($content)), 30, '...'); ?>"</p>
                        <div class="awt-review-author font-bold text-sm">- <?php echo esc_html($reviewer_name); ?></div>
                    </div>
                <?php endwhile; wp_reset_postdata(); ?>
            </div>
        <?php else : ?>
            <p class="text-gray-400 italic"><?php _e('No reviews found.', 'author-website-templates'); ?></p>
        <?php endif; ?>
    </div>
</section>
