<?php
use DWL\Wtm\Classes\Helper;

/**
 * The template for displaying a single Team Manager
 */

get_header();

/**
 * Enqueue scripts and styles for the Single Team Member page if Lightbox is enabled
 * Pro Feature: Lightbox for gallery images
 */
$lightbox_enabled = get_option('tm_single_team_lightbox') === '1' && Helper::is_pro_active();
if ($lightbox_enabled) {
    wp_enqueue_script('wp-team-magnific-popup');
    wp_enqueue_script('wp-team-pro');
    wp_enqueue_style('wp-team-magnific-popup');
}

do_action('wtm_single_team_after_header');

$tm_single_fields = (array) get_option('tm_single_fields', []); // Use default value directly
$selected = Helper::generate_single_fields('frontend');
?>

<div id="primary" class="content-area dwl-team-wrapper dwl-team-single wtm-container single-style">
    <div id="main" class="wtm-row site-main" role="main">
        <article id="post-<?php the_ID(); ?>" <?php post_class('wtm-col-12'); ?>>
            <?php while (have_posts()) : the_post();
                $post_id   = absint(get_the_ID()); // Ensure post ID is an integer
                $job_title = esc_html(get_post_meta($post_id, 'tm_jtitle', true)); // Escape early
                //$short_bio = wp_kses_post(get_post_meta($post_id, 'tm_short_bio', true)); 
                $short_bio = get_post_meta($post_id, 'tm_short_bio', true) ?? '';
                $long_bio = Helper::get_wysiwyg_output('tm_long_bio', $post_id);

                do_action('wtm_before_team_member_content', $post_id);
            ?>
                <div class="entry-content wtm-row">
                    <div class="team-bio-image wtm-col-12 wtm-col-md-6">
                        <?php 
                         if (has_post_thumbnail()) {
                            the_post_thumbnail(get_option('team_image_size_change', 'medium')); 
                        }
                        ?>
                    </div>

                    <div class="wtm-col-12 wtm-col-md-6">
                        <?php the_title('<h2 class="single-team-member-title">', '</h2>'); ?>

                        <?php if (!empty($job_title) && !in_array('tm_jtitle', $tm_single_fields)) : ?>
                            <h3 class="team-position my-0"><?php echo $job_title; ?></h3>
                        <?php endif; ?>

                        <?php if (!empty($short_bio)) : ?>
                            <div class="team-short-bio">
                                <?php echo apply_filters('wtm_team_short_bio_output', wp_strip_all_tags($short_bio), $short_bio, $post_id); ?>
                            </div>
                        <?php endif; ?>

                        <?php
                        // Pro feature: Skills/Progress bars
                        if (Helper::is_pro_active() && class_exists('DWL_Wtm_Pro_Helper')):
                        ?>
                            <div class="wtm-progress-bar">
                                <?php echo DWL_Wtm_Pro_Helper::display_skills_output($post_id); ?>
                            </div>
                        <?php endif; ?>

                        <?php
                            // Pro feature: Enable links for phone/mobile in other info
                            $enable_links = Helper::is_pro_active() ? 'yes' : 'no';
                            echo wp_kses_post(Helper::get_team_other_infos($post_id, $selected, $enable_links));
                            do_action('wtm_before_social_profiles', $post_id);
                            echo wp_kses_post(Helper::display_social_profile_output($post_id));
                            do_action('wtm_after_social_profiles', $post_id);
                        ?>
                    </div>

                    <?php if (!empty($long_bio)) : ?>
                        <div class="wtm-col-12 py-md-3 wp-team-manager-long-bio">
                            <?php echo $long_bio; ?>
                        </div>
                    <?php endif; ?>

                    <div class="wtm-col-12 py-md-3">
                        <?php the_content(); ?>
                    </div>

                    <?php
                        do_action('wtm_before_image_gallery', $post_id);
                        echo wp_kses_post(Helper::get_image_gallery_output($post_id));
                        do_action('wtm_after_image_gallery', $post_id);
                    ?>

                </div>
            <?php 
                if ( class_exists( '\DWL_Wtm_Pro_Helper' ) ) {
                    \DWL_Wtm_Pro_Helper::output_jsonld_for_single( get_the_ID() );
                }
                do_action('wtm_after_team_member_content', $post_id);
            endwhile; ?>
        </article>
    </div>
</div>

<?php get_footer(); ?>