<?php
/**
 * Template for single Staff member profile pages.
 *
 * @package kadence-child
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

get_header();

if ( function_exists( 'Kadence\kadence' ) ) {
	Kadence\kadence()->print_styles( 'kadence-content' );
}

while ( have_posts() ) :
	the_post();

	$post_id   = get_the_ID();
	$full_name = get_the_title();

	// ACF fields.
	$job_title        = get_post_meta( $post_id, 'job_title', true );
	$description_items = array();
	$desc_count       = (int) get_post_meta( $post_id, 'description_item', true );
	for ( $i = 0; $i < $desc_count; $i++ ) {
		$item = get_post_meta( $post_id, 'description_item_' . $i . '_item', true );
		if ( $item ) {
			$description_items[] = $item;
		}
	}

	// Schema fields.
	$suffix      = get_post_meta( $post_id, '_bw_schema_honorific_suffix', true );
	$expertise   = get_post_meta( $post_id, '_bw_schema_knows_about', true );
	$credentials = get_post_meta( $post_id, '_bw_schema_credentials', true );
	$social      = get_post_meta( $post_id, '_bw_schema_same_as', true );
	$awards      = get_post_meta( $post_id, '_bw_schema_awards', true );
	$alumni      = get_post_meta( $post_id, '_bw_schema_alumni_of', true );
	$memberships = get_post_meta( $post_id, '_bw_schema_member_of', true );

	// Build display name.
	$display_name = $full_name;
	if ( $suffix ) {
		$display_name .= ', ' . $suffix;
	}

	// Featured image.
	$has_image    = has_post_thumbnail();
	$image_url    = $has_image ? get_the_post_thumbnail_url( $post_id, 'medium_large' ) : '';

	// Bio.
	$bio = get_the_content();
	?>

	<div class="staff-profile">
		<div class="staff-profile__inner">

			<!-- Hero Section -->
			<div class="staff-profile__hero">
				<?php if ( $has_image ) : ?>
					<div class="staff-profile__photo">
						<img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $full_name ); ?>">
					</div>
				<?php endif; ?>

				<div class="staff-profile__intro">
					<h1 class="staff-profile__name"><?php echo esc_html( $display_name ); ?></h1>

					<?php if ( $job_title ) : ?>
						<p class="staff-profile__title"><?php echo esc_html( $job_title ); ?></p>
					<?php endif; ?>

					<?php if ( ! empty( $description_items ) ) : ?>
						<ul class="staff-profile__highlights">
							<?php foreach ( $description_items as $item ) : ?>
								<li><?php echo esc_html( $item ); ?></li>
							<?php endforeach; ?>
						</ul>
					<?php endif; ?>

					<?php if ( ! empty( $social ) && is_array( $social ) ) : ?>
						<div class="staff-profile__social">
							<?php foreach ( $social as $url ) :
								$icon  = 'link';
								$label = 'Website';
								if ( stripos( $url, 'linkedin.com' ) !== false ) {
									$icon  = 'linkedin';
									$label = 'LinkedIn';
								} elseif ( stripos( $url, 'twitter.com' ) !== false || stripos( $url, 'x.com' ) !== false ) {
									$icon  = 'twitter';
									$label = 'Twitter';
								} elseif ( stripos( $url, 'facebook.com' ) !== false ) {
									$icon  = 'facebook';
									$label = 'Facebook';
								}
								?>
								<a href="<?php echo esc_url( $url ); ?>" target="_blank" rel="noopener noreferrer" class="staff-profile__social-link staff-profile__social-link--<?php echo esc_attr( $icon ); ?>">
									<?php if ( 'linkedin' === $icon ) : ?>
										<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 01-2.063-2.065 2.064 2.064 0 112.063 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/></svg>
									<?php else : ?>
										<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M10 6v2H5v11h11v-5h2v6a1 1 0 01-1 1H4a1 1 0 01-1-1V7a1 1 0 011-1h6zm11-3v8h-2V6.413l-7.793 7.794-1.414-1.414L17.585 5H13V3h8z"/></svg>
									<?php endif; ?>
									<?php echo esc_html( $label ); ?>
								</a>
							<?php endforeach; ?>
						</div>
					<?php endif; ?>
				</div>
			</div>

			<!-- Bio Section -->
			<?php if ( $bio ) : ?>
				<div class="staff-profile__section staff-profile__bio">
					<h2>About</h2>
					<div class="staff-profile__bio-text">
						<?php the_content(); ?>
					</div>
				</div>
			<?php endif; ?>

			<!-- Info Grid -->
			<?php
			$has_expertise    = ! empty( $expertise ) && is_array( $expertise );
			$has_credentials  = ! empty( $credentials ) && is_array( $credentials );
			$has_memberships  = ! empty( $memberships ) && is_array( $memberships );
			$has_awards       = ! empty( $awards ) && is_array( $awards );

			if ( $has_expertise || $has_credentials || $has_memberships || $has_awards ) :
			?>
				<div class="staff-profile__details">

					<?php if ( $has_expertise ) : ?>
						<div class="staff-profile__section staff-profile__card">
							<h3>Areas of Expertise</h3>
							<div class="staff-profile__tags">
								<?php foreach ( $expertise as $skill ) : ?>
									<span class="staff-profile__tag"><?php echo esc_html( $skill ); ?></span>
								<?php endforeach; ?>
							</div>
						</div>
					<?php endif; ?>

					<?php if ( $has_credentials ) : ?>
						<div class="staff-profile__section staff-profile__card">
							<h3>Education & Credentials</h3>
							<ul class="staff-profile__credentials">
								<?php foreach ( $credentials as $cred ) :
									$category = isset( $cred['category'] ) ? $cred['category'] : '';
									$level    = isset( $cred['level'] ) ? $cred['level'] : '';
									$school   = isset( $cred['school'] ) ? $cred['school'] : '';
									$desc     = isset( $cred['description'] ) ? $cred['description'] : '';
									if ( ! $level ) continue;

									$cat_labels = array(
										'degree'        => 'Degree',
										'certification' => 'Certification',
										'professional'  => 'Professional',
									);
									$cat_label = isset( $cat_labels[ $category ] ) ? $cat_labels[ $category ] : '';
									?>
									<li class="staff-profile__credential">
										<?php if ( $cat_label ) : ?>
											<span class="staff-profile__credential-type"><?php echo esc_html( $cat_label ); ?></span>
										<?php endif; ?>
										<span class="staff-profile__credential-name"><?php echo esc_html( $level ); ?></span>
										<?php if ( $school ) : ?>
											<span class="staff-profile__credential-school"><?php echo esc_html( $school ); ?></span>
										<?php endif; ?>
										<?php if ( $desc ) : ?>
											<span class="staff-profile__credential-desc"><?php echo esc_html( $desc ); ?></span>
										<?php endif; ?>
									</li>
								<?php endforeach; ?>
							</ul>
						</div>
					<?php endif; ?>

					<?php if ( $has_memberships ) : ?>
						<div class="staff-profile__section staff-profile__card">
							<h3>Professional Memberships</h3>
							<ul class="staff-profile__list">
								<?php foreach ( $memberships as $org ) : ?>
									<li><?php echo esc_html( $org ); ?></li>
								<?php endforeach; ?>
							</ul>
						</div>
					<?php endif; ?>

					<?php if ( $has_awards ) : ?>
						<div class="staff-profile__section staff-profile__card">
							<h3>Awards & Recognition</h3>
							<ul class="staff-profile__list">
								<?php foreach ( $awards as $award ) : ?>
									<li><?php echo esc_html( $award ); ?></li>
								<?php endforeach; ?>
							</ul>
						</div>
					<?php endif; ?>

				</div>
			<?php endif; ?>

		</div>
	</div>

<?php
endwhile;

get_footer();
