<?php

/**
 * Display a participant card layout with header, image, and metadata.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

$participant_title = $term->name;
$participant_url   = aiopod_get_participant_page_url( $term );   

$image = aiopod_get_image( array(     
    'term'        => $term,
    'taxonomy'    => 'aiopod_participants', 
    'placeholder' => AIOPOD_PLUGIN_PLACEHOLDER_AVATAR_URL 
) );
?>
<div class="aiopod-participant aiopod-responsive-wrapper" data-term_id="<?php echo esc_attr( $term->term_id ); ?>">
    <div class="aiopod-card aiopod-card--participant">
        <a href="<?php echo esc_url( $participant_url ); ?>" class="aiopod-card__image-wrapper aiopod-participant__image-wrapper aiopod-link-unstyled" title="<?php echo esc_attr( $participant_title ); ?>">
            <?php aiopod_the_image( $image ); ?>
        </a>

        <?php
        // Participant content
        $participant_content = '';

        // Participant title
        if ( ! empty( $attributes['show_name'] ) ) {
            $participant_content .= '<div class="aiopod-participant__name aiopod-text-medium">';
            $participant_content .= sprintf(
                '<a href="%s" class="aiopod-link-unstyled" title="%s">%s</a>',
                esc_url( $participant_url ),
                esc_attr( $participant_title ),
                wp_kses_post( $participant_title )
            );
            $participant_content .= '</div>';
        }

        // Primary metadata
        $primary_meta = array();

        if ( ! empty( $attributes['show_job_title'] ) || ! empty( $attributes['show_company_name'] ) || ! empty( $attributes['show_website_url'] ) ) {
            // Job title
            if ( ! empty( $attributes['show_job_title'] ) ) {
                $job_title = get_term_meta( $term->term_id, 'job_title', true );
                
                if ( ! empty( $job_title ) ) {
                    $primary_meta[] = sprintf( 
                        '<span class="aiopod-participant__job-title">%s</span>', 
                        esc_html( $job_title )
                    );
                }
            }

            // Company name
            if ( ! empty( $attributes['show_company_name'] ) ) {
                $company_name = get_term_meta( $term->term_id, 'company_name', true );
                
                if ( ! empty( $company_name ) ) {
                    $primary_meta[] = sprintf( 
                        '<span class="aiopod-participant__company-name">%s</span>', 
                        esc_html( $company_name )
                    );
                }
            }

            // Website url
            if ( ! empty( $attributes['show_website_url'] ) ) {
                $website_url = get_term_meta( $term->term_id, 'website_url', true );
                
                if ( ! empty( $website_url ) ) {
                    $primary_meta[] = sprintf(
                        '<span class="aiopod-participant__website-url aiopod-ui-text">%1$s <a href="%2$s" target="_blank" rel="noopener noreferrer">%2$s</a></span>',
                        aiopod_get_svg( 'globe' ),
                        esc_url( $website_url )
                    );
                }
            }
        }

        if ( ! empty( $primary_meta ) ) {
            $participant_content .= '<div class="aiopod-participant__meta aiopod-participant__meta--primary aiopod-leading-normal aiopod-text-muted aiopod-text-small">';
            $participant_content .= implode( '', $primary_meta );
            $participant_content .= '</div>';
        }
        
        // Secondary metadata
        $secondary_meta = array();

        if ( ! empty( $attributes['show_podcasts_count'] ) || ! empty( $attributes['show_episodes_count'] ) ) {
            // Podcasts count
            if ( ! empty( $attributes['show_podcasts_count'] ) ) {
                $podcasts_count = aiopod_get_participant_posts_count( $term->term_id, 'aiopod_podcasts' );
                
                $html  = '<span class="aiopod-participant__podcasts-count aiopod-ui-text">';
                $html .= aiopod_get_svg( 'microphone' );
                $html .= sprintf( 
                    _n( '%s podcast', '%s podcasts', $podcasts_count, 'aio-podcast-studio' ), 
                    number_format_i18n( $podcasts_count ) 
                );
                $html .= '</span>';

                $secondary_meta[] = $html;
            }

            // Episodes count
            if ( ! empty( $attributes['show_episodes_count'] ) ) {
                $episodes_count = aiopod_get_participant_posts_count( $term->term_id, 'aiopod_episodes' );
                
                $html  = '<span class="aiopod-participant__episodes-count aiopod-ui-text">';
                $html .= aiopod_get_svg( 'episodes' );
                $html .= sprintf( 
                    _n( '%s episode', '%s episodes', $episodes_count, 'aio-podcast-studio' ), 
                    number_format_i18n( $episodes_count ) 
                );
                $html .= '</span>';

                $secondary_meta[] = $html;
            }

            if ( ! empty( $secondary_meta ) ) {
                $participant_content .= '<div class="aiopod-participant__meta aiopod-participant__meta--secondary aiopod-leading-normal aiopod-text-muted aiopod-text-small">';
                $participant_content .= implode( '<span class="aiopod-separator" aria-hidden="true">•</span>', $secondary_meta );
                $participant_content .= '</div>';
            }
        }

        // Participant description
        if ( ! empty( $attributes['show_description'] ) && ! empty( $term->description ) ) {
            $participant_content .= '<div class="aiopod-participant__description">';
            $participant_content .= wp_kses_post( $term->description );
            $participant_content .= '</div>';
        }

        // Output the participant content
        if ( ! empty( $participant_content ) ) {
            echo '<div class="aiopod-card__content-wrapper aiopod-participant__content-wrapper">';
            echo $participant_content;
            echo '</div>';
        }
        ?>
    </div>
</div>