<?php
/**
 * Feature taxonomy (hierarchical with parent groups).
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Taxonomies;

use ThemeREX\AutoDealer\PostTypes\VehiclePostType;

class FeatureTaxonomy {

	public const TAXONOMY = 'trx_ad_feature';

	/**
	 * Register the taxonomy.
	 *
	 * @return void
	 */
	public function register(): void {
		$labels = array(
			'name'                  => _x( 'Features', 'Taxonomy general name', 'trx-auto-dealer' ),
			'singular_name'         => _x( 'Feature', 'Taxonomy singular name', 'trx-auto-dealer' ),
			'search_items'          => __( 'Search Features', 'trx-auto-dealer' ),
			'all_items'             => __( 'All Features', 'trx-auto-dealer' ),
			'parent_item'           => __( 'Feature Group', 'trx-auto-dealer' ),
			'parent_item_colon'     => __( 'Feature Group:', 'trx-auto-dealer' ),
			'edit_item'             => __( 'Edit Feature', 'trx-auto-dealer' ),
			'update_item'           => __( 'Update Feature', 'trx-auto-dealer' ),
			'add_new_item'          => __( 'Add New Feature', 'trx-auto-dealer' ),
			'new_item_name'         => __( 'New Feature Name', 'trx-auto-dealer' ),
			'menu_name'             => __( 'Features', 'trx-auto-dealer' ),
			'not_found'             => __( 'No features found.', 'trx-auto-dealer' ),
			'no_terms'              => __( 'No features', 'trx-auto-dealer' ),
			'items_list_navigation' => __( 'Features list navigation', 'trx-auto-dealer' ),
			'items_list'            => __( 'Features list', 'trx-auto-dealer' ),
			'back_to_items'         => __( '&larr; Back to Features', 'trx-auto-dealer' ),
		);

		$args = array(
			'labels'            => $labels,
			'hierarchical'      => true,
			'public'            => true,
			'show_ui'           => true,
			'show_admin_column' => false,
			'show_in_nav_menus' => true,
			'show_tagcloud'     => false,
			'show_in_rest'      => false,
			'rewrite'           => array(
				'slug'         => 'feature',
				'with_front'   => false,
				'hierarchical' => true,
			),
		);

		register_taxonomy( self::TAXONOMY, VehiclePostType::POST_TYPE, $args );
	}

	/**
	 * Return the default terms for this taxonomy (groups with children).
	 *
	 * @return array<string, string[]> Group name => child terms.
	 */
	public function getDefaultTerms(): array {
		return array(
			'Safety'        => array(
				'ABS',
				'Airbags',
				'Traction Control',
				'Stability Control',
			),
			'Comfort'       => array(
				'Air Conditioning',
				'Climate Control',
				'Heated Seats',
				'Power Seats',
			),
			'Technology'    => array(
				'Bluetooth',
				'Navigation System',
				'Apple CarPlay',
				'Android Auto',
			),
			'Exterior'      => array(
				'Alloy Wheels',
				'LED Headlights',
				'Sunroof',
				'Roof Rails',
			),
			'Interior'      => array(
				'Leather Seats',
				'Keyless Entry',
				'Push Button Start',
				'Ambient Lighting',
			),
			'Driver Assist' => array(
				'Cruise Control',
				'Parking Sensors',
				'Backup Camera',
				'Blind Spot Monitor',
			),
		);
	}

	/**
	 * Insert default terms (groups + children) if the taxonomy is empty.
	 *
	 * @return void
	 */
	public function insertDefaultTerms(): void {
		$existing = wp_count_terms(
			array(
				'taxonomy'   => self::TAXONOMY,
				'hide_empty' => false,
			)
		);
		if ( is_wp_error( $existing ) || (int) $existing > 0 ) {
			return;
		}

		foreach ( $this->getDefaultTerms() as $group => $children ) {
			$parent_result = wp_insert_term( $group, self::TAXONOMY );
			if ( is_wp_error( $parent_result ) ) {
				continue;
			}

			$parent_id = (int) $parent_result['term_id'];

			foreach ( $children as $child ) {
				wp_insert_term( $child, self::TAXONOMY, array( 'parent' => $parent_id ) );
			}
		}
	}
}
