<?php
/**
 * The template for displaying the sidebar menu in the candidate dashboard
 *
 * @package jobus
 */

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

$user = wp_get_current_user();

// Get dashboard URL
$dashboard_url = get_permalink();

// Use menu_items passed from Dashboard class
$menu_items = $args['menu_items'] ?? [];

// Determine active endpoint from $args if available, otherwise default to dashboard
$active_endpoint = $args['active_endpoint'] ?? 'dashboard';

$profile_url = '';
$role_post_map = [
    'jobus_employer'  => 'jobus_company',
    'jobus_candidate' => 'jobus_candidate',
];

foreach ( $role_post_map as $role => $post_type ) {
    if ( in_array( $role, (array) $user->roles, true ) ) {
        $posts = get_posts([
            'post_type'   => $post_type,
            'author'      => $user->ID,
            'numberposts' => 1,
            'fields'      => 'ids',
        ]);

        if ( ! empty( $posts ) ) {
            $profile_url = get_permalink( $posts[0] );
        }
        break;
    }
}

$logo = jobus_opt( 'dashboard_logo' );


if ( ! empty( $logo['url'] ) ) { ?>
    <div class="logo jbs-text-md-center jbs-d-md-block jbs-d-flex jbs-align-items-center jbs-justify-content-between">
        <a href="<?php esc_url( home_url( '/' ) ); ?>">
            <img src="<?php echo esc_url( $logo['url'] ) ?>" alt="<?php get_bloginfo( 'name' ) ?>">
        </a>
        <button class="close-btn jbs-d-block jbs-d-md-none"><i class="bi bi-x-lg"></i></button>
    </div>
    <?php
}
?>

<div class="user-data">
    <div class="user-avatar online jbs-position-relative jbs-rounded-circle">
        <?php
        $user_avatar = get_user_meta($user->ID, 'jobus_avatar', true);
        if ($user_avatar && filter_var($user_avatar, FILTER_VALIDATE_URL)) {
            echo '<img src="' . esc_url($user_avatar) . '" alt="' . esc_attr($user->display_name) . '" class="lazy-img user-avatar-img" width="75" height="75">';
        } else {
            echo get_avatar($user->user_email, 75, '', $user->display_name, [ 'class' => 'lazy-img user-avatar-img' ]);
        }
        ?>
    </div>
    <div class="user-name-data jbs-dropdown">
        <button class="user-name jbs-dropdown-toggle user_name-btn" type="button" id="profile-dropdown" data-jbs-toggle="jbs-dropdown" data-jbs-auto-close="jbs-outside"
                aria-expanded="false">
            <?php echo esc_html( $user->display_name ); ?>
        </button>
        <ul class="jbs-dropdown-menu" aria-labelledby="profile-dropdown">
            <li>
                <?php $view_profile_label = jobus_opt( 'label_view_profile', esc_html__( 'View Profile', 'jobus' ) ); ?>
                <a href="<?php echo esc_url($profile_url); ?>" class="jbs-dropdown-item jbs-d-flex jbs-align-items-center">
                    <img src="<?php echo esc_url(JOBUS_IMG . '/dashboard/icons/profile.svg') ?>" alt="<?php echo esc_attr( $view_profile_label ); ?>" class="lazy-img">
                    <span class="jbs-ms-2 ps-1"><?php echo esc_html( $view_profile_label ); ?></span>
                </a>
            </li>
        </ul>
    </div>
</div>

<nav class="main-nav">
    <ul class="jbs-style-none">
        <?php
        foreach ( $menu_items as $endpoint => $item ) :
            if ( function_exists( 'jobus_get_dashboard_endpoint_url' ) ) {
                $url = jobus_get_dashboard_endpoint_url( $endpoint, $dashboard_url );
            } else {
                $url = trailingslashit( $dashboard_url ) . $endpoint . '/';
            }
            $is_active = $active_endpoint === $endpoint;
            $align_class = 'jbs-d-flex jbs-w-100 jbs-align-items-center dashboard-navigation-link dashboard-navigation-link--' . $endpoint;
            if ( $is_active ) {
                $align_class .= ' active';
            }
            $aria_current = $is_active ? ' aria-current="page"' : '';
            ?>
            <li>
                <a href="<?php echo esc_url( $url ); ?>" class="<?php echo esc_attr($align_class) ?>"<?php echo esc_attr($aria_current); ?>>
                    <img src="<?php echo esc_url( $item['icon'] ); ?>" alt="" class="lazy-img">
                    <span><?php echo esc_html( $item['label'] ); ?></span>
                </a>
            </li>
        <?php
        endforeach;
        ?>
    </ul>
</nav>

<?php
$logout_label = jobus_opt( 'label_logout', esc_html__( 'Logout', 'jobus' ) );
?>
<a href="<?php echo esc_url(wp_logout_url(home_url())); ?>" class="jbs-d-flex jbs-w-100 jbs-align-items-center logout-btn jbs-mt-25">
    <img src="<?php echo esc_url(JOBUS_IMG . '/dashboard/icons/logout.svg') ?>" alt="<?php echo esc_attr( $logout_label ); ?>" class="lazy-img">
    <span><?php echo esc_html( $logout_label ); ?></span>
</a>