<?php
namespace WooNooW\Compat;

/**
 * MetaFieldsRegistry
 * 
 * Allows plugins to register custom meta fields for display in WooNooW admin.
 * Part of Level 1 compatibility - plugins using standard WP/WooCommerce meta
 * storage can register their fields to be displayed automatically.
 * 
 * Zero coupling with specific plugins - just provides the mechanism.
 * 
 * @package WooNooW\Compat
 */
class MetaFieldsRegistry {
	
	/**
	 * Registered order meta fields
	 * @var array
	 */
	private static $order_fields = [];
	
	/**
	 * Registered product meta fields
	 * @var array
	 */
	private static $product_fields = [];
	
	/**
	 * Initialize the registry
	 */
	public static function init() {
		// Allow plugins to register their meta fields
		add_action('init', [__CLASS__, 'trigger_registration'], 20);
		
		// Localize fields to JavaScript
		add_action('admin_enqueue_scripts', [__CLASS__, 'localize_fields']);
	}
	
	/**
	 * Trigger the registration action for plugins
	 */
	public static function trigger_registration() {
		/**
		 * Fires when plugins can register their meta fields
		 * 
		 * @since 1.0.0
		 * 
		 * @example
		 * add_action('woonoow/register_meta_fields', function() {
		 *     \WooNooW\Compat\MetaFieldsRegistry::register_order_field('_tracking_number', [
		 *         'label' => __('Tracking Number'),
		 *         'type' => 'text',
		 *         'section' => 'Shipment Tracking',
		 *     ]);
		 * });
		 */
		do_action('woonoow/register_meta_fields');
	}
	
	/**
	 * Register an order meta field
	 * 
	 * @param string $key Meta key (e.g., '_tracking_number')
	 * @param array $args Field configuration
	 *   - label: string - Field label (default: formatted key)
	 *   - type: string - Field type: text, textarea, number, select, date, checkbox (default: text)
	 *   - section: string - Section name for grouping (default: 'Additional Fields')
	 *   - description: string - Help text (default: '')
	 *   - placeholder: string - Placeholder text (default: '')
	 *   - options: array - For select type: [{value: 'x', label: 'X'}] (default: [])
	 */
	public static function register_order_field($key, $args = []) {
		$defaults = [
			'key' => $key,
			'label' => self::format_label($key),
			'type' => 'text',
			'section' => 'Additional Fields',
			'description' => '',
			'placeholder' => '',
			'options' => [],
		];
		
		self::$order_fields[$key] = array_merge($defaults, $args);
		
		// Auto-add to allowed meta lists (Level 1 compatibility)
		self::add_to_allowed_meta('order', $key);
	}
	
	/**
	 * Register a product meta field
	 * 
	 * @param string $key Meta key (e.g., '_custom_field')
	 * @param array $args Field configuration (same as register_order_field)
	 */
	public static function register_product_field($key, $args = []) {
		$defaults = [
			'key' => $key,
			'label' => self::format_label($key),
			'type' => 'text',
			'section' => 'Additional Fields',
			'description' => '',
			'placeholder' => '',
			'options' => [],
		];
		
		self::$product_fields[$key] = array_merge($defaults, $args);
		
		// Auto-add to allowed meta lists (Level 1 compatibility)
		self::add_to_allowed_meta('product', $key);
	}
	
	/**
	 * Format meta key to human-readable label
	 * 
	 * @param string $key Meta key
	 * @return string Formatted label
	 */
	private static function format_label($key) {
		// Remove leading underscore
		$label = ltrim($key, '_');
		
		// Replace underscores with spaces
		$label = str_replace('_', ' ', $label);
		
		// Capitalize words
		$label = ucwords($label);
		
		return $label;
	}
	
	/**
	 * Add meta key to allowed lists automatically
	 * 
	 * @param string $type 'order' or 'product'
	 * @param string $key Meta key
	 */
	private static function add_to_allowed_meta($type, $key) {
		if ($type === 'order') {
			// Add to allowed private meta (for reading)
			add_filter('woonoow/order_allowed_private_meta', function($allowed) use ($key) {
				if (!in_array($key, $allowed, true)) {
					$allowed[] = $key;
				}
				return $allowed;
			});
			
			// Add to updatable meta (for writing)
			add_filter('woonoow/order_updatable_meta', function($allowed) use ($key) {
				if (!in_array($key, $allowed, true)) {
					$allowed[] = $key;
				}
				return $allowed;
			});
		} elseif ($type === 'product') {
			// Add to allowed private meta (for reading)
			add_filter('woonoow/product_allowed_private_meta', function($allowed) use ($key) {
				if (!in_array($key, $allowed, true)) {
					$allowed[] = $key;
				}
				return $allowed;
			});
			
			// Add to updatable meta (for writing)
			add_filter('woonoow/product_updatable_meta', function($allowed) use ($key) {
				if (!in_array($key, $allowed, true)) {
					$allowed[] = $key;
				}
				return $allowed;
			});
		}
	}
	
	/**
	 * Localize fields to JavaScript
	 * Exposes registered fields to frontend via window.WooNooWMetaFields
	 */
	public static function localize_fields() {
		// Only on admin pages
		if (!is_admin()) {
			return;
		}
		
		// Allow plugins to modify fields before localizing
		$order_fields = apply_filters('woonoow/meta_fields_orders', array_values(self::$order_fields));
		$product_fields = apply_filters('woonoow/meta_fields_products', array_values(self::$product_fields));
		
		// Localize to JavaScript
		wp_localize_script('woonoow-admin', 'WooNooWMetaFields', [
			'orders' => $order_fields,
			'products' => $product_fields,
		]);
	}
	
	/**
	 * Get registered order fields (for testing/debugging)
	 * 
	 * @return array
	 */
	public static function get_order_fields() {
		return self::$order_fields;
	}
	
	/**
	 * Get registered product fields (for testing/debugging)
	 * 
	 * @return array
	 */
	public static function get_product_fields() {
		return self::$product_fields;
	}
}
