<?php
/**
 * FoodOrd product labels helper.
 *
 * @package FoodOrd
 */

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

/**
 * Handles product label definitions, storage normalization, and frontend markup.
 */
class Foodord_Product_Labels {

	/**
	 * Product meta key for selected labels.
	 */
	public const META_KEY = '_foodord_product_labels';

	/**
	 * Get supported product labels.
	 *
	 * @return array<string,array<string,string>>
	 */
	public static function get_definitions(): array {
		return array(
			'vegetarian'  => array(
				'label' => __( 'Vegetarian', 'foodord' ),
				'icon'  => 'leaf',
			),
			'vegan'       => array(
				'label' => __( 'Vegan', 'foodord' ),
				'icon'  => 'sprout',
			),
			'spicy'       => array(
				'label' => __( 'Spicy', 'foodord' ),
				'icon'  => 'pepper',
			),
			'very_spicy'  => array(
				'label' => __( 'Very spicy', 'foodord' ),
				'icon'  => 'flame',
			),
			'halal'       => array(
				'label' => __( 'Halal', 'foodord' ),
				'icon'  => 'halal',
			),
			'gluten_free' => array(
				'label' => __( 'Gluten free', 'foodord' ),
				'icon'  => 'wheat',
			),
			'popular'     => array(
				'label' => __( 'Popular', 'foodord' ),
				'icon'  => 'star',
			),
			'new'         => array(
				'label' => __( 'New', 'foodord' ),
				'icon'  => 'sparkle',
			),
		);
	}

	/**
	 * Sanitize selected label slugs against supported definitions.
	 *
	 * @param mixed $labels Raw label values.
	 * @return array<int,string>
	 */
	public static function sanitize_labels( $labels ): array {
		if ( ! is_array( $labels ) ) {
			return array();
		}

		$selected    = array_values( array_unique( array_map( 'sanitize_key', $labels ) ) );
		$definitions = self::get_definitions();
		$ordered     = array();

		foreach ( array_keys( $definitions ) as $slug ) {
			if ( in_array( $slug, $selected, true ) ) {
				$ordered[] = $slug;
			}
		}

		return $ordered;
	}

	/**
	 * Get selected label slugs for a product.
	 *
	 * @param int $product_id Product ID.
	 * @return array<int,string>
	 */
	public static function get_product_labels( int $product_id ): array {
		if ( $product_id <= 0 ) {
			return array();
		}

		return self::sanitize_labels( get_post_meta( $product_id, self::META_KEY, true ) );
	}

	/**
	 * Build frontend product labels markup.
	 *
	 * @param int    $product_id Product ID.
	 * @param string $context    Output context.
	 * @return string
	 */
	public static function get_product_labels_html( int $product_id, string $context = 'card' ): string {
		$selected = self::get_product_labels( $product_id );
		if ( empty( $selected ) ) {
			return '';
		}

		$context     = in_array( $context, array( 'card', 'modal' ), true ) ? $context : 'card';
		$definitions = self::get_definitions();
		$items       = array();

		foreach ( $selected as $slug ) {
			if ( ! isset( $definitions[ $slug ] ) ) {
				continue;
			}

			$label = (string) $definitions[ $slug ]['label'];
			if ( '' === $label ) {
				continue;
			}

			$icon = self::get_icon_markup( (string) $definitions[ $slug ]['icon'] );

			$items[] = sprintf(
				'<span class="foodord-product-label foodord-product-label--%1$s"><span class="foodord-product-label__icon" aria-hidden="true">%2$s</span><span class="foodord-product-label__text">%3$s</span></span>',
				esc_attr( sanitize_html_class( $slug ) ),
				$icon,
				esc_html( $label )
			);
		}

		if ( empty( $items ) ) {
			return '';
		}

		return sprintf(
			'<div class="foodord-product-labels foodord-product-labels--%1$s" aria-label="%2$s">%3$s</div>',
			esc_attr( sanitize_html_class( $context ) ),
			esc_attr__( 'Product labels', 'foodord' ),
			implode( '', $items )
		);
	}

	/**
	 * Allowed HTML for product label markup.
	 *
	 * @return array<string,array<string,bool>>
	 */
	public static function get_allowed_html(): array {
		return array(
			'div'    => array(
				'class'      => true,
				'aria-label' => true,
			),
			'span'   => array(
				'class'       => true,
				'aria-hidden' => true,
			),
			'svg'    => array(
				'class'       => true,
				'viewBox'     => true,
				'viewbox'     => true,
				'aria-hidden' => true,
			),
			'path'   => array(
				'class' => true,
				'd'     => true,
			),
			'circle' => array(
				'class' => true,
				'cx'    => true,
				'cy'    => true,
				'r'     => true,
			),
			'text'   => array(
				'class'             => true,
				'x'                 => true,
				'y'                 => true,
				'text-anchor'       => true,
				'dominant-baseline' => true,
				'direction'         => true,
			),
		);
	}

	/**
	 * Build a controlled inline icon.
	 *
	 * @param string $icon Icon key.
	 * @return string
	 */
	private static function get_icon_markup( string $icon ): string {
		$paths = array(
			'leaf'    => '<path class="foodord-product-label__icon-path" d="M5 19C5 10 10 5 20 4C19 14 14 19 5 19Z"></path><path class="foodord-product-label__icon-path" d="M5 19C9 13 13 9 20 4"></path>',
			'sprout' => '<path class="foodord-product-label__icon-path" d="M12 20V10"></path><path class="foodord-product-label__icon-path" d="M12 10C8 10 5 8 4 4C8 4 11 6 12 10Z"></path><path class="foodord-product-label__icon-path" d="M12 10C16 10 19 8 20 4C16 4 13 6 12 10Z"></path>',
			'pepper' => '<path class="foodord-product-label__icon-path" d="M15 4C15 6 16 7 18 7"></path><path class="foodord-product-label__icon-path" d="M18 7C15 7 12 9 10 12L6 17C5 18 5 20 7 21C10 22 15 19 18 15C21 11 21 8 18 7Z"></path>',
			'flame'  => '<path class="foodord-product-label__icon-path" d="M12 21C8 21 5 18 5 14C5 10 8 8 9 5C12 7 12 10 12 12C14 10 15 8 15 5C18 8 20 11 20 15C20 18 17 21 12 21Z"></path>',
			'halal'  => '<text class="foodord-product-label__icon-text" x="16" y="10.5" text-anchor="middle" dominant-baseline="middle" direction="rtl">&#1581;&#1604;&#1575;&#1604;</text>',
			'wheat'  => '<path class="foodord-product-label__icon-path" d="M12 21V4"></path><path class="foodord-product-label__icon-path" d="M8 8C10 8 12 9 12 12C10 12 8 11 8 8Z"></path><path class="foodord-product-label__icon-path" d="M16 8C14 8 12 9 12 12C14 12 16 11 16 8Z"></path><path class="foodord-product-label__icon-path" d="M5 5L19 19"></path>',
			'star'   => '<path class="foodord-product-label__icon-path" d="M12 4L14.4 9L20 9.8L16 13.7L16.9 19.2L12 16.6L7.1 19.2L8 13.7L4 9.8L9.6 9L12 4Z"></path>',
			'sparkle' => '<path class="foodord-product-label__icon-path" d="M12 3L13.8 8.2L19 10L13.8 11.8L12 17L10.2 11.8L5 10L10.2 8.2L12 3Z"></path><path class="foodord-product-label__icon-path" d="M19 15L19.8 17.2L22 18L19.8 18.8L19 21L18.2 18.8L16 18L18.2 17.2L19 15Z"></path>',
		);

		$path_markup = $paths[ $icon ] ?? $paths['leaf'];
		$view_box    = 'halal' === $icon ? '0 0 32 20' : '0 0 24 24';

		return '<svg class="foodord-product-label__icon-svg" viewBox="' . esc_attr( $view_box ) . '" aria-hidden="true">' . $path_markup . '</svg>';
	}
}
