<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

require_once SIFENCY_ADDONS_DIR . 'helpers/post-summary-filter-helpers.php';

if ( ! function_exists( 'sifency_addons_render_post_summary' ) ) {
	/**
	 * Renders the dashboard post type summary grid.
	 */
	function sifency_addons_render_post_summary() {

		$cpt_config_file = SIFENCY_ADDONS_DIR . 'theme-builder/post-type/sifency-cpt-config.php';

		if ( ! file_exists( $cpt_config_file ) ) {
			echo '<div class="notice notice-error"><p>' . esc_html__( 'CPT Configuration file is missing.', 'sifency-addons' ) . '</p></div>';
			return;
		}

		$all_cpt_modules = include $cpt_config_file;

		$settings                    = get_option( 'sifency_addons_settings', array() );
		$sifency_addons_enabled_cpts = $settings['cpt_enabled'] ?? array();

		$is_pro_active = false;
		if ( class_exists( 'Sifency_Addons_Theme_Builder_Master' ) && method_exists( 'Sifency_Addons_Theme_Builder_Master', 'sync_environment_state' ) ) {
			$is_pro_active = (bool) Sifency_Addons_Theme_Builder_Master::sync_environment_state();
		}

		$custom_urls        = sifency_addons_get_dashboard_post_summary_custom_urls();
		$display_post_types = sifency_addons_get_dashboard_display_post_types();

		$post_type_breakdown = array();
		$total_posts         = 0;
		$total_drafts        = 0;

		foreach ( $display_post_types as $pt_slug ) {

			$counts      = wp_count_posts( $pt_slug );
			$count_total = is_object( $counts ) ? intval( array_sum( (array) $counts ) ) : 0;

			$total_posts  += $count_total;
			$total_drafts += isset( $counts->draft ) ? intval( $counts->draft ) : 0;

			$module_config = $all_cpt_modules[ $pt_slug ] ?? array();

			if ( empty( $module_config ) ) {
				$pt_object = get_post_type_object( $pt_slug );
				if ( $pt_object ) {
					$module_config['label'] = $pt_object->labels->name;
				} else {
					continue;
				}
			}

			$is_module_enabled = false;

			if ( ! empty( $sifency_addons_enabled_cpts[ $pt_slug ] ) && 'on' === $sifency_addons_enabled_cpts[ $pt_slug ] ) {
				$is_module_enabled = true;
			}

			if ( in_array( $pt_slug, array( 'post', 'page', 'product', 'themebuilder', 'section_templates' ), true ) ) {
				$is_module_enabled = true;
			}

			if ( class_exists( 'Sifency_Addons_CPT_Module_Manager' ) && Sifency_Addons_CPT_Module_Manager::is_module_enabled( $pt_slug ) ) {
				$is_module_enabled = true;
			}

			if ( ! empty( $module_config['sync_cpt'] ) && ! $is_pro_active ) {
				$status = 'pro_locked';
			} elseif ( $is_module_enabled ) {
				$status = 'enabled';
			} else {
				$status = 'inactive';
			}

			$post_type_breakdown[ $pt_slug ] = array(
				'label'       => sanitize_text_field( $module_config['label'] ),
				'count'       => $count_total,
				'status'      => $status,
				'create_link' => isset( $custom_urls[ $pt_slug ]['create'] )
									? esc_url( $custom_urls[ $pt_slug ]['create'] )
									: esc_url( admin_url( 'post-new.php?post_type=' . $pt_slug ) ),

				'view_link'   => isset( $custom_urls[ $pt_slug ]['view'] )
									? esc_url( $custom_urls[ $pt_slug ]['view'] )
									: esc_url( admin_url( 'edit.php?post_type=' . $pt_slug ) ),
			);
		}
		?>

		<div class="sifency-post-summary">
			<div class="sifency-post-type-grid">

				<div class="sifency-post-type-item">
					<h4><?php esc_html_e( 'All Post Items', 'sifency-addons' ); ?></h4>
					<div class="sifency-summary-value"><?php echo esc_html( $total_posts ); ?></div>
					<small>
						<?php
						printf(
							/* translators: %d: drafts */
							esc_html__( '%d drafts', 'sifency-addons' ),
							intval( $total_drafts )
						);
						?>
					</small>
				</div>

				<?php foreach ( $post_type_breakdown as $data ) : ?>
					<div class="sifency-post-type-item">

						<div class="sifency-post-count-wrapper">
							<div class="sifency-post-count"><?php echo esc_html( $data['count'] ); ?></div>

							<?php if ( $data['status'] === 'pro_locked' && ! $is_pro_active ) : ?>
								<span class="sifency-pro-badge"><?php esc_html_e( 'Pro', 'sifency-addons' ); ?></span>
							<?php endif; ?>

							<?php if ( $data['status'] === 'inactive' ) : ?>
								<span class="sifency-status-badge badge-inactive sifency-badge"><?php esc_html_e( 'Inactive', 'sifency-addons' ); ?></span>
							<?php endif; ?>
						</div>

						<h5><?php echo esc_html( $data['label'] ); ?></h5>

						<?php if ( $data['status'] === 'enabled' || ( $data['status'] === 'pro_locked' && $is_pro_active ) ) : ?>
							<div class="sifency-post-item-actions">

								<a href="<?php echo esc_url( $data['create_link'] ); ?>"
								   class="sifency-create-btn sifency-admin-btn"
								   title="<?php echo esc_attr( /* translators: %s: post */ sprintf( __( 'Create new %s', 'sifency-addons' ), $data['label'] ) ); ?>">
								   <span class="dashicons dashicons-plus-alt"></span>
								   <?php esc_html_e( 'New', 'sifency-addons' ); ?>
								</a>

								<?php if ( $data['count'] > 0 ) : ?>
									<a href="<?php echo esc_url( $data['view_link'] ); ?>"
									   class="sifency-view-btn sifency-admin-btn"
									   title="<?php echo esc_attr( /* translators: %s: post type label */ sprintf( __( 'View all %s', 'sifency-addons' ), $data['label'] ) ); ?>">
									   <span class="dashicons dashicons-visibility"></span>
									   <?php esc_html_e( 'View', 'sifency-addons' ); ?>
									</a>
								<?php endif; ?>

							</div>
						<?php endif; ?>

					</div>
				<?php endforeach; ?>

			</div>
		</div>
		<?php
	}
}
