<?php
/**
 * FoodOrd Products Shortcode Class
 *
 * @package FoodOrd
 */

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

/**
 * Class Foodord_Products_Shortcode
 */
class Foodord_Products_Shortcode {

	/**
	 * Initialize the shortcode
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'register_shortcode' ) );
	}

	/**
	 * Register the shortcode
	 */
	public function register_shortcode() {
		add_shortcode( 'foodord_products', array( $this, 'render_products' ) );
	}

	/**
	 * Enqueue shortcode styles
	 */
	private function enqueue_styles() {
		wp_enqueue_style(
			'foodord-products-shortcode',
			FOODORD_URL . 'public/css/shortcode-products.css',
			array(),
			FOODORD_VERSION,
			'all'
		);
	}

	/**
	 * Render products shortcode
	 *
	 * @param array $atts Shortcode attributes.
	 * @return string HTML output
	 */
	public function render_products( $atts ) {
		// Enqueue styles only when shortcode is used.
		$this->enqueue_styles();

		// Parse shortcode attributes.
		$atts = shortcode_atts(
			array(
				'limit'   => -1,
				'columns' => 4,
				'orderby' => 'date',
				'order'   => 'DESC',
			),
			$atts,
			'foodord_products'
		);

		// Mark that shortcode was used on this request.
		$GLOBALS['foodord_has_products_shortcode'] = true;
		// Use the shared renderer to maintain consistency with block.
		return Foodord_Products_Renderer::render( $atts );
	}
}
