<?php
/**
 * Template Name: Home Page Template
 */

get_header();

$blogging_hub_slider_hide = get_theme_mod('blogging_hub_slider_hide', false);
$blogging_hub_featured_post_heading = get_theme_mod('blogging_hub_featured_post_heading', 'FEATURED POST');
$blogging_hub_featured_post_hide = get_theme_mod('blogging_hub_new_arrivals_hide', false);
?>

<main id="main" class="site-main">

	<?php
		$blogging_hub_category_enable = get_theme_mod('blogging_hub_categories_hide', false);

		if ($blogging_hub_category_enable) :

		$blogging_hub_category_count = get_theme_mod('blogging_hub_categories_count', 4);
		$blogging_hub_categories_title = get_theme_mod('blogging_hub_categories_title');

		$blogging_hub_category_categories = get_categories(array(
			'number' => $blogging_hub_category_count,
			'orderby' => 'count',
			'order' => 'DESC'
		));
		?>

		<section id="home-categories">
			<div class="container">

				<!-- HEADER -->
				<div class="section-header d-flex justify-content-between align-items-center mb-3">

					<div class="sec-title-wrap">
						<?php if ($blogging_hub_categories_title) : ?>
							<h2 class="categories-section-title">
								<?php echo esc_html($blogging_hub_categories_title); ?>
							</h2>
						<?php endif; ?>
					</div>

					<!-- Here carousel navs -->
					<div class="category-nav">
						<button class="cat-prev"><i class="fa-solid fa-arrow-left"></i></button>
						<button class="cat-next"><i class="fa-solid fa-arrow-right"></i></button>
					</div>

				</div>
				<div class="owl-carousel categories-carousel">

					<?php
					$blogging_hub_cat_num = 1;
					foreach ($blogging_hub_category_categories as $blogging_hub_category_category) :

						// Category Image (fallback)
						$blogging_hub_image_id = get_term_meta($blogging_hub_category_category->term_id, 'category_image', true);

						if ($blogging_hub_image_id) {
							$blogging_hub_category_image = wp_get_attachment_url($blogging_hub_image_id);
						} else {
							$blogging_hub_category_image = get_template_directory_uri() . '/assets/images/category-deafult-img.png';
						}
					?>

					<div class="category-card">

						<a href="<?php echo esc_url(get_category_link($blogging_hub_category_category->term_id)); ?>">

							<div class="category-img">
								<img src="<?php echo esc_url($blogging_hub_category_image); ?>" alt="">
							</div>

							<div class="category-overlay"></div>

							<div class="category-content">

								<span class="category-number">
									<?php echo sprintf('%02d', $blogging_hub_cat_num); ?>
								</span>

								<h3><?php echo esc_html($blogging_hub_category_category->name); ?></h3>

							</div>

						</a>

					</div>

					<?php
					$blogging_hub_cat_num++;
					endforeach;
					?>

				</div>

			</div>
		</section>

	<?php endif; ?>

	<?php
		$blogging_hub_blog_hero_enable = get_theme_mod('blogging_hub_blog_hero_hide', false);

		if ($blogging_hub_blog_hero_enable) :

		$blogging_hub_blog_hero_count = get_theme_mod('blogging_hub_blog_hero_count', 3);
		$blogging_hub_blog_hero_cat = get_theme_mod('blogging_hub_blog_hero_count_category');
		$blogging_hub_trending_title = get_theme_mod('blogging_hub_trending_title');

		// HERO POSTS
		$blogging_hub_blog_hero_args = array( 'post_type' => 'post', 'posts_per_page' => $blogging_hub_blog_hero_count, ); 
		
		if ($blogging_hub_blog_hero_cat) { $blogging_hub_blog_hero_args['cat'] = $blogging_hub_blog_hero_cat; } 
		
		$blogging_hub_blog_hero_query = new WP_Query($blogging_hub_blog_hero_args);

		// TRENDING POSTS (based on comments 🔥)
		$blogging_hub_blog_trending_query = new WP_Query(array(
			'post_type' => 'post',
			'posts_per_page' => 3,
			'orderby'        => 'date',
			'order' => 'DESC',
			'ignore_sticky_posts' => 1 
		));
		?>

		<section id="blog-hero" class="py-4">
			<div class="container">
				<div class="row">

					<!-- LEFT SLIDER -->
					<div class="col-xl-9 col-lg-8">

						<div class="owl-carousel blog-hero-slider">

							<?php while ($blogging_hub_blog_hero_query->have_posts()) : $blogging_hub_blog_hero_query->the_post(); ?>

								<div class="hero-slide position-relative">

									<!-- IMAGE -->
									<?php if (has_post_thumbnail()) {
										the_post_thumbnail('full');
									} else {
										echo '<img src="'.esc_url(get_template_directory_uri().'/assets/images/hero-default-img.png').'">';
									} ?>

									<!-- OVERLAY -->
									<div class="hero-overlay"></div>

									<!-- CONTENT -->
									<div class="hero-content-main-box">
										<div class="hero-content">

											<!-- CATEGORY -->
											<span class="hero-cat">
												<?php $blogging_hub_cat = get_the_category(); if($blogging_hub_cat) echo esc_html($blogging_hub_cat[0]->name); ?>
											</span>

											<!-- RATING -->
											<span class="hero-rating ms-3">
												<?php

												$blogging_hub_rating = 0;

												// Plugin support
												if ( function_exists('rmp_get_avg_rating') ) {
													$blogging_hub_rating = floatval( rmp_get_avg_rating( get_the_ID() ) );
												}

												// CHECK: if no rating
												if ( $blogging_hub_rating > 0 ) {

													$blogging_hub_full = floor( $blogging_hub_rating );
													$blogging_hub_half = ( $blogging_hub_rating - $blogging_hub_full ) >= 0.5;

													for ( $blogging_hub_star_i = 1; $blogging_hub_star_i <= 5; $blogging_hub_star_i++ ) {

														if ( $blogging_hub_star_i <= $blogging_hub_full ) {
															echo '<i class="fa fa-star"></i>';

														} elseif ( $blogging_hub_half && $blogging_hub_star_i == $blogging_hub_full + 1 ) {
															echo '<i class="fa fa-star-half-alt"></i>';

														} else {
															echo '<i class="fa fa-star-o"></i>';
														}
													}

													echo '<span>' . esc_html( $blogging_hub_rating ) . '</span>';

												} else {

													// NO RATING TEXT ✅
													echo '<span class="no-rating">';
													echo esc_html__( 'No ratings', 'blogging-hub' );
													echo '</span>';
												}

												?>
											</span>

											<!-- TITLE -->
											<h2 class="hero-heading">
												<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
											</h2>

										</div>
											
										<div class="hero-content-second">
												
											<!-- CONTENT -->
											<div class="hero-description">
												<p><?php echo wp_trim_words(get_the_content(), 20, '...'); ?></p>
											</div>

											<!-- META -->
											<div class="hero-meta">
												<p>
													<?php esc_html_e('Admin :', 'blogging-hub'); ?>
													<?php echo esc_html( get_the_author() ); ?> -
													<?php esc_html_e('Date :', 'blogging-hub'); ?>
													<?php echo esc_html( get_the_date() ); ?>
												</p>
											</div>

										</div>
									</div>
									
									<!-- Here carousel navs -->
									<div class="hero-nav">
										<button class="hero-prev"><i class="fa-solid fa-arrow-left"></i></button>
										<button class="hero-next"><i class="fa-solid fa-arrow-right"></i></button>
									</div>
									
								</div>

							<?php endwhile; wp_reset_postdata(); ?>

						</div>
					</div>

					<!-- RIGHT TRENDING -->
					<div class="col-xl-3 col-lg-4">

						<div class="trending-box">

							<div class="sec-title-wrap">
								<?php if ($blogging_hub_trending_title) : ?>
									<h2 class="trending-section-title">
										<?php echo esc_html($blogging_hub_trending_title); ?>
									</h2>
								<?php endif; ?>
							</div>

							<?php 
							$blogging_hub_i = 1;
							while ($blogging_hub_blog_trending_query->have_posts()) : 
							$blogging_hub_blog_trending_query->the_post(); ?>

								<?php if ($blogging_hub_i == 1) : ?>
									<!-- FIRST BIG POST -->
									<div class="trending-big">

										<div class="trend-img">
											<?php if (has_post_thumbnail()) {
												the_post_thumbnail('medium_large');
											} else {
												echo '<img src="'.esc_url(get_template_directory_uri().'/assets/images/header-img.png').'">';
											} ?>
										</div>

										<div class="trend-cont">
											<span class="trend-cat">
												<?php $blogging_hub_cat = get_the_category(); if($blogging_hub_cat) echo esc_html($blogging_hub_cat[0]->name); ?>
											</span>

											<h4>
												<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
											</h4>
										</div>

									</div>

								<?php else : ?>
									<!-- SMALL ITEMS -->
									<div class="trending-item">

										<span class="trend-cat">
											<?php $blogging_hub_cat = get_the_category(); if($blogging_hub_cat) echo esc_html($blogging_hub_cat[0]->name); ?>
										</span>

										<h4>
											<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
										</h4>

									</div>
								<?php endif; ?>

							<?php $blogging_hub_i++; endwhile; wp_reset_postdata(); ?>

							</div>

					</div>

				</div>
			</div>
		</section>

	<?php endif; ?>

	<?php
		$blogging_hub_enable = get_theme_mod('blogging_hub_popular_review_hide', false);

		if ($blogging_hub_enable) :

		$blogging_hub_popular_review_title = get_theme_mod('blogging_hub_popular_review_title');
		$blogging_hub_count = get_theme_mod('blogging_hub_popular_review_count', 4);

		$blogging_hub_args = array(
			'post_type'      => 'post',
			'posts_per_page' => $blogging_hub_count,
			'orderby' => 'comment_count',
			'order'          => 'DESC',
			'ignore_sticky_posts' => 1 
		);

		$blogging_hub_query = new WP_Query($blogging_hub_args);
		?>

		<section id="most-popular-review" class="py-5">
			<div class="container">

				<!-- HEADER -->
				<div class="section-header most-popular d-flex justify-content-between align-items-center mb-4">

					<div class="sec-title-wrap">
						<?php if ($blogging_hub_popular_review_title) : ?>
							<h2 class="most-popular-section-title">
								<?php echo esc_html($blogging_hub_popular_review_title); ?>
							</h2>
						<?php endif; ?>
					</div>

					<!-- VIEW ALL BUTTON (YOUR LOGIC ✅) -->
					<a href="<?php echo esc_url(get_permalink(get_option('page_for_posts'))); ?>" class="view-all-btn">
						<?php echo esc_html(get_theme_mod('blogging_hub_popular_review_view_all_btn')); ?>
						<i class="fa-solid fa-arrow-right"></i>
					</a>

				</div>

				<!-- CAROUSEL -->
				<div class="owl-carousel popular-review-carousel">

					<?php if ($blogging_hub_query->have_posts()) :
						while ($blogging_hub_query->have_posts()) : $blogging_hub_query->the_post(); ?>

						<div class="review-card">

							<!-- IMAGE -->
							<div class="review-img position-relative">
								<?php if (has_post_thumbnail()) {
									the_post_thumbnail('medium_large');
								} else {
									echo '<img src="' . esc_url(get_template_directory_uri() . '/assets/images/popular-deafult-img.png') . '">';
								} ?>

								<span class="review-category">
									<?php
									$blogging_hub_cat = get_the_category();
									if ($blogging_hub_cat) echo esc_html($blogging_hub_cat[0]->name);
									?>
								</span>
							</div>

							<!-- CONTENT -->
							<div class="review-content">

								<!-- RATING -->
								<span class="review-rating">
									<?php

									$blogging_hub_rating = 0;

									// Plugin support
									if ( function_exists('rmp_get_avg_rating') ) {
										$blogging_hub_rating = floatval( rmp_get_avg_rating( get_the_ID() ) );
									}

									// CHECK: if no rating
									if ( $blogging_hub_rating > 0 ) {

										$blogging_hub_full = floor( $blogging_hub_rating );
										$blogging_hub_half = ( $blogging_hub_rating - $blogging_hub_full ) >= 0.5;

										for ( $blogging_hub_star_i = 1; $blogging_hub_star_i <= 5; $blogging_hub_star_i++ ) {

											if ( $blogging_hub_star_i <= $blogging_hub_full ) {
												echo '<i class="fa fa-star"></i>';

											} elseif ( $blogging_hub_half && $blogging_hub_star_i == $blogging_hub_full + 1 ) {
												echo '<i class="fa fa-star-half-alt"></i>';

											} else {
												echo '<i class="fa fa-star-o"></i>';
											}
										}

										echo '<span>' . esc_html( $blogging_hub_rating ) . '</span>';

									} else {

										// NO RATING TEXT ✅
										echo '<span class="no-rating">';
										echo esc_html__( 'No ratings', 'blogging-hub' );
										echo '</span>';
									}

									?>
								</span>

								<!-- TITLE -->
								<h3 class="review-title">
									<a href="<?php the_permalink(); ?>">
										<?php the_title(); ?>
									</a>
								</h3>

								<!-- META -->
								<div class="review-meta">
									<p>
										<?php esc_html_e('Admin :', 'blogging-hub'); ?>
										<?php echo esc_html( get_the_author() ); ?> -
										<?php esc_html_e('Date :', 'blogging-hub'); ?>
										<?php echo esc_html( get_the_date() ); ?>
									</p>
								</div>

							</div>

						</div>

					<?php endwhile;
						wp_reset_postdata();
					endif; ?>

				</div>

			</div>
		</section>

	<?php endif; ?>
</main>
<?php
get_footer();