<?php

/**
 * The template for displaying header user profile menu
 *
 * This template can be overridden by copying it to yourtheme/movie-engine/global/user-menu.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/global
 * @version 1.0.0
 * @since 1.0.0
 */

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

$movie_engine_user_id = get_current_user_id();
$movie_engine_user = $args['user'] ?? get_userdata($movie_engine_user_id);
$movie_engine_avatar_url = $args['avatar_url'] ?? '';

if (!$movie_engine_avatar_url) {
    $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' => 100]);
}

$movie_engine_dashboard_page_id = $args['dashboard_page_id'] ?? '';
if (!$movie_engine_dashboard_page_id) {
    $movie_engine_design_settings = get_option('movie_engine_design_settings', []);
    $movie_engine_dashboard_page_id = isset($movie_engine_design_settings['dashboard_page']) ? $movie_engine_design_settings['dashboard_page'] : '';
}
$movie_engine_dashboard_url = $movie_engine_dashboard_page_id ? get_permalink($movie_engine_dashboard_page_id) : home_url('/dashboard');
$atts = $args['atts'] ?? [];
?>

<div class="movie-engine-user-menu-container <?php echo isset($atts['custom_class']) ? esc_attr($atts['custom_class']) : ''; ?>">
    <div class="movie-engine-user-trigger">
        <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-dropdown">
        <div class="movie-engine-dropdown-header">
            <div class="movie-engine-dropdown-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-dropdown-user-info">
                <span class="user-name"><?php echo esc_html($movie_engine_user->display_name); ?></span>
            </div>
        </div>

        <nav class="movie-engine-dropdown-nav">
            <a href="<?php echo esc_url(add_query_arg('movie_engine_view', 'settings', $movie_engine_dashboard_url)); ?>" class="dropdown-item">
                <span class="fa-regular fa-user" aria-hidden="true"></span>
                <?php esc_html_e('Profile', 'cine-movie-engine'); ?>
            </a>
            <a href="<?php echo esc_url(add_query_arg('movie_engine_view', 'watchlist', $movie_engine_dashboard_url)); ?>" class="dropdown-item">
                <span class="fa-solid fa-plus" aria-hidden="true"></span>
                <?php esc_html_e('Watch List', 'cine-movie-engine'); ?>
            </a>
            <a href="<?php echo esc_url(add_query_arg('movie_engine_view', 'playlists', $movie_engine_dashboard_url)); ?>" class="dropdown-item">
                <span class="fa-solid fa-list-ul" aria-hidden="true"></span>
                <?php esc_html_e('Playlist', 'cine-movie-engine'); ?>
            </a>
            <a href="<?php echo esc_url(add_query_arg('movie_engine_view', 'membership', $movie_engine_dashboard_url)); ?>" class="dropdown-item">
                <span class="fa-solid fa-crown" aria-hidden="true"></span>
                <?php esc_html_e('Subscription', 'cine-movie-engine'); ?>
            </a>
            <a href="<?php echo esc_url(add_query_arg('movie_engine_view', 'notifications', $movie_engine_dashboard_url)); ?>" class="dropdown-item">
                <span class="fa-regular fa-bell" aria-hidden="true"></span>
                <?php esc_html_e('Notification', 'cine-movie-engine'); ?>
            </a>
        </nav>

        <div class="movie-engine-dropdown-footer">
            <a href="<?php echo esc_url(wp_logout_url(home_url())); ?>" class="dropdown-item logout-link">
                <span class="fa-solid fa-right-from-bracket" aria-hidden="true"></span>
                <?php esc_html_e('Logout', 'cine-movie-engine'); ?>
            </a>
        </div>
    </div>
</div>