<?php

namespace MovieEngine\Widgets;

use WP_Widget;

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

/**
 * User Watchlist Widget
 */
class WatchlistWidget extends WP_Widget {

    public function __construct() {
        parent::__construct(
            'movie_engine_watchlist_widget',
            esc_html__('MovieEngine: My Watchlist', 'cine-movie-engine'),
            ['description' => esc_html__('Displays the logged-in user\'s watchlist.', 'cine-movie-engine')]
        );
    }

    public function widget($args, $instance) {
        if (! is_user_logged_in()) {
            return;
        }

        $title = ! empty($instance['title']) ? $instance['title'] : esc_html__('My Watchlist', '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'];
        }

        $user_id = get_current_user_id();
        $watchlist = get_user_meta($user_id, '_movie_engine_watchlist', true);

        if (! empty($watchlist) && is_array($watchlist)) {
            $watchlist = array_reverse($watchlist);
            $watchlist = array_slice($watchlist, 0, $limit);
?>
            <div class="movie-engine-watchlist-widget">
                <?php foreach ($watchlist as $post_id) :
                    $post = get_post($post_id);
                    if (!$post) continue;
                ?>
                    <div class="me-widget-item">
                        <a href="<?php echo esc_url(get_permalink($post_id)); ?>" class="me-widget-link">
                            <div class="me-widget-info">
                                <span class="me-widget-title"><?php echo esc_html(get_the_title($post_id)); ?></span>
                                <span class="me-widget-meta"><?php echo esc_html(get_post_type_object(get_post_type($post_id))->labels->singular_name); ?></span>
                            </div>
                        </a>
                    </div>
                <?php endforeach; ?>
                <div class="me-widget-footer">
                    <a href="<?php echo esc_url(\MovieEngine\Payments\Membership::get_dashboard_url('watchlist')); ?>" class="me-view-all">
                        <?php esc_html_e('View Full Watchlist', 'cine-movie-engine'); ?>
                    </a>
                </div>
            </div>
        <?php
        } else {
            echo '<p>' . esc_html__('Your watchlist is empty.', 'cine-movie-engine') . '</p>';
        }

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

    public function form($instance) {
        $title = ! empty($instance['title']) ? $instance['title'] : esc_html__('My Watchlist', '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;
    }
}
