<?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('pt_gold_register_module')) {
	function pt_gold_register_module(array $module)
	{
		PreciousSuite_Registry::instance()->register($module);
	}
}

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