<?php
/**
 * Services Section — Static Grid Layout.
 *
 * @package technology_software_firm
 */

if ( ! get_theme_mod( 'technology_software_firm_enable_services_section', false ) ) {
	return;
}

// ── Section-level settings ────────────────────────────────────────────────────
$technology_software_firm_sv_bg_text    = get_theme_mod( 'technology_software_firm_services_bg_heading', '' );
$technology_software_firm_sv_title      = get_theme_mod( 'technology_software_firm_services_section_title', '' );
$technology_software_firm_sv_category   = get_theme_mod( 'technology_software_firm_services_category', '' );
$technology_software_firm_sv_count      = absint( get_theme_mod( 'technology_software_firm_services_posts_count', 3 ) );

if ( $technology_software_firm_sv_count < 1 ) {
	$technology_software_firm_sv_count = 1;
} elseif ( $technology_software_firm_sv_count > 9 ) {
	$technology_software_firm_sv_count = 9;
}

$technology_software_firm_query_args = array(
	'post_type'           => 'post',
	'post_status'         => 'publish',
	'posts_per_page'      => $technology_software_firm_sv_count,
	'ignore_sticky_posts' => true,
	'no_found_rows'       => true,
);

if ( ! empty( $technology_software_firm_sv_category ) ) {
	$technology_software_firm_query_args['category_name'] = sanitize_title( $technology_software_firm_sv_category );
}

$technology_software_firm_icon_fallback_map = array(
	'development' => 'fas fa-code',
	'software'    => 'fas fa-laptop-code',
	'design'      => 'fas fa-pen-ruler',
	'ui-ux'       => 'fas fa-swatchbook',
	'marketing'   => 'fas fa-bullhorn',
	'seo'         => 'fas fa-chart-line',
	'consulting'  => 'fas fa-user-tie',
	'cloud'       => 'fas fa-cloud',
	'ai'          => 'fas fa-microchip',
	'analytics'   => 'fas fa-chart-pie',
);

$technology_software_firm_default_service_icon = 'fas fa-code';

// ── Services posts ────────────────────────────────────────────────────────────
$technology_software_firm_blog_query = new WP_Query( $technology_software_firm_query_args );
?>
<section id="technology_software_firm_services_section" class="cm-services-section">
	<?php if ( is_customize_preview() ) : ?>
		<?php technology_software_firm_section_link( 'technology_software_firm_services_section' ); ?>
	<?php endif; ?>

	<div class="asterthemes-wrapper">

		<header class="cm-services__header">
			<div class="cm-services__header-main">
				<?php if ( ! empty( $technology_software_firm_sv_bg_text ) ) : ?>
					<span class="cm-services__badge"><?php echo esc_html( $technology_software_firm_sv_bg_text ); ?></span>
				<?php endif; ?>
				<?php if ( ! empty( $technology_software_firm_sv_title ) ) : ?>
					<h2 class="cm-services__title"><?php echo esc_html( $technology_software_firm_sv_title ); ?></h2>
				<?php endif; ?>
			</div>
		</header>

		<?php if ( $technology_software_firm_blog_query->have_posts() ) : ?>
		<div class="cm-services__grid">
			<?php
			while ( $technology_software_firm_blog_query->have_posts() ) :
				$technology_software_firm_blog_query->the_post();
				$technology_software_firm_image_id = get_post_thumbnail_id();

				// Optional per-post icon class via custom fields.
				$technology_software_firm_icon_candidates = array(
					get_post_meta( get_the_ID(), 'technology_software_firm_service_icon', true ),
					get_post_meta( get_the_ID(), 'service_icon', true ),
					get_post_meta( get_the_ID(), '_service_icon', true ),
				);

				$technology_software_firm_service_icon = '';
				foreach ( $technology_software_firm_icon_candidates as $technology_software_firm_icon_candidate ) {
					if ( ! empty( $technology_software_firm_icon_candidate ) ) {
						$technology_software_firm_service_icon = $technology_software_firm_icon_candidate;
						break;
					}
				}

				if ( empty( $technology_software_firm_service_icon ) ) {
					$technology_software_firm_post_categories = get_the_category();
					if ( ! empty( $technology_software_firm_post_categories ) ) {
						foreach ( $technology_software_firm_post_categories as $technology_software_firm_post_category ) {
							if ( isset( $technology_software_firm_icon_fallback_map[ $technology_software_firm_post_category->slug ] ) ) {
								$technology_software_firm_service_icon = $technology_software_firm_icon_fallback_map[ $technology_software_firm_post_category->slug ];
								break;
							}
						}
					}
				}

				if ( empty( $technology_software_firm_service_icon ) ) {
					$technology_software_firm_service_icon = $technology_software_firm_default_service_icon;
				}

				$technology_software_firm_service_icon = trim( preg_replace( '/[^A-Za-z0-9\-\s]/', '', $technology_software_firm_service_icon ) );
				if ( empty( $technology_software_firm_service_icon ) ) {
					$technology_software_firm_service_icon = $technology_software_firm_default_service_icon;
				}

				$technology_software_firm_excerpt = get_the_excerpt();
				if ( empty( $technology_software_firm_excerpt ) ) {
					$technology_software_firm_excerpt = wp_strip_all_tags( get_the_content() );
				}
				$technology_software_firm_excerpt = wp_trim_words( $technology_software_firm_excerpt, 50, '...' );
			?>
			<div class="cm-services__slide">
				<article class="cm-services__card">

					<a href="<?php the_permalink(); ?>" class="cm-services__card-bg-link">
						<div class="cm-services__card-bg">
							<a href="<?php the_permalink(); ?>">
								<?php
								if ( ! empty( $technology_software_firm_image_id ) ) {
									echo wp_get_attachment_image(
										$technology_software_firm_image_id,
										'large',
										false,
										array(
											'class'    => 'cm-services__card-bg-img',
												'alt'      => esc_attr( get_the_title() ),
											'loading'  => 'lazy',
											'decoding' => 'async',
										)
									);
								} else {
									?>
									<img 
										src="<?php echo esc_url( get_template_directory_uri() . '/resource/img/services-default.png' ); ?>" 
										class="cm-services__card-bg-img" 
										alt="<?php echo esc_attr( get_the_title() ); ?>"
										loading="lazy"
										decoding="async"
									/>
									<?php
								}
								?>
							</a>
						</div>
					</a>

					<div class="cm-services__card-overlay" aria-hidden="true"></div>

					<div class="cm-services__card-info">
						<div class="cm-services__card-head">
							<div class="cm-services__card-icon" aria-hidden="true">
								<i class="<?php echo esc_attr( $technology_software_firm_service_icon ); ?>"></i>
							</div>
							<div class="cm-services__card-title-div" aria-hidden="true">
								<h3 class="cm-services__card-title">
									<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
									<span class="cm-services__card-divider" aria-hidden="true"></span>
								</h3>
							</div>
						</div>
						<?php if ( ! empty( $technology_software_firm_excerpt ) ) : ?>
							<p class="cm-services__card-excerpt"><?php echo esc_html( $technology_software_firm_excerpt ); ?></p>
						<?php endif; ?>
					</div>

				</article>
			</div>
			<?php endwhile; ?>
			<?php wp_reset_postdata(); ?>
		</div><!-- .cm-services__grid -->
		<?php endif; ?>

	</div><!-- .asterthemes-wrapper -->
</section><!-- #technology_software_firm_services_section -->