<?php
/**
 * Color taxonomy with hex meta.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Taxonomies;

use ThemeREX\AutoDealer\PostTypes\VehiclePostType;

class ColorTaxonomy {

	public const TAXONOMY = 'trx_ad_color';
	public const META_KEY = '_trx_ad_color_hex';

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

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

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

		register_term_meta(
			self::TAXONOMY,
			self::META_KEY,
			array(
				'type'              => 'string',
				'description'       => __( 'Hex colour code for this term.', 'trx-auto-dealer' ),
				'single'            => true,
				'show_in_rest'      => false,
				'sanitize_callback' => array( $this, 'sanitizeHex' ),
			)
		);
	}

	/**
	 * Sanitize a hex colour string.
	 *
	 * @param mixed $value Raw value.
	 * @return string Sanitized hex with leading #, or empty string.
	 */
	public function sanitizeHex( mixed $value ): string {
		if ( ! is_string( $value ) ) {
			return '';
		}

		$hex = sanitize_hex_color( $value );
		return $hex !== null ? $hex : '';
	}

	/**
	 * Return the default terms for this taxonomy.
	 *
	 * @return array<string, string> Name => Hex code.
	 */
	public function getDefaultTerms(): array {
		return array(
			'Black'  => '#000000',
			'White'  => '#FFFFFF',
			'Silver' => '#C0C0C0',
			'Grey'   => '#808080',
			'Red'    => '#FF0000',
			'Blue'   => '#0000FF',
			'Green'  => '#008000',
			'Brown'  => '#8B4513',
			'Orange' => '#FFA500',
			'Yellow' => '#FFFF00',
			'Gold'   => '#FFD700',
			'Purple' => '#800080',
			'Beige'  => '#F5F5DC',
		);
	}

	/**
	 * Insert default terms (with hex meta) 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 $name => $hex ) {
			$result = wp_insert_term( $name, self::TAXONOMY );
			if ( ! is_wp_error( $result ) && isset( $result['term_id'] ) ) {
				update_term_meta( (int) $result['term_id'], self::META_KEY, $hex );
			}
		}
	}
}
