<?php
if ( ! get_theme_mod( 'clothing_marketplace_enable_services_section', false ) ) {
	return;
}

if ( ! function_exists( 'clothing_marketplace_render_services_section' ) ) {
	function clothing_marketplace_render_services_section() {
		if ( ! class_exists( 'WooCommerce' ) ) {
			if ( is_customize_preview() ) {
				?>
				<section id="clothing_marketplace_services_section" class="asterthemes-frontpage-section services-section wc-products-section">
					<div class="asterthemes-wrapper">
						<p><?php esc_html_e( 'Install and activate WooCommerce to display this products section.', 'clothing-marketplace' ); ?></p>
					</div>
				</section>
				<?php
			}
			return;
		}

		$clothing_marketplace_section_title       = get_theme_mod( 'clothing_marketplace_trending_post_content');
		$clothing_marketplace_product_count       = absint( get_theme_mod( 'clothing_marketplace_services_post_count', 6 ) );
		$clothing_marketplace_category_slug       = get_theme_mod( 'clothing_marketplace_services_product_category', '' );

		if ( 0 === $clothing_marketplace_product_count ) {
			$clothing_marketplace_product_count = 6;
		}

		$clothing_marketplace_query_args = array(
			'post_type'           => 'product',
			'post_status'         => 'publish',
			'posts_per_page'      => $clothing_marketplace_product_count,
			'ignore_sticky_posts' => true,
			'no_found_rows'       => true,
		);

		if ( ! empty( $clothing_marketplace_category_slug ) ) {
			$clothing_marketplace_query_args['tax_query'] = array(
				array(
					'taxonomy' => 'product_cat',
					'field'    => 'slug',
					'terms'    => sanitize_title( $clothing_marketplace_category_slug ),
				),
			);
		}

		$clothing_marketplace_query_args = apply_filters( 'clothing_marketplace_services_section_args', $clothing_marketplace_query_args );
		$clothing_marketplace_products_query = new WP_Query( $clothing_marketplace_query_args );
		?>
		<section id="clothing_marketplace_services_section" class="asterthemes-frontpage-section services-section wc-products-section">
			<?php if ( is_customize_preview() ) : ?>
				<?php clothing_marketplace_section_link( 'clothing_marketplace_services_section' ); ?>
			<?php endif; ?>

			<div class="product-asterthemes-wrapper">
				<div class="wc-products-section__header services-inner">
					<?php if ( ! empty( $clothing_marketplace_section_title ) ) : ?>
						<h2><?php echo esc_html( $clothing_marketplace_section_title ); ?></h2>
					<?php endif; ?>
				</div>

				<div class="wc-products-section__grid service-section-wrapper owl-carousel">
					<?php if ( $clothing_marketplace_products_query->have_posts() ) : ?>
						<?php while ( $clothing_marketplace_products_query->have_posts() ) : ?>
							<?php
							$clothing_marketplace_products_query->the_post();
							$clothing_marketplace_product = wc_get_product( get_the_ID() );

							if ( ! $clothing_marketplace_product ) {
								continue;
							}

							$clothing_marketplace_regular_price = (float) $clothing_marketplace_product->get_regular_price();
							$clothing_marketplace_sale_price    = (float) $clothing_marketplace_product->get_sale_price();
							$clothing_marketplace_is_on_sale    = $clothing_marketplace_product->is_on_sale();
							$clothing_marketplace_discount_pct  = 0;

							if ( $clothing_marketplace_is_on_sale && $clothing_marketplace_regular_price > 0 && $clothing_marketplace_sale_price > 0 ) {
								$clothing_marketplace_discount_pct = (int) round( ( ( $clothing_marketplace_regular_price - $clothing_marketplace_sale_price ) / $clothing_marketplace_regular_price ) * 100 );
							}
							?>
							<article <?php post_class( 'wc-products-card' ); ?>>
								<a class="wc-products-card__media" href="<?php echo esc_url( get_permalink() ); ?>">
									<?php if ( $clothing_marketplace_discount_pct > 0 ) : ?>
										<span class="wc-products-card__badge"><?php echo esc_html( $clothing_marketplace_discount_pct . '% OFF' ); ?></span>
									<?php endif; ?>

									<?php
									if ( has_post_thumbnail() ) {
										echo wp_kses_post( get_the_post_thumbnail( get_the_ID(), '', array( 'loading' => 'lazy' ) ) );
									} else {
										echo wc_placeholder_img( 'woocommerce_thumbnail' );
									}
									?>
								</a>

								<div class="wc-products-card__content">
									<h3 class="wc-products-card__title">
										<a href="<?php echo esc_url( get_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a>
									</h3>

									<div class="wc-products-card__price">
										<?php if ( $clothing_marketplace_regular_price > 0 && $clothing_marketplace_is_on_sale ) : ?>
											<span class="wc-products-card__price--regular"><?php echo wp_kses_post( wc_price( $clothing_marketplace_regular_price ) ); ?></span>
											<span class="wc-products-card__price--sale"><?php echo wp_kses_post( wc_price( $clothing_marketplace_sale_price ) ); ?></span>
										<?php else : ?>
											<span class="wc-products-card__price--sale"><?php echo wp_kses_post( $clothing_marketplace_product->get_price_html() ); ?></span>
										<?php endif; ?>
									</div>
								</div>
							</article>
						<?php endwhile; ?>
						<?php wp_reset_postdata(); ?>
					<?php else : ?>
						<p class="wc-products-section__empty"><?php esc_html_e( 'No products found for this section.', 'clothing-marketplace' ); ?></p>
					<?php endif; ?>
				</div>
			</div>
		</section>
		<?php
	}
}

clothing_marketplace_render_services_section();
?>