<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
/**
 * Post summary renderer for Sifency Addons dashboard.
 * Provides a single function `sifency_addons_render_themebuilder_summary()` which
 * computes counts and renders the post-type grid used on the welcome page.
 *
 * This version is styled to match the modern dashboard UI, includes icons,
 * and has a "View More" feature.
 */

if ( ! function_exists( 'sifency_addons_render_themebuilder_summary' ) ) {
	function sifency_addons_render_themebuilder_summary() {
		$post_type_breakdown = array();
		$total_posts         = 0;
		$total_drafts        = 0;

		$cpt_slug      = 'themebuilder';
		$taxonomy_slug = 'sifency_builder_type';

		$theme_builder_terms = get_terms(
			array(
				'taxonomy'   => $taxonomy_slug,
				'hide_empty' => false,
			)
		);

		$card_icons = array(
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
			'dashicons-align-wide',
		);
		$icon_index = 0;

		if ( ! is_wp_error( $theme_builder_terms ) && ! empty( $theme_builder_terms ) ) {

			foreach ( $theme_builder_terms as $term ) {
				$is_cpt_disabled = false;
				if ( class_exists( 'Sifency_Addons_CPT_Module_Manager' ) ) {
					// Map exceptions
					$module_term_map = array(
						'doc'  => array( 'documentation', 'doc-archive' ),
						'team' => array( 'member', 'team-archive' ),
						'shop' => array( 'shop', 'product', 'shop-archive', 'product-archive' ),
						'blog' => array( 'post', 'post-archive' ),
					);

					// Core theme builder parts that aren't tied to a specific CPT
					$core_parts = array( 'header', 'footer', 'pre-before-footer', 'before-footer', 'subheader', 'after-subheader', 'error_404', 'popup', 'offcanvas', 'scrollup', 'topbar' );

					if ( ! in_array( $term->slug, $core_parts ) ) {
						// Figure out which CPT module this term belongs to
						$matched_module = '';
						foreach ( $module_term_map as $key => $slugs ) {
							if ( in_array( $term->slug, $slugs ) ) {
								$matched_module = $key;
								break;
							}
						}

						// Default fallback: if it's 'career' or 'career-archive', the module is 'career'
						if ( empty( $matched_module ) ) {
							$matched_module = str_replace( '-archive', '', $term->slug );
						}

						$all_modules = Sifency_Addons_CPT_Module_Manager::get_all_modules();

						// If the module was completely removed via filter, or is toggled off, disable the summary
						if ( ! isset( $all_modules[ $matched_module ] ) ) {
							$is_cpt_disabled = true;
						} elseif ( ! Sifency_Addons_CPT_Module_Manager::is_module_enabled( $matched_module ) ) {
							$is_cpt_disabled = true;
						}
					}
				}

				if ( $is_cpt_disabled ) {
					continue;
				}

				$query_args = array(
					'post_type'      => $cpt_slug,
					'post_status'    => 'any',
					'posts_per_page' => -1,
					'fields'         => 'ids',
					'tax_query'      => array(
						array(
							'taxonomy' => $taxonomy_slug,
							'field'    => 'slug',
							'terms'    => $term->slug,
						),
					),
				);
				$term_query = new WP_Query( $query_args );
				$term_count = $term_query->found_posts;

				$draft_query_args                = $query_args;
				$draft_query_args['post_status'] = 'draft';
				$draft_query                     = new WP_Query( $draft_query_args );
				$draft_count                     = $draft_query->found_posts;

				$total_posts  += $term_count;
				$total_drafts += $draft_count;

				$view_link = add_query_arg(
					array(
						'page' => 'sifency-theme-builder',
						'tab'  => $term->slug,
					),
					admin_url( 'admin.php' )
				);

				$post_type_breakdown[ 'themebuilder_' . $term->slug ] = array(
					'label'     => $term->name,
					'count'     => $term_count,
					'view_link' => $view_link,
					'icon'      => $card_icons[ $icon_index % count( $card_icons ) ],
				);

				$icon_index++;
			}
		}

		$post_type_breakdown = apply_filters( 'sifency_addons_themebuilder_summary_items', $post_type_breakdown );

		// ======================================================================
		// RENDER THE MARKUP
		// ======================================================================
		?>
		<?php $show_all = ! Sifency_Addons_Theme_Builder_Master::sifency_addons_selective_environment(); ?>
		<div class="sifency-themebuilder-summary-wrapper">
			<div id="sifency-themebuilder-grid" class="sifency-themebuilder-type-grid<?php echo $show_all ? ' sifency-grid-expanded' : ''; ?>">
				<!-- Individual Category Cards -->
				<?php foreach ( $post_type_breakdown as $post_type => $data ) : ?>
				<a href="<?php echo esc_url( $data['view_link'] ); ?>" class="sifency-themebuilder-type-item">
					<div class="sifency-card-icon-wrapper">
								<span class="dashicons <?php echo esc_attr( $data['icon'] ); ?>"></span>
							</div>
					<div class="sifency-themebuilder-count"><?php echo esc_html( $data['count'] ); ?></div>
					<h5><?php echo esc_html( $data['label'] ); ?></h5>
				</a>
				<?php endforeach; ?>
			</div>

			<?php if ( ! $show_all && count( $post_type_breakdown ) + 1 > 6 ) : ?>
			<div class="sifency-view-more-container">
				<button id="sifency-view-more-btn" class="button sifency-admin-btn" data-view-more="<?php esc_attr_e( 'View More', 'sifency-addons' ); ?>" data-view-less="<?php esc_attr_e( 'View Less', 'sifency-addons' ); ?>">
					<?php esc_html_e( 'View More', 'sifency-addons' ); ?>
				</button>
			</div>
			<?php endif; ?>
		</div>
		<?php
	}
}
