<?php

/**
 * The template for displaying dashboard main content
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/dashboard/main.php.
 *
 * HOWEVER, on occasion MovieEngine will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @note Please do not delete our CSS classes when you customize, as it may break functionality.
 * @package Movie_Engine
 * @subpackage Movie_Engine/templates/dashboard
 * @version 1.0.0
 * @since 1.0.0
 */

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

use MovieEngine\Frontend\Users\Devices;

$movie_engine_user = $args['user'] ?? wp_get_current_user();
$movie_engine_is_premium = $args['is_premium'] ?? false;
$movie_engine_stats = $args['stats'] ?? [];
$movie_engine_watchlist_count = $args['watchlist_count'] ?? 0;

if (isset($_POST['movie_engine_action']) && wp_verify_nonce(isset($_POST['_wpnonce']) ? sanitize_text_field(wp_unslash($_POST['_wpnonce'])) : '', 'movie_engine_dashboard_action')) {
    $movie_engine_dashboard_action = sanitize_text_field(wp_unslash($_POST['movie_engine_action']));
    if ($movie_engine_dashboard_action === 'logout_device' && isset($_POST['token'])) {
        $movie_engine_token_to_remove = sanitize_text_field(wp_unslash($_POST['token']));
        Devices::logout_device($movie_engine_user->ID, $movie_engine_token_to_remove);
        wp_safe_redirect(remove_query_arg(['movie_engine_action', 'token']));
        exit;
    }

    if ($movie_engine_dashboard_action === 'logout_all_others') {
        Devices::logout_all_others($movie_engine_user->ID);
        wp_safe_redirect(remove_query_arg(['movie_engine_action']));
        exit;
    }
}

$movie_engine_custom_avatar = get_user_meta($movie_engine_user->ID, 'movie_engine_avatar_url', true);
$movie_engine_avatar_url = $movie_engine_custom_avatar ? $movie_engine_custom_avatar : get_avatar_url($movie_engine_user->ID, ['size' => 150]);
$movie_engine_active_plan = get_user_meta($movie_engine_user->ID, '_movie_engine_active_plan', true);
$movie_engine_plan_name = $movie_engine_active_plan ? get_the_title($movie_engine_active_plan) : esc_html__('Free Plan', 'cine-movie-engine');

$movie_engine_view = isset($_GET['movie_engine_view']) ? sanitize_text_field(wp_unslash($_GET['movie_engine_view'])) : 'dashboard';

$movie_engine_design_settings = get_option('movie_engine_design_settings', []);
$movie_engine_pricing_page_id = isset($movie_engine_design_settings['pricing_page']) ? $movie_engine_design_settings['pricing_page'] : '';
$movie_engine_pricing_url = $movie_engine_pricing_page_id ? get_permalink($movie_engine_pricing_page_id) : home_url('/pricing');

?>

<div class="movie-engine-container-fluid">
    <div class="movie-engine-dashboard-wrapper">
        <div class="movie-engine-dashboard-header">
            <div class="movie-engine-user-identity">
                <div class="movie-engine-avatar">
                    <img src="<?php echo esc_url($movie_engine_avatar_url); ?>" alt="<?php echo esc_attr($movie_engine_user->display_name); ?>">
                </div>
                <div class="movie-engine-user-details">
                    <h2 class="movie-engine-name"><?php echo esc_html($movie_engine_user->display_name); ?></h2>
                    <div class="movie-engine-email"><?php echo esc_html($movie_engine_user->user_email); ?></div>
                    <div class="movie-engine-username"><?php echo esc_html($movie_engine_user->user_login); ?></div>
                </div>
            </div>
            <div class="movie-engine-header-actions">
                <div class="movie-engine-membership-status">
                    <span class="label"><?php esc_html_e('Active Membership:', 'cine-movie-engine'); ?></span>
                    <span class="value"><?php echo esc_html($movie_engine_plan_name); ?></span>
                </div>
                <a href="<?php echo esc_url($movie_engine_pricing_url); ?>" class="movie-engine-btn-upgrade">
                    <span class="fa-solid fa-cloud-arrow-up"></span> <?php esc_html_e('Upgrade', 'cine-movie-engine'); ?>
                </a>
                <a href="?movie_engine_view=settings" class="movie-engine-btn-settings">
                    <span class="fa-sharp fa-solid fa-gear"></span>
                </a>
                <a href="<?php echo esc_url(wp_logout_url(home_url())); ?>" class="movie-engine-btn-logout-header" title="<?php esc_attr_e('Logout', 'cine-movie-engine'); ?>">
                    <span class="fa-sharp fa-solid fa-right-from-bracket"></span>
                </a>
            </div>
        </div>

        <div class="movie-engine-dashboard-body">
            <div class="movie-engine-dashboard-sidebar">
                <nav class="movie-engine-nav-menu">
                    <a href="?movie_engine_view=dashboard" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'dashboard' || !isset($_GET['movie_engine_view']) ? 'active' : ''); ?>">
                        <span class="fa-solid fa-gauge"></span> <?php esc_html_e('Dashboard', 'cine-movie-engine'); ?>
                    </a>
                    <a href="?movie_engine_view=history" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'history' ? 'active' : ''); ?>">
                        <span class="fa-solid fa-clock"></span> <?php esc_html_e('Watch History', 'cine-movie-engine'); ?>
                    </a>
                    <?php if (\MovieEngine\Frontend\Users\Devices::is_device_management_enabled()) : ?>
                        <a href="?movie_engine_view=devices" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'devices' ? 'active' : ''); ?>">
                            <span class="fa-solid fa-desktop"></span> <?php esc_html_e('Device Limit', 'cine-movie-engine'); ?>
                        </a>
                    <?php endif; ?>
                    <a href="?movie_engine_view=playlists" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'playlists' ? 'active' : ''); ?>">
                        <span class="fa-solid fa-list"></span> <?php esc_html_e('Playlist', 'cine-movie-engine'); ?>
                    </a>
                    <a href="?movie_engine_view=watchlist" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'watchlist' ? 'active' : ''); ?>">
                        <span class="fa-solid fa-eye"></span> <?php esc_html_e('Watch List', 'cine-movie-engine'); ?>
                    </a>
                    <a href="?movie_engine_view=liked" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'liked' ? 'active' : ''); ?>">
                        <span class="fa-solid fa-heart"></span> <?php esc_html_e('Liked Content', 'cine-movie-engine'); ?>
                    </a>
                    <a href="?movie_engine_view=notifications" class="movie-engine-nav-item me-nav-item-flex <?php echo esc_attr($movie_engine_view === 'notifications' ? 'active' : ''); ?>">
                        <span><span class="fa-solid fa-bell"></span> <?php esc_html_e('Notification', 'cine-movie-engine'); ?></span>
                        <?php
                        $movie_engine_unread_count = \MovieEngine\Frontend\Users\Notifications::get_unread_count($movie_engine_user->ID);
                        if ($movie_engine_unread_count > 0) :
                        ?>
                            <span class="movie-engine-notif-badge me-notif-badge"><?php echo esc_html(($movie_engine_unread_count > 99) ? '99+' : $movie_engine_unread_count); ?></span>
                        <?php endif; ?>
                    </a>
                    <?php $movie_engine_is_pro = apply_filters('movie_engine_is_pro', false); ?>
                    <a href="?movie_engine_view=rentals" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'rentals' ? 'active' : ''); ?>">
                        <span><span class="fa-solid fa-store"></span> <?php esc_html_e('Rental Collection', 'cine-movie-engine'); ?></span>
                        <?php if (!$movie_engine_is_pro) : ?>
                            <span class="me-pro-badge"><?php esc_html_e('PRO', 'cine-movie-engine'); ?></span>
                        <?php endif; ?>
                    </a>
                    <a href="?movie_engine_view=membership" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'membership' ? 'active' : ''); ?>">
                        <span><span class="fa-solid fa-crown"></span> <?php esc_html_e('Membership', 'cine-movie-engine'); ?></span>
                        <?php if (!$movie_engine_is_pro) : ?>
                            <span class="me-pro-badge"><?php esc_html_e('PRO', 'cine-movie-engine'); ?></span>
                        <?php endif; ?>
                    </a>
                    <a href="?movie_engine_view=referrals" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'referrals' ? 'active' : ''); ?>">
                        <span><span class="fa-solid fa-people-line"></span> <?php esc_html_e('Refer & Earn', 'cine-movie-engine'); ?></span>
                        <?php if (!$movie_engine_is_pro) : ?>
                            <span class="me-pro-badge"><?php esc_html_e('PRO', 'cine-movie-engine'); ?></span>
                        <?php endif; ?>
                    </a>
                    <a href="?movie_engine_view=pay_per_view" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'pay_per_view' ? 'active' : ''); ?>">
                        <span><span class="fa-solid fa-ticket"></span> <?php esc_html_e('Pay Per View', 'cine-movie-engine'); ?></span>
                        <?php if (!$movie_engine_is_pro) : ?>
                            <span class="me-pro-badge"><?php esc_html_e('PRO', 'cine-movie-engine'); ?></span>
                        <?php endif; ?>
                    </a>
                    <a href="?movie_engine_view=transactions" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'transactions' ? 'active' : ''); ?>">
                        <span><span class="fa-solid fa-receipt"></span> <?php esc_html_e('Transactions', 'cine-movie-engine'); ?></span>
                        <?php if (!$movie_engine_is_pro) : ?>
                            <span class="me-pro-badge"><?php esc_html_e('PRO', 'cine-movie-engine'); ?></span>
                        <?php endif; ?>
                    </a>
                    <a href="?movie_engine_view=settings" class="movie-engine-nav-item <?php echo esc_attr($movie_engine_view === 'settings' ? 'active' : ''); ?>">
                        <span class="fa-solid fa-user"></span> <?php esc_html_e('Account Settings', 'cine-movie-engine'); ?>
                    </a>
                </nav>
            </div>
            <div class="movie-engine-dashboard-content">
                <?php if ($movie_engine_view !== 'referrals' && $movie_engine_view !== 'devices') : ?>
                    <div class="movie-engine-dashboard-stats-grid">
                        <div class="movie-engine-stat-card">
                            <div class="movie-engine-stat-icon movies"><span class="fa-solid fa-film"></span></div>
                            <div class="movie-engine-stat-data">
                                <span class="movie-engine-stat-value"><?php echo esc_html(number_format_i18n($movie_engine_stats['movies'] ?? 0)); ?></span>
                                <span class="movie-engine-stat-label"><?php esc_html_e('Movies Watched', 'cine-movie-engine'); ?></span>
                            </div>
                        </div>
                        <div class="movie-engine-stat-card">
                            <div class="movie-engine-stat-icon episodes"><span class="fa-solid fa-video"></span></div>
                            <div class="movie-engine-stat-data">
                                <span class="movie-engine-stat-value"><?php echo esc_html(number_format_i18n($movie_engine_stats['episodes'] ?? 0)); ?></span>
                                <span class="movie-engine-stat-label"><?php esc_html_e('Episodes Watched', 'cine-movie-engine'); ?></span>
                            </div>
                        </div>
                        <div class="movie-engine-stat-card">
                            <div class="movie-engine-stat-icon watchtime"><span class="fa-solid fa-clock"></span></div>
                            <div class="movie-engine-stat-data">
                                <span class="movie-engine-stat-value"><?php echo esc_html(number_format_i18n($movie_engine_stats['watchtime_hours'] ?? 0)); ?>h</span>
                                <span class="movie-engine-stat-label"><?php esc_html_e('Total Watch Time', 'cine-movie-engine'); ?></span>
                            </div>
                        </div>
                        <div class="movie-engine-stat-card">
                            <div class="movie-engine-stat-icon watchlist"><span class="fa-solid fa-eye"></span></div>
                            <div class="movie-engine-stat-data">
                                <span class="movie-engine-stat-value"><?php echo esc_html(number_format_i18n($movie_engine_watchlist_count ?? 0)); ?></span>
                                <span class="movie-engine-stat-label"><?php esc_html_e('In Watchlist', 'cine-movie-engine'); ?></span>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
                <?php if ($movie_engine_view === 'dashboard' || !isset($_GET['movie_engine_view'])) : ?>
                    <div class="movie-engine-dashboard-recent-history me-dashboard-recent">
                        <?php echo \MovieEngine\Utils\Helpers::kses_post(do_shortcode('[movie_engine_history limit="8" title="' . esc_attr__('Continue Watching', 'cine-movie-engine') . '" view_all_link="?movie_engine_view=history"]')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML ?>
                    </div>
                    <div class="movie-engine-dashboard-watchlist me-dashboard-section">
                        <?php echo \MovieEngine\Utils\Helpers::kses_post(do_shortcode('[movie_engine_watchlist limit="8" title="' . esc_attr__('My Watch List', 'cine-movie-engine') . '" view_all_link="?movie_engine_view=watchlist" pagination="false"]')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML ?>
                    </div>
                    <div class="movie-engine-dashboard-liked me-dashboard-section">
                        <?php echo \MovieEngine\Utils\Helpers::kses_post(do_shortcode('[movie_engine_liked limit="8" title="' . esc_attr__('Liked Content', 'cine-movie-engine') . '" view_all_link="?movie_engine_view=liked" pagination="false"]')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- kses_post sanitizes HTML ?>
                    </div>
                <?php endif; ?>
                <?php if ($movie_engine_view === 'devices') :
                    if (!\MovieEngine\Frontend\Users\Devices::is_device_management_enabled()) {
                        echo '<div class="movie-engine-view-header"><h3>' . esc_html__('Device Management', 'cine-movie-engine') . '</h3></div><div class="movie-engine-alert me-alert-warning">' . esc_html__('Device management is currently disabled.', 'cine-movie-engine') . '</div>';
                    } else {
                        $movie_engine_devices_list = \MovieEngine\Frontend\Users\Devices::get_user_devices($movie_engine_user->ID);
                        if (empty($movie_engine_devices_list)) {
                            $movie_engine_devices_list = [];
                        }
                        $movie_engine_device_count = count($movie_engine_devices_list);
                        $movie_engine_device_limit = \MovieEngine\Frontend\Users\Devices::get_device_limit();
                        $movie_engine_usage_percent = ($movie_engine_device_limit > 0) ? min(100, round(($movie_engine_device_count / $movie_engine_device_limit) * 100)) : 0;
                ?>
                        <div class="movie-engine-view-header">
                            <h3><span class="fa-solid fa-shield-halved"></span> <?php esc_html_e('Device Management', 'cine-movie-engine'); ?></h3>
                        </div>
                        <div class="movie-engine-device-stats">
                            <div class="movie-engine-stat-row">
                                <div class="movie-engine-plan-info">
                                    <h4><?php echo esc_html($movie_engine_plan_name); ?></h4>
                                    <span><?php printf(
                                                /* translators: 1: current device count, 2: max device limit */
                                                esc_html__('%1$d of %2$d devices used', 'cine-movie-engine'),
                                                absint($movie_engine_device_count),
                                                absint($movie_engine_device_limit)
                                            ); ?></span>
                                </div>
                                <div class="movie-engine-device-count-badge">
                                    <?php echo esc_html($movie_engine_device_count . '/' . $movie_engine_device_limit . ' ');
                                    esc_html_e('Devices', 'cine-movie-engine'); ?>
                                </div>
                            </div>

                            <div class="movie-engine-usage-bar-wrapper">
                                <span class="label"><?php esc_html_e('Usage', 'cine-movie-engine'); ?></span>
                                <div class="movie-engine-usage-track">
                                    <div class="movie-engine-usage-fill" style="width: <?php echo esc_attr($movie_engine_usage_percent); ?>%;"></div>
                                </div>
                                <span class="percentage"><?php echo esc_html($movie_engine_usage_percent); ?>%</span>
                            </div>

                            <?php if ($movie_engine_device_count >= $movie_engine_device_limit) : ?>
                                <div class="movie-engine-alert-warning">
                                    <span class="fa-solid fa-triangle-exclamation"></span>
                                    <?php esc_html_e('You have reached your device limit. Please log out of a device to add a new one.', 'cine-movie-engine'); ?>
                                </div>
                                <?php if (!apply_filters('movie_engine_is_pro', false)) : ?>
                                    <div class="me-device-upgrade-banner">
                                        <span class="fa-solid fa-bolt"></span>
                                        <span><?php esc_html_e('Want more devices? Upgrade to Pro for unlimited device connections.', 'cine-movie-engine'); ?></span>
                                        <a href="<?php echo esc_url(MovieEngine\Admin\Upgrade::get_pro_url('pricing')); ?>" target="_blank" class="me-upgrade-link"><?php esc_html_e('Upgrade Now', 'cine-movie-engine'); ?></a>
                                    </div>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>

                        <div class="movie-engine-devices-list-section">
                            <div class="section-header">
                                <h4><?php esc_html_e('Active Devices', 'cine-movie-engine'); ?></h4>
                                <?php if ($movie_engine_device_count > 1) : ?>
                                    <form method="post" onsubmit="return confirm('<?php esc_html_e('Are you sure you want to logout all other devices?', 'cine-movie-engine'); ?>');">
                                        <?php wp_nonce_field('movie_engine_dashboard_action'); ?>
                                        <input type="hidden" name="movie_engine_action" value="logout_all_others">
                                        <button type="submit" class="movie-engine-btn-logout-all text-danger"><span class="fa-solid fa-right-from-bracket"></span> <?php esc_html_e('Logout All', 'cine-movie-engine'); ?></button>
                                    </form>
                                <?php endif; ?>
                            </div>

                            <div class="movie-engine-device-list">
                                <?php if (!empty($movie_engine_devices_list)) : foreach ($movie_engine_devices_list as $movie_engine_token => $movie_engine_device) : ?>
                                        <div class="movie-engine-device-item <?php echo esc_attr($movie_engine_device['is_current']) ? 'active' : ''; ?>">
                                            <div class="device-icon">
                                                <span class="fa-solid fa-<?php echo stripos($movie_engine_device['name'], 'apple-whole') !== false || stripos($movie_engine_device['name'], 'mobile') !== false ? 'mobile-screen' : 'desktop'; ?>"></span>
                                            </div>
                                            <div class="device-info">
                                                <div class="device-name">
                                                    <?php echo esc_html($movie_engine_device['name']); ?>
                                                    <?php if ($movie_engine_device['is_current']) : ?>
                                                        <span class="badge-current"><?php esc_html_e('Current Device', 'cine-movie-engine'); ?></span>
                                                    <?php endif; ?>
                                                </div>
                                                <div class="device-time">
                                                    <?php
                                                    echo esc_html__('Logged in:', 'cine-movie-engine') . ' ' . esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $movie_engine_device['last_active']));
                                                    ?>
                                                </div>
                                                <div class="device-meta me-device-meta">IP: <?php echo esc_html($movie_engine_device['ip']); ?></div>
                                            </div>
                                            <?php if (!$movie_engine_device['is_current']) : ?>
                                                <form method="post">
                                                    <?php wp_nonce_field('movie_engine_dashboard_action'); ?>
                                                    <input type="hidden" name="movie_engine_action" value="logout_device">
                                                    <input type="hidden" name="token" value="<?php echo esc_attr($movie_engine_device['token']); ?>">
                                                    <button type="submit" class="movie-engine-btn-logout"><?php esc_html_e('Log out', 'cine-movie-engine'); ?></button>
                                                </form>
                                            <?php endif; ?>
                                        </div>
                                    <?php endforeach;
                                else: ?>
                                    <div class="movie-engine-device-item">
                                        <p><?php esc_html_e('No active devices found (Session error).', 'cine-movie-engine'); ?></p>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php } ?>

                <?php elseif ($movie_engine_view === 'playlists') : ?>
                    <?php echo do_shortcode('[movie_engine_playlists]'); ?>

                <?php elseif ($movie_engine_view === 'watchlist') : ?>
                    <div class="movie-engine-view-header">
                        <h3><?php esc_html_e('My Watch List', 'cine-movie-engine'); ?></h3>
                    </div>
                    <?php echo do_shortcode('[movie_engine_watchlist]'); ?>

                <?php elseif ($movie_engine_view === 'liked') : ?>
                    <div class="movie-engine-view-header">
                        <h3><span class="fa-solid fa-heart"></span> <?php esc_html_e('Liked Content', 'cine-movie-engine'); ?></h3>
                    </div>
                    <?php echo do_shortcode('[movie_engine_liked]'); ?>

                <?php elseif ($movie_engine_view === 'notifications') :
                    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                    $movie_engine_notif_tab_val = isset($_GET['notif_tab']) ? sanitize_text_field(wp_unslash($_GET['notif_tab'])) : 'unread';
                ?>
                    <div class="movie-engine-view-header">
                        <h3><span class="fa-solid fa-bell"></span> <?php esc_html_e('Notifications', 'cine-movie-engine'); ?></h3>
                    </div>

                    <div class="movie-engine-notif-filters">
                        <div class="movie-engine-tabs-minimal">
                            <a href="?movie_engine_view=notifications&notif_tab=unread" class="movie-engine-tab <?php echo esc_attr($movie_engine_notif_tab_val === 'unread' ? 'active' : ''); ?>" data-tab="unread"><?php esc_html_e('Unread', 'cine-movie-engine'); ?></a>
                            <a href="?movie_engine_view=notifications&notif_tab=read" class="movie-engine-tab <?php echo esc_attr($movie_engine_notif_tab_val === 'read' ? 'active' : ''); ?>" data-tab="read"><?php esc_html_e('Read', 'cine-movie-engine'); ?></a>
                        </div>
                    </div>

                    <div class="movie-engine-notifications-list movie-engine-ajax-container me-ajax-container" data-action="movie_engine_get_notifications" data-tab="<?php echo esc_attr($movie_engine_notif_tab_val); ?>" data-limit="10">
                        <?php \MovieEngine\Utils\TemplateLoader::get_template_part('dashboard/notifications-list'); ?>
                    </div>
                <?php elseif ($movie_engine_view === 'rentals' || $movie_engine_view === 'pay_per_view') : ?>
                    <?php if (has_action('movie_engine_dashboard_purchases')) : ?>
                        <?php do_action('movie_engine_dashboard_purchases', $movie_engine_view, $movie_engine_user); ?>
                    <?php else : ?>
                        <?php \MovieEngine\Utils\TemplateLoader::get_template_part('dashboard/purchases'); ?>
                    <?php endif; ?>

                <?php elseif ($movie_engine_view === 'referrals') : ?>
                    <?php if (has_action('movie_engine_dashboard_referrals')) : ?>
                        <?php do_action('movie_engine_dashboard_referrals', $movie_engine_user); ?>
                    <?php else : ?>
                        <?php \MovieEngine\Utils\TemplateLoader::get_template_part('dashboard/referrals'); ?>
                    <?php endif; ?>

                <?php elseif ($movie_engine_view === 'transactions') : ?>
                    <?php if (has_action('movie_engine_dashboard_transactions')) : ?>
                        <?php do_action('movie_engine_dashboard_transactions', $movie_engine_user); ?>
                    <?php else : ?>
                        <?php \MovieEngine\Utils\TemplateLoader::get_template_part('dashboard/transactions'); ?>
                    <?php endif; ?>

                <?php elseif ($movie_engine_view === 'membership') : ?>
                    <?php if (has_action('movie_engine_dashboard_membership')) : ?>
                        <?php do_action('movie_engine_dashboard_membership', $movie_engine_user, $movie_engine_active_plan, $movie_engine_plan_name, $movie_engine_pricing_url); ?>
                    <?php else : ?>
                        <div class="movie-engine-view-header">
                            <h3><span class="fa-solid fa-trophy"></span> <?php esc_html_e('Membership Statistics', 'cine-movie-engine'); ?></h3>
                        </div>
                        <div class="movie-engine-upgrade-container me-frontend-upgrade">
                            <div class="me-upgrade-header">
                                <div class="me-lock-icon">
                                    <span class="fa-solid fa-lock"></span>
                                </div>
                                <h1><?php esc_html_e('Unlimited Membership Levels', 'cine-movie-engine'); ?> <span><?php esc_html_e('(Pro Feature)', 'cine-movie-engine'); ?></span></h1>
                                <p class="me-upgrade-subtitle"><?php esc_html_e('Upgrade to Movie Engine Pro to create unlimited membership tiers and recurring subscriptions.', 'cine-movie-engine'); ?></p>
                            </div>

                            <div class="me-upgrade-content">
                                <div class="me-upgrade-features-list">
                                    <div class="me-feature-item"><span class="fa-solid fa-check"></span> <?php esc_html_e('Create Multiple Membership Tiers', 'cine-movie-engine'); ?></div>
                                    <div class="me-feature-item"><span class="fa-solid fa-check"></span> <?php esc_html_e('Recurring Payments (Monthly/Yearly)', 'cine-movie-engine'); ?></div>
                                    <div class="me-feature-item"><span class="fa-solid fa-check"></span> <?php esc_html_e('Custom User Badges', 'cine-movie-engine'); ?></div>
                                    <div class="me-feature-item"><span class="fa-solid fa-check"></span> <?php esc_html_e('Restrict Content by Level', 'cine-movie-engine'); ?></div>
                                    <div class="me-feature-item"><span class="fa-solid fa-check"></span> <?php esc_html_e('Private Member Community', 'cine-movie-engine'); ?></div>
                                </div>

                                <div class="me-upgrade-cta-box">
                                    <div class="me-price-notice"><?php esc_html_e('Pro Plans Starting at $89', 'cine-movie-engine'); ?></div>
                                    <div class="me-upgrade-buttons">
                                        <a href="<?php echo esc_url(MovieEngine\Admin\Upgrade::get_pro_url('pricing')); ?>" target="_blank" class="movie-engine-btn me-btn-red">
                                            <?php esc_html_e('Upgrade to Pro', 'cine-movie-engine'); ?>
                                        </a>
                                        <a href="<?php echo esc_url(MovieEngine\Admin\Upgrade::get_pro_url('features')); ?>" target="_blank" class="movie-engine-btn me-btn-outline">
                                            <?php esc_html_e('View Features', 'cine-movie-engine'); ?>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>

                <?php elseif ($movie_engine_view === 'history') : ?>
                    <div class="movie-engine-view-header">
                        <h3><span class="fa-solid fa-clock-rotate-left"></span> <?php esc_html_e('Watch History', 'cine-movie-engine'); ?></h3>
                    </div>
                    <?php echo do_shortcode('[movie_engine_full_history limit="24"]'); ?>

                <?php elseif ($movie_engine_view === 'settings') : ?>
                    <div class="movie-engine-view-header">
                        <h3><span class="fa-solid fa-user"></span> <?php esc_html_e('Account Settings', 'cine-movie-engine'); ?></h3>
                    </div>
                    <div class="movie-engine-settings-inline-wrapper me-settings-inline-wrapper">
                        <form id="movie-engine-profile-form-inline" class="movie-engine-profile-form" enctype="multipart/form-data">
                            <?php wp_nonce_field('movie_engine_update_profile', 'movie_engine_profile_nonce'); ?>

                            <div class="me-section-divider">
                                <h4 class="me-section-title"><?php esc_html_e('Profile Picture', 'cine-movie-engine'); ?></h4>
                            </div>
                            <div class="movie-engine-avatar-upload-section">
                                <div class="movie-engine-avatar-preview">
                                    <img src="<?php echo esc_url($movie_engine_avatar_url); ?>" alt="<?php echo esc_attr($movie_engine_user->display_name); ?>" id="movie-engine-avatar-preview-img">
                                </div>
                                <div class="movie-engine-avatar-upload-controls">
                                    <label for="movie-engine-avatar-upload" class="movie-engine-btn me-btn-secondary">
                                        <span class="fa-solid fa-upload"></span> <?php esc_html_e('Upload New Picture', 'cine-movie-engine'); ?>
                                    </label>
                                    <input type="file" id="movie-engine-avatar-upload" name="avatar" accept="image/*" style="display: none;">
                                    <p class="movie-engine-avatar-hint"><?php esc_html_e('JPG, PNG or GIF. Max size 2MB.', 'cine-movie-engine'); ?></p>
                                </div>
                            </div>

                            <div class="me-section-divider">
                                <h4 class="me-section-title"><?php esc_html_e('Personal Information', 'cine-movie-engine'); ?></h4>
                            </div>

                            <div class="movie-engine-form-row">
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Display Name', 'cine-movie-engine'); ?></label>
                                    <input type="text" name="display_name" value="<?php echo esc_attr($movie_engine_user->display_name); ?>" class="movie-engine-form-input" placeholder="<?php esc_attr_e('e.g. John Doe', 'cine-movie-engine'); ?>">
                                </div>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Email Address', 'cine-movie-engine'); ?></label>
                                    <input type="email" name="user_email" value="<?php echo esc_attr($movie_engine_user->user_email); ?>" class="movie-engine-form-input" placeholder="<?php esc_attr_e('e.g. john@example.com', 'cine-movie-engine'); ?>">
                                </div>
                            </div>
                            <div class="movie-engine-form-row">
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('First Name', 'cine-movie-engine'); ?></label>
                                    <input type="text" name="first_name" value="<?php echo esc_attr(get_user_meta($movie_engine_user->ID, 'first_name', true)); ?>" class="movie-engine-form-input" placeholder="<?php esc_attr_e('First Name', 'cine-movie-engine'); ?>">
                                </div>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Last Name', 'cine-movie-engine'); ?></label>
                                    <input type="text" name="last_name" value="<?php echo esc_attr(get_user_meta($movie_engine_user->ID, 'last_name', true)); ?>" class="movie-engine-form-input" placeholder="<?php esc_attr_e('Last Name', 'cine-movie-engine'); ?>">
                                </div>
                            </div>

                            <div class="me-section-divider">
                                <h4 class="me-section-title"><?php esc_html_e('Change Password', 'cine-movie-engine'); ?></h4>
                            </div>

                            <?php
                            /**
                             * Allow Pro plugin to modify password fields for social login users
                             * 
                             * @param bool $handled Whether custom fields were rendered
                             * @param int $user_id Current user ID
                             * @param WP_User $user Current user object
                             */
                            $custom_fields_rendered = apply_filters('movie_engine_render_custom_password_fields', false, $movie_engine_user->ID, $movie_engine_user);

                            if (!$custom_fields_rendered) :
                            ?>
                                <div class="movie-engine-form-group">
                                    <label><?php esc_html_e('Current Password', 'cine-movie-engine'); ?></label>
                                    <input type="password" name="current_password" class="movie-engine-form-input" autocomplete="current-password" placeholder="<?php esc_attr_e('Enter current password', 'cine-movie-engine'); ?>">
                                </div>
                                <div class="movie-engine-form-row">
                                    <div class="movie-engine-form-group">
                                        <label><?php esc_html_e('New Password', 'cine-movie-engine'); ?></label>
                                        <input type="password" name="new_password" class="movie-engine-form-input" autocomplete="new-password" placeholder="<?php esc_attr_e('Enter new password', 'cine-movie-engine'); ?>">
                                    </div>
                                    <div class="movie-engine-form-group">
                                        <label><?php esc_html_e('Confirm Password', 'cine-movie-engine'); ?></label>
                                        <input type="password" name="confirm_password" class="movie-engine-form-input" autocomplete="new-password" placeholder="<?php esc_attr_e('Confirm new password', 'cine-movie-engine'); ?>">
                                    </div>
                                </div>
                            <?php endif; ?>
                            <div class="movie-engine-form-actions me-form-actions-top">
                                <button type="submit" class="movie-engine-btn me-btn-primary" id="movie-engine-save-profile-inline">
                                    <?php esc_html_e('Save All Changes', 'cine-movie-engine'); ?>
                                </button>
                            </div>
                            <div id="movie-engine-profile-message-inline" class="me-profile-message-inline"></div>
                        </form>
                    </div>
                <?php elseif ($movie_engine_view === 'dashboard') : ?>
                    <!-- Dashboard Overview (Stats handled above) -->

                <?php else : ?>
                    <div class="movie-engine-empty-state">
                        <h3><?php esc_html_e('Coming Soon', 'cine-movie-engine'); ?></h3>
                        <p><?php esc_html_e('This feature is currently under development.', 'cine-movie-engine'); ?></p>
                    </div>
                <?php endif; ?>

            </div>
        </div>
    </div>
</div>