<?php
/**
 * Shared Preciousuite module registry.
 *
 * @package Preciousuite
 */

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

if (!class_exists('PreciousSuite_Registry', false)) {
	final class PreciousSuite_Registry
	{
		private static $instance = null;

		/**
		 * Registered active modules.
		 *
		 * @var array
		 */
		private $modules = array();

		public static function instance()
		{
			if (null === self::$instance) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		public function register(array $module)
		{
			$module = wp_parse_args(
				$module,
				array(
					'slug' => '',
					'name' => '',
					'label' => '',
					'description' => '',
					'settings_url' => '',
					'plugin_file' => '',
					'wporg_slug' => '',
					'position' => 50,
					'stats' => null,
				)
			);

			if (empty($module['slug']) || empty($module['name'])) {
				return;
			}

			$this->modules[sanitize_key($module['slug'])] = $module;
		}

		public function all()
		{
			$modules = $this->modules;

			uasort(
				$modules,
				static function ($a, $b) {
					return (int) $a['position'] <=> (int) $b['position'];
				}
			);

			return $modules;
		}
	}
}

if (!function_exists('precioussuite_register_module')) {
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Part of shared suite API.
	function precioussuite_register_module(array $module)
	{
		PreciousSuite_Registry::instance()->register($module);
	}
}

if (!function_exists('precioussuite_get_catalog')) {
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Part of shared suite API.
	function precioussuite_get_catalog()
	{
		return array(
			'gold-price' => array(
				'name' => __('Gold Price Plugin', 'preciousuite-gold-buyback-quote-calculator'),
				'label' => __('Product Pricing', 'preciousuite-gold-buyback-quote-calculator'),
				'wporg_slug' => 'gold-price-based-on-weight',
				'description' => __('Calculate jewelry product prices from metal, weight, and current rates.', 'preciousuite-gold-buyback-quote-calculator'),
			),
			'gold-price-widgets' => array(
				'name' => __('Metal Rate Charts Plugin', 'preciousuite-gold-buyback-quote-calculator'),
				'label' => __('Widgets & API', 'preciousuite-gold-buyback-quote-calculator'),
				'wporg_slug' => 'preciousuite-metal-rate-charts',
				'description' => __('Display custom metal rate charts with a modern UI.', 'preciousuite-gold-buyback-quote-calculator'),
			),
			'buyback-calculator' => array(
				'name' => __('Gold Buyback Plugin', 'preciousuite-gold-buyback-quote-calculator'),
				'label' => __('Buyback Calculator', 'preciousuite-gold-buyback-quote-calculator'),
				'wporg_slug' => 'preciousuite-gold-buyback-quote-calculator',
				'description' => __('Offer transparent customer gold buyback quotes from your current rate.', 'preciousuite-gold-buyback-quote-calculator'),
			),
			'invoice-generator' => array(
				'name' => __('Invoice Generator Plugin', 'preciousuite-gold-buyback-quote-calculator'),
				'label' => __('Invoice & Certificates', 'preciousuite-gold-buyback-quote-calculator'),
				'wporg_slug' => 'jewelry-invoice-certificate-generator',
				'description' => __('Create jewelry invoices, certificates, templates, and PDF documents.', 'preciousuite-gold-buyback-quote-calculator'),
			),
			'gold-savings-scheme' => array(
				'name' => __('Gold Savings Scheme Plugin', 'preciousuite-gold-buyback-quote-calculator'),
				'label' => __('Gold Savings Scheme', 'preciousuite-gold-buyback-quote-calculator'),
				'wporg_slug' => 'preciousuite-gold-savings-scheme',
				'description' => __('A production-grade jewelry saving scheme and EMI management system for WooCommerce.', 'preciousuite-gold-buyback-quote-calculator'),
			),
		);
	}
}
