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

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div id="author-kit-archive-template">

	<!-- ✅ AUTHOR BIO BOX -->
	<div id="author-kit-bio-box">
		<div class="author-kit-bio-container">
			<div class="author-kit-avatar-wrapper">

                <?php // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
				echo '<img src="' . esc_url( $avatar ) . '" alt="' . esc_attr( $alt_text ) . '" class="author-kit-avatar">';
				?>

			</div>
			<div class="author-kit-bio-content">
				<h2 class="author-kit-name"><?php echo wp_kses_post( $nicename ); ?></h2>
				<span class="author-kit-job-title"><?php echo esc_html( $designation ); ?></span>
				<div class="author-kit-description"><?php echo wp_kses_post( $description ); ?></div>

				<!-- ✅ SOCIAL LINKS -->
				<div class="author-kit-social-links">
					<?php
					$template_option_name  = 'author_kit_author_archive_template';
					$default_template_name = 'archive-template-default';
					$active_template       = get_option( $template_option_name, $default_template_name );

					// Normalize templates for switch case.
					$active_template = sanitize_key( $active_template );

					switch ( $active_template ) {
						case 'archive-template-scribe':
							$icon_color           = get_theme_mod( 'author_kit_archive_social_icon_color_scribe', '#ffffff' );
							$icon_container_color = get_theme_mod( 'author_kit_archive_icon_container_color_scribe', '#3c3c50' );
							$icon_style_class     = get_theme_mod( 'author_kit_archive_icon_style_scribe', 'author-kit-circle' );
							break;

						case 'archive-template-quill':
							$icon_color           = get_theme_mod( 'author_kit_archive_social_icon_color_quill', '#ffffff' );
							$icon_container_color = get_theme_mod( 'author_kit_archive_icon_container_color_quill', '#fe4f2d' );
							$icon_style_class     = get_theme_mod( 'author_kit_archive_icon_style_quill', 'author-kit-square' );
							break;

						case 'archive-template-default':
						default:
							$icon_color           = get_theme_mod( 'author_kit_archive_social_icon_color', '#ffffff' );
							$icon_container_color = get_theme_mod( 'author_kit_archive_icon_container_color', '#000000' );
							$icon_style_class     = get_theme_mod( 'author_kit_archive_icon_style', 'author-kit-circle' );
							break;
					}

					if ( ! empty( $social_profiles ) && is_array( $social_profiles ) ) :
						?>
						<div class="author-kit-social-icons-list">
							<?php
							foreach ( $social_profiles as $key => $platform ) :
								$url     = isset( $platform['url'] ) ? esc_url( $platform['url'] ) : '';
								$fa_icon = isset( $platform['fa_icon'] ) ? esc_attr( $platform['fa_icon'] ) : '';
								$name    = isset( $platform['name'] ) ? esc_attr( $platform['name'] ) : '';
								$color   = isset( $platform['color'] ) ? $platform['color'] : '#000000';

								if ( $url && $fa_icon ) :
									$final_container_color = ( '#000000' === $icon_container_color ) ? $color : $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( $link_attributes['target'] ); ?>"
										rel="<?php echo esc_attr( implode( ' ', array_map( 'esc_attr', (array) $link_attributes['rel'] ) ) ); ?>"
										itemprop="sameAs"
										title="<?php echo esc_attr( $name ); ?>"
										class="author-kit-social-icon author-kit-social-<?php echo esc_attr( $key ); ?>"
										id="<?php echo esc_attr( $icon_style_class ); ?>">
										<i class="<?php echo esc_attr( $fa_icon ); ?>" style="color: <?php echo esc_attr( $icon_color ); ?>;"></i>
									</a>

									<?php
							endif;
							endforeach;
							?>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>

	<!-- ✅ POSTS GRID -->
	<section class="author-kit-posts">
		<div class="author-kit-posts-title">
			<h3 id="post-title"><?php echo esc_html( get_theme_mod( 'author_kit_grid_archive_title', 'Posts By' ) ); ?></h3>
			<h3 id="author-name"><?php echo wp_kses_post( $nicename ); ?></h3>
		</div>

		<div class="author-kit-posts-grid">
			<?php
			$paged_value = max( 1, (int) get_query_var( 'paged' ) );

			$args = array(
				'author'         => absint( $author_id ),
				'posts_per_page' => 6,
				'paged'          => $paged_value,
			);

			$author_posts = new WP_Query( $args );

			if ( $author_posts->have_posts() ) :
				while ( $author_posts->have_posts() ) :
					$author_posts->the_post();
					?>
					<div class="author-kit-post-item">
						<a href="<?php the_permalink(); ?>">
							<?php the_post_thumbnail( 'medium', array( 'class' => 'author-kit-post-thumbnail' ) ); ?>
						</a>

						<h4 class="author-kit-post-title">
							<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
						</h4>

						<div class="author-kit-post-excerpt">
							<?php echo esc_html( wp_trim_words( get_the_excerpt(), 20, '...' ) ); ?>
						</div>

						<div class="author-kit-post-meta">
							<span class="post-date"><?php echo esc_html( get_the_date() ); ?></span>
							<span class="author-name"><?php echo esc_html( get_the_author() ); ?></span>
						</div>
					</div>
					<?php
				endwhile;
				wp_reset_postdata();
			else :
				?>
				<p class="author-kit-no-posts">No posts found.</p>
			<?php endif; ?>
		</div>
	</section>

	<!-- ✅ PAGINATION -->
	<?php if ( ! empty( $author_posts ) && $author_posts->max_num_pages > 1 ) : ?>
		<div class="author-kit-pagination-container">
			<?php author_kit_pagination( $author_posts->max_num_pages ); ?>
		</div>
	<?php endif; ?>

</div>
