<?php
/**
 * File that holdes the front-end component of Author Bio Box default template
 *
 * @package Author_Kit
 * @since 1.0.0
 */

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

$author_kit_settings = author_kit_get_bio_template_settings();

$author_kit_icon_style_class     = sanitize_html_class( $author_kit_settings['icons_style'] );
$author_kit_show_heading         = (int) $author_kit_settings['hide_heading'];
$author_kit_heading_text         = sanitize_text_field( $author_kit_settings['heading_text'] );
$author_kit_icon_color           = sanitize_hex_color( $author_kit_settings['icons_color'] );
$author_kit_icon_container_color = sanitize_hex_color( $author_kit_settings['container_color'] );

$author_kit_social_profiles = author_kit_get_social_profiles( $author_id );
$author_kit_link_attributes = author_kit_get_link_attributes();
?>

<div id="author-bio-container" itemscope itemtype="https://schema.org/Person">

	<?php if ( 0 === $author_kit_show_heading ) : ?>
		<div id="about-author-heading">
			<?php echo esc_html( $author_kit_heading_text ); ?>
		</div>
	<?php endif; ?>

	<div id="author-kit-bio-box">
		<!-- Author Avatar -->
		<?php if ( ! empty( $avatar ) && ! empty( $nicename ) ) : ?>
			<div id="avatar-main-container">
				<div id="author-avatar-container">
					<img id="author-avatar"
						src="<?php echo esc_url( $avatar ); ?>"
						alt="<?php echo esc_attr( $avatar_alt_text ); ?>"
						itemprop="image"
						decoding="async">
				</div>
			</div>
		<?php endif; ?>

		<div id="author-details">
			<!-- Author Name -->
			<?php if ( ! empty( $author_url ) && ! empty( $nicename ) ) : ?>
				<h2 id="author-name" itemprop="name">
					<a href="<?php echo esc_url( $author_url ); ?>" itemprop="url">
						<?php echo wp_kses_post( $nicename ); ?>
					</a>
				</h2>
				<meta itemprop="url" content="<?php echo esc_url( $author_url ); ?>">
			<?php endif; ?>

			<!-- Author Designation / Dynamic Info -->
			<?php if ( ! empty( $selected_information ) ) : ?>
				<p id="author-info-placeholder">
					<?php
					$info_text = trim( $selected_information ); // Trim whitespace first.

					if ( is_email( $info_text ) ) {
						// Email context: escape using antispambot + esc_html.
						echo '<span itemprop="email">' . esc_html( antispambot( $info_text ) ) . '</span>';
					} elseif (
						stripos( $info_text, 'Inc' ) !== false ||
						stripos( $info_text, 'LLC' ) !== false ||
						stripos( $info_text, 'Ltd' ) !== false ||
						stripos( $info_text, 'Company' ) !== false
					) {
						// Organization context: escape inner text.
						echo '<span itemprop="worksFor" itemscope itemtype="https://schema.org/Organization">';
						echo '<span itemprop="name">' . esc_html( $info_text ) . '</span>';
						echo '</span>';
					} else {
						// Job title or other text context.
						echo '<span itemprop="jobTitle">' . esc_html( $info_text ) . '</span>';
					}
					?>
				</p>

			<?php endif; ?>


<!-- Author Bio -->
<?php if ( ! empty( trim( $description ) ) ) : ?>
	<div id="author-bio-text" itemprop="description">
		<?php echo wp_kses_post( $description ); ?>
	</div>
<?php endif; ?>



			<!-- Social Icons -->
			<?php if ( ! empty( $author_kit_social_profiles ) ) : ?>
				<div class="author-social-icons-default">
					<div class="author-kit-social-icons-list">
						<?php foreach ( $author_kit_social_profiles as $key => $platform ) : ?>
							<?php
							$url            = isset( $platform['url'] ) ? $platform['url'] : '';
							$icon           = isset( $platform['fa_icon'] ) ? $platform['fa_icon'] : '';
							$platform_name  = isset( $platform['name'] ) ? $platform['name'] : '';
							$platform_color = isset( $platform['color'] ) ? $platform['color'] : '#000000';

							if ( ! empty( $url ) && ! empty( $icon ) ) :
								$final_container_color = ( '#000000' === $author_kit_icon_container_color )
									? $platform_color
									: $author_kit_icon_container_color;
								?>
								<a href="<?php echo esc_url( $url ); ?>"
									style="background-color: <?php echo esc_attr( $final_container_color ); ?>;"
									target="<?php echo esc_attr( $author_kit_link_attributes['target'] ); ?>"
									rel="<?php echo esc_attr( implode( ' ', $author_kit_link_attributes['rel'] ) ); ?>"
									title="<?php echo esc_attr( $platform_name ); ?>"
									class="author-kit-social-icon author-kit-social-<?php echo esc_attr( $key ); ?>"
									id="<?php echo esc_attr( $author_kit_icon_style_class ); ?>"
									itemprop="sameAs">
									<i class="<?php echo esc_attr( $icon ); ?>"
										style="color: <?php echo esc_attr( $author_kit_icon_color ); ?>;"></i>
								</a>
							<?php endif; ?>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>
