<?php
/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package blueboxed
 */

get_header();
?>

	<main id="primary" class="site-main blueboxed-page blueboxed-post">
		<div class="container">
			<?php
			if ( have_posts() ) :
				if ( is_home() && ! is_paged() ) :
					$featured_query = new WP_Query(
						array(
							'post_type'           => 'post',
							'post_status'         => 'publish',
							'posts_per_page'      => 1,
							'ignore_sticky_posts' => true,
						)
					);
					$categories = get_categories(
						array(
							'orderby'    => 'count',
							'order'      => 'DESC',
							'hide_empty' => true,
							'number'     => 5,
						)
					);
					?>
					<section class="blueboxed-home-hero">
						<div class="blueboxed-home-hero__content">
							<p class="blueboxed-home-hero__eyebrow"><?php esc_html_e( 'Daily Publishing', 'blueboxed' ); ?></p>
							<h1 class="blueboxed-home-hero__title"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></h1>
							<p class="blueboxed-home-hero__desc"><?php echo esc_html( get_bloginfo( 'description' ) ); ?></p>
							<div class="blueboxed-home-hero__cta">
								<a class="btn-green" href="<?php echo esc_url( get_permalink( get_option( 'page_for_posts' ) ) ?: home_url( '/' ) ); ?>">
									<?php esc_html_e( 'Explore Articles', 'blueboxed' ); ?>
								</a>
								<a class="btn-green2" href="<?php echo esc_url( home_url( '/contact' ) ); ?>">
									<?php esc_html_e( 'Work With Us', 'blueboxed' ); ?>
								</a>
							</div>
						</div>
						<?php if ( $featured_query->have_posts() ) : ?>
							<div class="blueboxed-home-hero__feature">
								<?php
								while ( $featured_query->have_posts() ) :
									$featured_query->the_post();
									?>
									<a class="blueboxed-feature-card" href="<?php the_permalink(); ?>">
										<?php if ( has_post_thumbnail() ) : ?>
											<div class="blueboxed-feature-card__thumb"><?php the_post_thumbnail( 'large' ); ?></div>
										<?php endif; ?>
										<h2 class="blueboxed-feature-card__title"><?php the_title(); ?></h2>
										<p class="blueboxed-feature-card__meta"><?php echo esc_html( get_the_date() ); ?></p>
									</a>
									<?php
								endwhile;
								wp_reset_postdata();
								?>
							</div>
						<?php endif; ?>
					</section>

					<?php if ( ! empty( $categories ) ) : ?>
						<section class="blueboxed-home-topics" aria-label="<?php echo esc_attr__( 'Top categories', 'blueboxed' ); ?>">
							<?php foreach ( $categories as $category ) : ?>
								<a class="blueboxed-topic-chip" href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
									<?php echo esc_html( $category->name ); ?>
									<span><?php echo esc_html( (string) $category->count ); ?></span>
								</a>
							<?php endforeach; ?>
						</section>
					<?php endif; ?>
					<?php
				endif;

				if ( is_home() && ! is_front_page() ) :
					?>
					<header>
						<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
					</header>
					<?php
				endif;

				/* Start the Loop */
				while ( have_posts() ) :
					the_post();

					/*
					* Include the Post-Type-specific template for the content.
					* If you want to override this in a child theme, then include a file
					* called content-___.php (where ___ is the Post Type name) and that will be used instead.
					*/
					get_template_part( 'template-parts/content/content', get_post_type() );

				endwhile;

				the_posts_navigation();

			else :

				get_template_part( 'template-parts/content/content', 'none' );

			endif;
			?>
		</div>
	</main><!-- #main -->

<?php
get_footer();
