<?php
/**
 * CPT Builder - Taxonomies tab.
 *
 * @package CptBuilder
 *
 * @var array<string, mixed> $definition
 */

namespace CptBuilder;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$selected      = isset( $definition['taxonomies'] ) && is_array( $definition['taxonomies'] ) ? array_map( 'strval', $definition['taxonomies'] ) : [];
$registered    = get_taxonomies( [], 'objects' );
$builder_taxes = ( new TaxonomyStorage() )->all();

// Taxonomies attached to this post type from the taxonomy editor's
// "Post Types" tab. Shown as locked: that connection is managed there.
$pt_key            = isset( $definition['key'] ) ? (string) $definition['key'] : '';
$attached_via_tax  = [];
if ( $pt_key !== '' ) {
	foreach ( $builder_taxes as $tax_key => $tax_definition ) {
		$tax_object_types = isset( $tax_definition['object_types'] ) && is_array( $tax_definition['object_types'] )
			? array_map( 'strval', $tax_definition['object_types'] )
			: [];
		if ( in_array( $pt_key, $tax_object_types, true ) ) {
			$attached_via_tax[] = (string) $tax_key;
		}
	}
}

$candidates = [];
foreach ( $registered as $tax ) {
	if ( ! $tax instanceof \WP_Taxonomy ) {
		continue;
	}
	// Skip internal/private taxonomies that should not be attached manually.
	if ( in_array( $tax->name, [ 'nav_menu', 'link_category', 'post_format', 'wp_pattern_category', 'wp_theme', 'wp_template_part_area' ], true ) ) {
		continue;
	}
	// Only offer real content taxonomies: public ones, or taxonomies created
	// with the CPT Builder. This hides plugin internals (WooCommerce product
	// visibility/type, Elementor, CartFlows, etc.).
	if ( ! $tax->public && ! isset( $builder_taxes[ $tax->name ] ) ) {
		continue;
	}
	$candidates[ $tax->name ] = $tax->labels->singular_name ?? $tax->name;
}

// Ensure previously-selected taxonomies remain listed even if not registered yet.
foreach ( array_merge( $selected, $attached_via_tax ) as $sel ) {
	if ( ! isset( $candidates[ $sel ] ) ) {
		$candidates[ $sel ] = $sel;
	}
}

ksort( $candidates );
?>
<h2><?php esc_html_e( 'Taxonomies', 'wp-powersuite' ); ?></h2>
<p class="description"><?php esc_html_e( 'Connect existing taxonomies (built-in or custom) to this post type. Connections are stored and re-applied on every load, so taxonomies registered later still attach correctly.', 'wp-powersuite' ); ?></p>
<?php if ( $attached_via_tax !== [] ) : ?>
	<p class="description"><?php esc_html_e( 'Locked items below are connected from the taxonomy editor (Taxonomies → edit → Post Types tab). To disconnect them, uncheck this post type there.', 'wp-powersuite' ); ?></p>
<?php endif; ?>

<?php if ( $candidates === [] ) : ?>
	<p><?php esc_html_e( 'No taxonomies are available to connect yet.', 'wp-powersuite' ); ?></p>
<?php else : ?>
	<div class="cptb-checkbox-grid">
		<?php foreach ( $candidates as $tax_slug => $tax_label ) : ?>
			<?php $is_locked = in_array( (string) $tax_slug, $attached_via_tax, true ); ?>
			<label class="cptb-checkbox-card<?php echo $is_locked ? ' cptb-checkbox-card--locked' : ''; ?>">
				<input type="checkbox" name="taxonomies[]" value="<?php echo esc_attr( $tax_slug ); ?>" <?php checked( $is_locked || in_array( (string) $tax_slug, $selected, true ) ); ?> <?php disabled( $is_locked ); ?>>
				<span class="cptb-checkbox-card__title"><?php echo esc_html( $tax_label ); ?></span>
				<span class="cptb-checkbox-card__help">
					<code><?php echo esc_html( (string) $tax_slug ); ?></code>
					<?php if ( $is_locked ) : ?>
						<em><?php esc_html_e( 'via taxonomy settings', 'wp-powersuite' ); ?></em>
					<?php endif; ?>
				</span>
			</label>
		<?php endforeach; ?>
	</div>
<?php endif; ?>
