<?php
/**
 * Shortcodes Admin Page for FoodOrd.
 *
 * @package FoodOrd
 */

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

/**
 * Class Foodord_Shortcodes
 */
class Foodord_Shortcodes {
	/**
	 * Retrieve potential admin hook slugs for the shortcodes page.
	 *
	 * @return string[]
	 */
	private function get_page_slugs(): array {
		return function_exists( 'foodord_get_settings_page_slugs' )
			? foodord_get_settings_page_slugs( 'shortcodes' )
			: array( 'foodord-theme-settings-shortcodes', 'foodord-shortcodes' );
	}

	/**
	 * Initialize shortcodes functionality.
	 */
	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
	}

	/**
	 * Enqueue admin scripts and styles for shortcodes page.
	 *
	 * @param string $hook Hook suffix.
	 */
	public function enqueue_scripts( $hook ) {
		$matches_page = false;
		foreach ( $this->get_page_slugs() as $slug ) {
			if ( '' !== $slug && false !== strpos( (string) $hook, $slug ) ) {
				$matches_page = true;
				break;
			}
		}

		if ( ! $matches_page ) {
			return;
		}

		// Enqueue CSS.
		wp_enqueue_style(
			'wrc-shortcodes',
			plugin_dir_url( __DIR__ ) . 'admin/css/wrc-shortcodes.css',
			array(),
			FOODORD_VERSION
		);

		// Enqueue JavaScript.
		wp_enqueue_script(
			'wrc-shortcodes',
			plugin_dir_url( __DIR__ ) . 'admin/js/wrc-shortcodes.js',
			array( 'jquery' ),
			FOODORD_VERSION,
			true
		);
	}

	/**
	 * Render the Shortcodes settings page.
	 */
	public function render_shortcodes_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		?>
		<div class="wrap foodord-shortcodes">
			<h1 class="wr-shortcodes-title"><?php echo esc_html__( 'Shortcodes', 'foodord' ); ?></h1>
			<p class="page-description">
				<?php esc_html_e( 'Copy and paste these shortcodes anywhere in your content.', 'foodord' ); ?>
			</p>
			<?php
			if ( function_exists( 'foodord_admin_render_horizontal_menu' ) ) {
				foodord_admin_render_horizontal_menu();
			}
			?>

			<!-- FoodOrd Custom Shortcodes -->
			<div class="shortcode-group">
				<h2 class="group-title">
					<span class="dashicons dashicons-cart"></span>
					<?php esc_html_e( 'FoodOrd Shortcodes', 'foodord' ); ?>
				</h2>
				<div class="shortcode-grid">
					<div class="shortcode-card" data-card-id="foodord-card">
						<div class="shortcode-header">
							<h3 class="shortcode-card-title"><?php esc_html_e( 'Shopping Cart', 'foodord' ); ?></h3>
							<span class="shortcode-badge">Custom</span>
						</div>
						<div class="shortcode-content">
							<div class="shortcode-code">
								<code>[foodord_card]</code>
								<button class="copy-btn" data-code="[foodord_card]" title="<?php esc_attr_e( 'Copy to clipboard', 'foodord' ); ?>">
									<span class="dashicons dashicons-admin-page"></span>
								</button>
							</div>
							<p class="shortcode-desc">
								<?php esc_html_e( 'Displays a minimal shopping cart widget with add/remove functionality.', 'foodord' ); ?>
							</p>
						</div>
					</div>

					<div class="shortcode-card" data-card-id="foodord-products">
						<div class="shortcode-header">
							<h3 class="shortcode-card-title"><?php esc_html_e( 'Product Grid', 'foodord' ); ?></h3>
							<span class="shortcode-badge">Custom</span>
						</div>
						<div class="shortcode-content">
							<div class="shortcode-code">
								<code>[foodord_products]</code>
								<button class="copy-btn" data-code="[foodord_products]" title="<?php esc_attr_e( 'Copy to clipboard', 'foodord' ); ?>">
									<span class="dashicons dashicons-admin-page"></span>
								</button>
							</div>
							<p class="shortcode-desc">
								<?php esc_html_e( 'Display products in a customizable grid layout.', 'foodord' ); ?>
							</p>
							<details class="shortcode-params" id="params-foodord-products">
								<summary><?php esc_html_e( 'Parameters', 'foodord' ); ?></summary>
								<div class="params-content">
									<ul>
										<li><code>limit</code> - <?php esc_html_e( 'Number of products (default: -1)', 'foodord' ); ?></li>
										<li><code>columns</code> - <?php esc_html_e( 'Grid columns (default: 4)', 'foodord' ); ?></li>
										<li><code>orderby</code> - <?php esc_html_e( 'Sort by: date, title, menu_order', 'foodord' ); ?></li>
										<li><code>order</code> - <?php esc_html_e( 'ASC or DESC', 'foodord' ); ?></li>
									</ul>
									<div class="example-code">
										<strong><?php esc_html_e( 'Example:', 'foodord' ); ?></strong>
										<code><?php echo esc_html( '[foodord_products limit="6" columns="3"]' ); ?></code>
									</div>
								</div>
							</details>
					</div>
				</div>
			</div>

			<div class="shortcode-group">
				<h2 class="group-title">
					<span class="dashicons dashicons-forms"></span>
					<?php esc_html_e( 'WooCommerce Core Shortcodes', 'foodord' ); ?>
				</h2>
				<div class="shortcode-grid">
					<div class="shortcode-card" data-card-id="woocommerce-checkout">
						<div class="shortcode-header">
							<h3 class="shortcode-card-title"><?php esc_html_e( 'Checkout Page', 'foodord' ); ?></h3>
							<span class="shortcode-badge">WooCommerce</span>
						</div>
						<div class="shortcode-content">
							<div class="shortcode-code">
								<code>[woocommerce_checkout]</code>
								<button class="copy-btn" data-code="[woocommerce_checkout]" title="<?php esc_attr_e( 'Copy to clipboard', 'foodord' ); ?>">
									<span class="dashicons dashicons-admin-page"></span>
								</button>
							</div>
					<p class="shortcode-desc">
						<?php esc_html_e( 'Displays the classic WooCommerce checkout form.', 'foodord' ); ?>
					</p>
						</div>
					</div>
				</div>
			</div>
		</div>
		</div>
		<?php
	}
}