<?php
/**
 * Vehicle Type taxonomy.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Taxonomies;

use ThemeREX\AutoDealer\PostTypes\VehiclePostType;

class VehicleTypeTaxonomy {

	public const TAXONOMY = 'trx_ad_vehicle_type';

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

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

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

	/**
	 * Return the default terms for this taxonomy.
	 *
	 * @return string[]
	 */
	public function getDefaultTerms(): array {
		return array(
			'Car',
			'Truck',
			'Motorcycle',
			'Boat',
			'RV',
			'ATV',
			'Commercial',
		);
	}

	/**
	 * Insert default terms 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 $term ) {
			wp_insert_term( $term, self::TAXONOMY );
		}
	}
}
