<?php

namespace MovieEngine\Widgets;

use WP_Widget;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

/**
 * Recent Reviews Widget
 */
class RecentReviews extends WP_Widget {

    public function __construct() {
        parent::__construct(
            'movie_engine_recent_reviews',
            esc_html__('MovieEngine: Recent Reviews', 'cine-movie-engine'),
            ['description' => esc_html__('Display a list of the latest user reviews.', 'cine-movie-engine')]
        );
    }

    public function widget($args, $instance) {
        $title = ! empty($instance['title']) ? $instance['title'] : esc_html__('Community Reviews', 'cine-movie-engine');
        $limit = ! empty($instance['limit']) ? intval($instance['limit']) : 5;

        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        echo $args['before_widget'];
        if ($title) {
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            echo $args['before_title'] . apply_filters('widget_title', $title) . $args['after_title'];
        }

        $comments = get_comments([
            'number'  => $limit,
            'status'  => 'approve',
            'post_type' => ['movie_engine_movie', 'movie_engine_series', 'movie_engine_episode'],
        ]);

        if (! empty($comments)) : ?>
            <div class="movie-engine-reviews-widget">
                <?php foreach ($comments as $comment) :
                    $rating = get_comment_meta($comment->comment_ID, 'movie_engine_rating', true);
                    $post_title = get_the_title($comment->comment_post_ID);
                ?>
                    <div class="me-review-widget-item">
                        <div class="me-review-header">
                            <span class="me-reviewer"><?php echo esc_html($comment->comment_author); ?></span>
                            <?php if ($rating) : ?>
                                <span class="me-review-rating">
                                    <?php for ($i = 1; $i <= 5; $i++): ?>
                                        <i class="fa-solid fa-star <?php echo esc_attr($i <= $rating ? 'active' : ''); ?>"></i>
                                    <?php endfor; ?>
                                </span>
                            <?php endif; ?>
                        </div>
                        <a href="<?php echo esc_url(get_comment_link($comment)); ?>" class="me-review-content-link">
                            <p class="me-review-excerpt"><?php echo esc_html(wp_trim_words($comment->comment_content, 10)); ?></p>
                            <span class="me-review-post-title">on <?php echo esc_html($post_title); ?></span>
                        </a>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php else : ?>
            <p><?php esc_html_e('No reviews yet.', 'cine-movie-engine'); ?></p>
        <?php endif;

        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        echo $args['after_widget'];
    }

    public function form($instance) {
        $title = ! empty($instance['title']) ? $instance['title'] : esc_html__('Community Reviews', 'cine-movie-engine');
        $limit = ! empty($instance['limit']) ? intval($instance['limit']) : 5;
        ?>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'cine-movie-engine'); ?></label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
        </p>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('limit')); ?>"><?php esc_html_e('Limit:', 'cine-movie-engine'); ?></label>
            <input class="tiny-text" id="<?php echo esc_attr($this->get_field_id('limit')); ?>" name="<?php echo esc_attr($this->get_field_name('limit')); ?>" type="number" step="1" min="1" value="<?php echo esc_attr($limit); ?>" size="3">
        </p>
<?php
    }

    public function update($new_instance, $old_instance) {
        $instance = [];
        $instance['title'] = (! empty($new_instance['title'])) ? wp_strip_all_tags($new_instance['title']) : '';
        $instance['limit'] = (! empty($new_instance['limit'])) ? intval($new_instance['limit']) : 5;
        return $instance;
    }
}
