<?php

namespace MovieEngine\Widgets;

use WP_Widget;
use WP_Query;

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

/**
 * Random Movie Widget
 */
class RandomMovie extends WP_Widget {

    public function __construct() {
        parent::__construct(
            'movie_engine_random_movie',
            esc_html__('MovieEngine: Surprise Me', 'cine-movie-engine'),
            ['description' => esc_html__('Displays a random movie/series with a refresh button.', 'cine-movie-engine')]
        );
    }

    public function widget($args, $instance) {
        $title = ! empty($instance['title']) ? $instance['title'] : esc_html__('Don\'t know what to watch?', 'cine-movie-engine');

        // 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'];
        }

        $query_args = [
            'post_type'      => ['movie_engine_movie', 'movie_engine_series'],
            'posts_per_page' => 1,
            'orderby'        => 'rand',
        ];

        $random_query = new WP_Query($query_args);

        if ($random_query->have_posts()) : ?>
            <div class="movie-engine-random-widget">
                <?php while ($random_query->have_posts()) : $random_query->the_post(); ?>
                    <div class="me-random-card">
                        <div class="me-random-poster">
                            <?php if (has_post_thumbnail()) : ?>
                                <?php the_post_thumbnail('medium'); ?>
                            <?php endif; ?>
                            <div class="me-random-overlay">
                                <a href="<?php the_permalink(); ?>" class="me-btn me-btn-primary me-btn-sm"><?php esc_html_e('Watch Now', 'cine-movie-engine'); ?></a>
                            </div>
                        </div>
                        <div class="me-random-info">
                            <h4 class="me-random-title"><?php the_title(); ?></h4>
                            <p class="me-random-excerpt"><?php echo esc_html(wp_trim_words(get_the_excerpt(), 15)); ?></p>
                        </div>
                    </div>
                <?php endwhile;
                wp_reset_postdata(); ?>

                <button type="button" class="me-random-refresh" onclick="window.location.reload();">
                    <i class="fa-solid fa-arrows-rotate"></i> <?php esc_html_e('Try Another One', 'cine-movie-engine'); ?>
                </button>
            </div>
        <?php else : ?>
            <p><?php esc_html_e('No content found.', '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__('Don\'t know what to watch?', 'cine-movie-engine');
        ?>
        <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>
<?php
    }

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