<?php
/**
 * Preciousuite dashboard view.
 *
 * @package Preciousuite
 */

if (!defined('ABSPATH')) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$modules = PreciousSuite_Registry::instance()->all();
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$catalog = pt_gold_get_catalog();
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$installed = count($modules);
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$total = count($catalog);
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$percent = $total > 0 ? round(($installed / $total) * 100) : 0;
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$user = wp_get_current_user();
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$name = $user && $user->display_name ? $user->display_name : __('admin', 'preciousuite-metal-rate-charts');
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$rates_configured = '' !== get_option('gold', '') && '' !== get_option('silver', '') && '' !== get_option('platinum', '');
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$product_total = 0;
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$product_selected_count = 0;
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$physical_product_total = 0;
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$product_weight_count = 0;

if (post_type_exists('product')) {
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$product_statuses = array('publish', 'draft', 'pending', 'private');
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$product_query = new WP_Query(
		array(
			'post_type' => 'product',
			'post_status' => $product_statuses,
			'fields' => 'ids',
			'posts_per_page' => 1,
		)
	);
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$product_selected_query = new WP_Query(
		array(
			'post_type' => 'product',
			'post_status' => $product_statuses,
			'fields' => 'ids',
			'posts_per_page' => 1,
			// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
			'meta_query' => array(
				array(
					'key' => 'jewellery_field',
					'value' => array('1', '2', '3'),
					'compare' => 'IN',
				),
			),
		)
	);
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$physical_product_query = new WP_Query(
		array(
			'post_type' => 'product',
			'post_status' => $product_statuses,
			'fields' => 'ids',
			'posts_per_page' => 1,
			// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
			'meta_query' => array(
				'relation' => 'OR',
				array(
					'key' => '_virtual',
					'compare' => 'NOT EXISTS',
				),
				array(
					'key' => '_virtual',
					'value' => 'yes',
					'compare' => '!=',
				),
			),
		)
	);

	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$product_weight_query = new WP_Query(
		array(
			'post_type' => 'product',
			'post_status' => $product_statuses,
			'fields' => 'ids',
			'posts_per_page' => 1,
			// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
			'meta_query' => array(
				'relation' => 'AND',
				array(
					'relation' => 'OR',
					array(
						'key' => '_virtual',
						'compare' => 'NOT EXISTS',
					),
					array(
						'key' => '_virtual',
						'value' => 'yes',
						'compare' => '!=',
					),
				),
				array(
					'key' => '_weight',
					'value' => '',
					'compare' => '!=',
				),
				array(
					'key' => '_weight',
					'value' => '0',
					'compare' => '!=',
				),
			),
		)
	);

	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$product_total = (int) $product_query->found_posts;
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$product_selected_count = (int) $product_selected_query->found_posts;
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$physical_product_total = (int) $physical_product_query->found_posts;
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$product_weight_count = (int) $product_weight_query->found_posts;
}
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$product_remaining_count = max(0, $product_total - $product_selected_count);
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$weight_remaining_count = max(0, $physical_product_total - $product_weight_count);
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$dashboard_tasks = array(
	array(
		'label' => __('Set API Key', 'preciousuite-metal-rate-charts'),
		'done' => '' !== get_option('pmrcgold_api_key', ''),
	),
	array(
		'label' => __('Widget 1 Setup', 'preciousuite-metal-rate-charts'),
		'done' => is_array(get_option('widget1_metals', false)) && !empty(get_option('widget1_metals', false))
			&& is_array(get_option('widget1_currencies', false)) && !empty(get_option('widget1_currencies', false))
			&& is_array(get_option('widget1_enabled', false)) && !empty(get_option('widget1_enabled', false)),
	),
	array(
		'label' => __('Widget 2 Setup', 'preciousuite-metal-rate-charts'),
		'done' => is_array(get_option('widget2_metals', false)) && !empty(get_option('widget2_metals', false))
			&& is_array(get_option('widget2_currencies', false)) && !empty(get_option('widget2_currencies', false)),
	),
	array(
		'label' => __('Widget 3 Setup', 'preciousuite-metal-rate-charts'),
		'done' => is_array(get_option('widget3_metals', false)) && !empty(get_option('widget3_metals', false))
			&& is_array(get_option('widget3_currencies', false)) && !empty(get_option('widget3_currencies', false)),
	),
	array(
		'label' => __('Widget 4 Setup', 'preciousuite-metal-rate-charts'),
		'done' => is_array(get_option('widget4_metals', false)) && !empty(get_option('widget4_metals', false))
			&& is_array(get_option('widget4_currencies', false)) && !empty(get_option('widget4_currencies', false)),
	),
);

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$dashboard_task_total = count($dashboard_tasks);
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$dashboard_tasks_completed = count(array_filter($dashboard_tasks, static function ($t) {
	return !empty($t['done']);
}));
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$dashboard_task_pct = $dashboard_task_total > 0 ? round(($dashboard_tasks_completed / $dashboard_task_total) * 100) : 0;

$buyback_tasks = array();
$buyback_task_total = 0;
$buyback_tasks_completed = 0;
$buyback_task_pct = 0;
if (isset($modules['buyback-calculator'])) {
	$buyback_tasks = array(
		array(
			'label' => __('Add Rates', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('preciousuite_manual_gold_rate', ''),
		),
		array(
			'label' => __('Set Margin', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('preciousuite_buyback_margin', ''),
		),
		array(
			'label' => __('Set Currency', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('preciousuite_currency', ''),
		),
		array(
			'label' => __('Set WhatsApp', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('preciousuite_whatsapp_number', ''),
		),
		array(
			'label' => __('Set SMTP', 'preciousuite-metal-rate-charts'),
			'done' => ('1' === get_option('preciousuite_smtp_enable', '')) || ('' !== get_option('preciousuite_smtp_host', '') && '' !== get_option('preciousuite_smtp_port', '') && '0' !== get_option('preciousuite_smtp_port', '') && '' !== get_option('preciousuite_smtp_username', '') && '' !== get_option('preciousuite_smtp_password', '')),
		),
	);
	$buyback_task_total = count($buyback_tasks);
	$buyback_tasks_completed = count(array_filter($buyback_tasks, static function ($t) {
		return !empty($t['done']);
	}));
	$buyback_task_pct = $buyback_task_total > 0 ? round(($buyback_tasks_completed / $buyback_task_total) * 100) : 0;
}

$invoice_tasks = array();
$invoice_task_total = 0;
$invoice_tasks_completed = 0;
$invoice_task_pct = 0;
if (isset($modules['invoice-generator'])) {
	$invoice_tasks = array(
		array(
			'label' => __('Shop Details', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('jicg_shop_name', ''),
		),
		array(
			'label' => __('Set Prefix', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('jicg_invoice_prefix', ''),
		),
		/*
		array(
			'label' => __('Metal & Unit', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('jicg_default_metal', '') && '' !== get_option('jicg_weight_unit', ''),
		),
		*/
		array(
			'label' => __('Set Tax', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('jicg_tax_mode', ''),
		),
		array(
			'label' => __('Set Cert', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('jicg_cert_title', ''),
		),
		array(
			'label' => __('Set Currency', 'preciousuite-metal-rate-charts'),
			'done' => true,
		),
		array(
			'label' => __('Set Template', 'preciousuite-metal-rate-charts'),
			'done' => true,
		),
		/*
		array(
			'label' => __('Email Config', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('jicg_email_subject', ''),
		),
		*/
		array(
			'label' => __('WhatsApp Config', 'preciousuite-metal-rate-charts'),
			'done' => '' !== get_option('jicg_whatsapp_message', ''),
		),
	);
	$invoice_task_total = count($invoice_tasks);
	$invoice_tasks_completed = count(array_filter($invoice_tasks, static function ($t) {
		return !empty($t['done']);
	}));
	$invoice_task_pct = $invoice_task_total > 0 ? round(($invoice_tasks_completed / $invoice_task_total) * 100) : 0;
}
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$tool_status = array(
	'gold-price' => array(
		'label' => __('Gold Price Based on Weight', 'preciousuite-metal-rate-charts'),
		'good' => __('Metal rates ready', 'preciousuite-metal-rate-charts'),
		'todo' => __('Set metal rates', 'preciousuite-metal-rate-charts'),
	),
	'gold-price-widgets' => array(
		'label' => __('Metal Rate Charts', 'preciousuite-metal-rate-charts'),
		'good' => __('Charts ready', 'preciousuite-metal-rate-charts'),
		'todo' => __('Set API Key', 'preciousuite-metal-rate-charts'),
	),
	'buyback-calculator' => array(
		'label' => __('Gold Buyback Quote Calculator', 'preciousuite-metal-rate-charts'),
		'good' => __('Calculator active', 'preciousuite-metal-rate-charts'),
		'todo' => __('Configure calculator', 'preciousuite-metal-rate-charts'),
	),
	'invoice-generator' => array(
		'label' => __('Jewelry Invoice & Certificate Generator', 'preciousuite-metal-rate-charts'),
		'good' => __('Templates available', 'preciousuite-metal-rate-charts'),
		'todo' => __('Set invoice details', 'preciousuite-metal-rate-charts'),
	),
	'gold-savings-scheme' => array(
		'label' => __('Gold savings scheme', 'preciousuite-metal-rate-charts'),
		'good' => __('Savings scheme active', 'preciousuite-metal-rate-charts'),
		'todo' => __('Configure savings scheme', 'preciousuite-metal-rate-charts'),
	),
);

?>

<div class="wrap preciousuite-wrap preciousuite-dashboard">
	<div class="preciousuite-dashboard-shell">
		<aside class="preciousuite-dashboard-nav">
			<div class="preciousuite-dashboard-brand">
				<div class="preciousuite-page-brand">
					<div class="preciousuite-box preciousuite-logo">
						<a href="<?php echo esc_url("https://www.preciousuite.com"); ?>" target="_blank"
							rel="noopener noreferrer" class="preciousuite-page-title">
							<img src="<?php echo esc_url(PMRCGOLD_URL . 'includes/precioussuite/assets/' . 'uploads/Preciousuite_Logo.png'); ?>"
								alt="<?php esc_attr_e('Preciousuite Logo', 'preciousuite-metal-rate-charts'); ?>">
						</a>
					</div>
				</div>
			</div>
			<nav class="preciousuite-dashboard-nav-inner">
				<div class="preciousuite-nav-links">
					<a class="is-active" href="<?php echo esc_url(admin_url('admin.php?page=preciousuite')); ?>">
						<img src="<?php echo esc_url(PMRCGOLD_URL . 'includes/precioussuite/assets/' . '/uploads/dashboard-icon.png'); ?>"
							alt="<?php esc_attr_e('General', 'preciousuite-metal-rate-charts'); ?>" width="20"
							height="20" loading="lazy" style="margin-right: .75rem;" />
						<?php esc_html_e('General', 'preciousuite-metal-rate-charts'); ?>
					</a>
					<a href="<?php echo esc_url(admin_url('admin.php?page=preciousuite-tasks')); ?>">
						<img src="<?php echo esc_url(PMRCGOLD_URL . 'includes/precioussuite/assets/' . '/uploads/task-list-icon.png'); ?>"
							alt="<?php esc_attr_e('Task list', 'preciousuite-metal-rate-charts'); ?>" width="20"
							height="20" loading="lazy" style="margin-right: .75rem;" />
						<?php esc_html_e('Task list', 'preciousuite-metal-rate-charts'); ?>
					</a>
					<a href="<?php echo esc_url(admin_url('admin.php?page=preciousuite-alerts')); ?>">
						<img src="<?php echo esc_url(PMRCGOLD_URL . 'includes/precioussuite/assets/' . '/uploads/alert-center-icon.png'); ?>"
							alt="<?php esc_attr_e('Alert center', 'preciousuite-metal-rate-charts'); ?>" width="20"
							height="20" loading="lazy" style="margin-right: .75rem;" />
						<?php esc_html_e('Alert center', 'preciousuite-metal-rate-charts'); ?></a>
					<a href="<?php echo esc_url(admin_url('admin.php?page=preciousuite-configuration')); ?>">
						<img src="<?php echo esc_url(PMRCGOLD_URL . 'includes/precioussuite/assets/' . '/uploads/first-time-configuration-icon.png'); ?>"
							alt="<?php esc_attr_e('First-time configuration', 'preciousuite-metal-rate-charts'); ?>"
							width="20" height="20" loading="lazy" style="margin-right: .75rem;" />
						<?php esc_html_e('First-time configuration', 'preciousuite-metal-rate-charts'); ?>
					</a>
				</div>
				<div class="preciousuite-sidebar-help-card">
					<strong>
						<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
							stroke-linecap="round" stroke-linejoin="round">
							<circle cx="12" cy="12" r="10"></circle>
							<circle cx="12" cy="12" r="4"></circle>
							<line x1="4.93" y1="4.93" x2="9.17" y2="9.17"></line>
							<line x1="14.83" y1="14.83" x2="19.07" y2="19.07"></line>
							<line x1="14.83" y1="9.17" x2="19.07" y2="4.93"></line>
							<line x1="14.83" y1="9.17" x2="18.36" y2="5.64"></line>
							<line x1="4.93" y1="19.07" x2="9.17" y2="14.83"></line>
						</svg>
						Need help?
					</strong>
					<p>Check our documentation or contact support.</p>
					<a href="https://wordpress.org/support/plugin/preciousuite-metal-rate-charts/" target="_blank"
						rel="noopener noreferrer">Visit Support
						&rarr;</a>
				</div>
			</nav>
		</aside>

		<main class="preciousuite-dashboard-main">
			<section class="preciousuite-dashboard-card preciousuite-dashboard-hero-card">
				<div class="preciousuite-dashboard-hero-content">
					<h1>
						<?php esc_html_e('Welcome to your Preciousuite dashboard!', 'preciousuite-metal-rate-charts'); ?>
					</h1>
					<p>
						<?php esc_html_e('Review active tools, finish setup tasks, and open each plugin without leaving the shared ecosystem.', 'preciousuite-metal-rate-charts'); ?>
					</p>
				</div>
				<div class="preciousuite-dashboard-hero-image">
					<svg viewBox="0 0 300 150" fill="none" xmlns="http://www.w3.org/2000/svg">
						<rect width="300" height="150" rx="12" fill="#F8FAFC" />
						<rect x="30" y="25" width="240" height="100" rx="8" fill="#FFFFFF" stroke="#E2E8F0"
							stroke-width="2" />
						<circle cx="45" cy="40" r="4" fill="#E2E8F0" />
						<circle cx="57" cy="40" r="4" fill="#E2E8F0" />
						<circle cx="69" cy="40" r="4" fill="#E2E8F0" />
						<line x1="30" y1="55" x2="270" y2="55" stroke="#E2E8F0" stroke-width="2" />
						<!-- Abstract bar charts -->
						<rect x="50" y="85" width="15" height="30" rx="2" fill="#818CF8" />
						<rect x="75" y="75" width="15" height="40" rx="2" fill="#6366F1" />
						<rect x="100" y="65" width="15" height="50" rx="2" fill="#4F46E5" />
						<rect x="125" y="95" width="15" height="20" rx="2" fill="#C7D2FE" />
						<!-- Abstract donut chart -->
						<circle cx="210" cy="90" r="22" stroke="#E0E7FF" stroke-width="8" />
						<path d="M232 90 A22 22 0 0 0 210 68" stroke="#4F46E5" stroke-width="8"
							stroke-linecap="round" />
					</svg>
				</div>
			</section>

			<div class="preciousuite-dashboard-grid" id="preciousuite-toolkit">
				<section class="preciousuite-dashboard-card preciousuite-setup-card">
					<div class="preciousuite-setup-card-header">
						<div class="preciousuite-setup-card-icon buyback">
							<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
								stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
								<line x1="18" y1="20" x2="18" y2="10"></line>
								<line x1="12" y1="20" x2="12" y2="4"></line>
								<line x1="6" y1="20" x2="6" y2="14"></line>
							</svg>
						</div>
						<div class="preciousuite-setup-card-title">
							<h2><?php esc_html_e('Metal Rate Charts', 'preciousuite-metal-rate-charts'); ?></h2>
							<p><?php esc_html_e('Complete these setup tasks to enable metal rate chart widgets.', 'preciousuite-metal-rate-charts'); ?>
							</p>
						</div>
					</div>

					<div class="preciousuite-steps">
						<?php
						// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
						foreach ($dashboard_tasks as $index => $task): ?>
							<div class="preciousuite-step <?php echo $task['done'] ? 'is-complete' : ''; ?>">
								<div class="preciousuite-step-number"><?php echo esc_html($index + 1); ?></div>
								<div class="preciousuite-step-content">
									<strong><?php echo esc_html($task['label']); ?></strong>
									<small
										class="<?php echo $task['done'] ? 'status-done' : 'status-todo'; ?>"><?php echo esc_html($task['done'] ? __('Done', 'preciousuite-metal-rate-charts') : __('Todo', 'preciousuite-metal-rate-charts')); ?></small>
								</div>
							</div>
						<?php endforeach; ?>
					</div>

					<div class="preciousuite-progress-block" id="preciousuite-progress">
						<div>
							<?php /* translators: 1: Number of completed tasks, 2: Total number of tasks.
							  */ ?>
							<strong><?php echo esc_html(sprintf(__('Task %1$d/%2$d', 'preciousuite-metal-rate-charts'), $dashboard_tasks_completed, $dashboard_task_total)); ?></strong>
							<?php /* translators: %d: Progress percentage. 
							  */ ?>
							<span><?php echo esc_html(sprintf(__('Progress %d%%', 'preciousuite-metal-rate-charts'), $dashboard_task_pct)); ?></span>
						</div>
						<div class="preciousuite-progress" aria-hidden="true"><span
								style="width: <?php echo esc_attr($dashboard_task_pct); ?>%"></span></div>
					</div>
				</section>

				<?php if (isset($modules['buyback-calculator'])): ?>
					<section class="preciousuite-dashboard-card preciousuite-setup-card">
						<div class="preciousuite-setup-card-header">
							<div class="preciousuite-setup-card-icon buyback">
								<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
									stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
									<polyline points="17 1 21 5 17 9"></polyline>
									<path d="M3 11V9a4 4 0 0 1 4-4h14"></path>
									<polyline points="7 23 3 19 7 15"></polyline>
									<path d="M21 13v2a4 4 0 0 1-4 4H3"></path>
								</svg>
							</div>
							<div class="preciousuite-setup-card-title">
								<h2><?php esc_html_e('Buyback Plugin', 'preciousuite-metal-rate-charts'); ?></h2>
								<p><?php esc_html_e('Complete these setup tasks to enable gold buyback quote calculations.', 'preciousuite-metal-rate-charts'); ?>
								</p>
							</div>
						</div>

						<div class="preciousuite-steps">
							<?php foreach ($buyback_tasks as $index => $task): ?>
								<div class="preciousuite-step <?php echo $task['done'] ? 'is-complete' : ''; ?>">
									<div class="preciousuite-step-number"><?php echo esc_html($index + 1); ?></div>
									<div class="preciousuite-step-content">
										<strong><?php echo esc_html($task['label']); ?></strong>
										<small
											class="<?php echo $task['done'] ? 'status-done' : 'status-todo'; ?>"><?php echo $task['done'] ? esc_html__('Done', 'preciousuite-metal-rate-charts') : esc_html__('Todo', 'preciousuite-metal-rate-charts'); ?></small>
									</div>
								</div>
							<?php endforeach; ?>
						</div>

						<div class="preciousuite-progress-block">
							<div>
								<strong><?php
								// translators: 1: Number of completed buyback tasks, 2: Total buyback tasks.
								echo esc_html(sprintf(__('Task %1$d/%2$d', 'preciousuite-metal-rate-charts'), $buyback_tasks_completed, $buyback_task_total));
								?></strong>
								<span><?php
								// translators: %d: Buyback task progress percentage.
								echo esc_html(sprintf(__('Progress %d%%', 'preciousuite-metal-rate-charts'), $buyback_task_pct));
								?></span>
							</div>
							<div class="preciousuite-progress" aria-hidden="true"><span
									style="width: <?php echo esc_attr($buyback_task_pct); ?>%"></span></div>
						</div>
					</section>
				<?php endif; ?>

				<?php if (isset($modules['invoice-generator'])): ?>
					<section class="preciousuite-dashboard-card preciousuite-setup-card">
						<div class="preciousuite-setup-card-header">
							<div class="preciousuite-setup-card-icon invoice">
								<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
									stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
									<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
									<polyline points="14 2 14 8 20 8"></polyline>
									<line x1="16" y1="13" x2="8" y2="13"></line>
									<line x1="16" y1="17" x2="8" y2="17"></line>
									<polyline points="10 9 9 9 8 9"></polyline>
								</svg>
							</div>
							<div class="preciousuite-setup-card-title">
								<h2><?php esc_html_e('Invoice Plugin', 'preciousuite-metal-rate-charts'); ?></h2>
								<p><?php esc_html_e('Complete these setup tasks to enable invoice and certificate generation.', 'preciousuite-metal-rate-charts'); ?>
								</p>
							</div>
						</div>

						<div class="preciousuite-steps">
							<?php foreach ($invoice_tasks as $index => $task): ?>
								<div class="preciousuite-step <?php echo $task['done'] ? 'is-complete' : ''; ?>">
									<div class="preciousuite-step-number"><?php echo esc_html($index + 1); ?></div>
									<div class="preciousuite-step-content">
										<strong><?php echo esc_html($task['label']); ?></strong>
										<small
											class="<?php echo $task['done'] ? 'status-done' : 'status-todo'; ?>"><?php echo $task['done'] ? esc_html__('Done', 'preciousuite-metal-rate-charts') : esc_html__('Todo', 'preciousuite-metal-rate-charts'); ?></small>
									</div>
								</div>
							<?php endforeach; ?>
						</div>

						<div class="preciousuite-progress-block">
							<div>
								<strong><?php
								// translators: 1: Number of completed invoice tasks, 2: Total invoice tasks.
								echo esc_html(sprintf(__('Task %1$d/%2$d', 'preciousuite-metal-rate-charts'), $invoice_tasks_completed, $invoice_task_total));
								?></strong>
								<span><?php
								// translators: %d: Invoice task progress percentage.
								echo esc_html(sprintf(__('Progress %d%%', 'preciousuite-metal-rate-charts'), $invoice_task_pct));
								?></span>
							</div>
							<div class="preciousuite-progress" aria-hidden="true"><span
									style="width: <?php echo esc_attr($invoice_task_pct); ?>%"></span></div>
						</div>
					</section>
				<?php endif; ?>

				<?php if (isset($modules['gold-savings-scheme'])): ?>
					<section class="preciousuite-dashboard-card preciousuite-setup-card">
						<div class="preciousuite-setup-card-header">
							<div class="preciousuite-setup-card-icon buyback">
								<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
									stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
									<rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
									<line x1="12" y1="8" x2="12" y2="16"></line>
									<line x1="8" y1="12" x2="16" y2="12"></line>
								</svg>
							</div>
							<div class="preciousuite-setup-card-title">
								<h2><?php esc_html_e('Savings Scheme Plugin', 'preciousuite-metal-rate-charts'); ?></h2>
								<p><?php esc_html_e('Complete these setup tasks to enable recurring gold saving schemes.', 'preciousuite-metal-rate-charts'); ?>
								</p>
							</div>
						</div>

						<div class="preciousuite-steps">
							<?php
							$savings_tasks = array(
								array(
									'label' => __('Razorpay Key ID', 'preciousuite-metal-rate-charts'),
									'done' => '' !== get_option('ps_gs_razorpay_key_id', ''),
								),
								array(
									'label' => __('Razorpay Secret Key', 'preciousuite-metal-rate-charts'),
									'done' => '' !== get_option('ps_gs_razorpay_secret_key', ''),
								),
								array(
									'label' => __('Set Gold Price', 'preciousuite-metal-rate-charts'),
									'done' => '' !== get_option('ps_gs_manual_gold_price', ''),
								),
							);
							$savings_task_total = count($savings_tasks);
							$savings_tasks_completed = count(array_filter($savings_tasks, static function ($t) {
								return !empty($t['done']);
							}));
							$savings_task_pct = $savings_task_total > 0 ? round(($savings_tasks_completed / $savings_task_total) * 100) : 0;

							foreach ($savings_tasks as $index => $task): ?>
								<div class="preciousuite-step <?php echo $task['done'] ? 'is-complete' : ''; ?>">
									<div class="preciousuite-step-number"><?php echo esc_html($index + 1); ?></div>
									<div class="preciousuite-step-content">
										<strong><?php echo esc_html($task['label']); ?></strong>
										<small
											class="<?php echo $task['done'] ? 'status-done' : 'status-todo'; ?>"><?php echo $task['done'] ? esc_html__('Done', 'preciousuite-metal-rate-charts') : esc_html__('Todo', 'preciousuite-metal-rate-charts'); ?></small>
									</div>
								</div>
							<?php endforeach; ?>
						</div>

						<div class="preciousuite-progress-block">
							<div>
								<?php
								/* translators: 1: completed task count, 2: total task count */
								$task_label = sprintf(__('Task %1$d/%2$d', 'preciousuite-metal-rate-charts'), $savings_tasks_completed, $savings_task_total);
								?>
								<strong><?php echo esc_html($task_label); ?></strong>
								<?php
								/* translators: %d: percentage progress number */
								$progress_label = sprintf(__('Progress %d%%', 'preciousuite-metal-rate-charts'), $savings_task_pct);
								?>
								<span><?php echo esc_html($progress_label); ?></span>
							</div>
							<div class="preciousuite-progress" aria-hidden="true"><span
									style="width: <?php echo esc_attr($savings_task_pct); ?>%"></span></div>
						</div>
					</section>
				<?php endif; ?>

				<section class="preciousuite-dashboard-card preciousuite-score-card">
					<div class="preciousuite-score-header">
						<div>
							<h2><?php esc_html_e('Business toolkit status', 'preciousuite-metal-rate-charts'); ?></h2>
							<p><?php esc_html_e('A quick snapshot of your Preciousuite modules. Static recommendations are shown where deeper plugin data is not available yet.', 'preciousuite-metal-rate-charts'); ?>
							</p>
						</div>

						<div class="preciousuite-donut" style="--score: <?php echo esc_attr(max(8, $percent)); ?>">
							<span><?php echo esc_html($percent); ?>%</span>
						</div>
					</div>

					<div class="preciousuite-status-list">
						<?php
						// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
						foreach ($tool_status as $slug => $status): ?>
							<?php
							// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
							$module = isset($modules[$slug]) ? $modules[$slug] : null; ?>
							<div class="preciousuite-status-row">
								<span class="<?php echo $module ? 'is-good' : 'is-warning'; ?>"></span>
								<div>
									<strong><?php echo esc_html($status['label']); ?></strong>
									<small><?php echo esc_html($module ? $status['good'] : $status['todo']); ?></small>
								</div>
								<?php if ($module): ?>
									<a class="button preciousuite-status-row-btn" id="preciousuite-status-row-btn"
										href="<?php echo esc_url($module['settings_url']); ?>"><?php esc_html_e('Open', 'preciousuite-metal-rate-charts'); ?></a>
								<?php else: ?>
									<a class="button preciousuite-status-row-btn" id="preciousuite-status-row-btn"
										href="<?php echo esc_url(self_admin_url('plugin-install.php?s=' . rawurlencode($catalog[$slug]['wporg_slug']) . '&tab=search&type=term')); ?>"><?php esc_html_e('Install', 'preciousuite-metal-rate-charts'); ?></a>
								<?php endif; ?>
							</div>
						<?php endforeach; ?>
					</div>


				</section>
			</div>

			<section class="preciousuite-dashboard-card preciousuite-toolkit-list">
				<h2><?php esc_html_e('Jewelry Business Toolkit', 'preciousuite-metal-rate-charts'); ?></h2>
				<div class="preciousuite-products">
					<?php
					// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
					foreach ($catalog as $slug => $product): ?>
						<?php
						// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
						$module = isset($modules[$slug]) ? $modules[$slug] : null; ?>
						<div class="preciousuite-product">
							<div style="display: flex; gap: 16px; align-items: flex-start;">
								<?php
								$icon_class = '';
								if ('gold-price' === $slug) {
									$icon_class = 'gold';
								} elseif ('gold-price-widgets' === $slug) {
									$icon_class = 'buyback';
								} elseif ('buyback-calculator' === $slug) {
									$icon_class = 'buyback';
								} elseif ('invoice-generator' === $slug) {
									$icon_class = 'invoice';
								} elseif ('gold-savings-scheme' === $slug) {
									$icon_class = 'buyback';
								}

								if ($icon_class): ?>
									<div class="preciousuite-setup-card-icon <?php echo esc_attr($icon_class); ?>">
										<?php if ('gold-price' === $slug): ?>
											<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
												stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
												<polygon points="12 2 2 7 12 12 22 7 12 2"></polygon>
												<polyline points="2 17 12 22 22 17"></polyline>
												<polyline points="2 12 12 17 22 12"></polyline>
											</svg>
										<?php elseif ('gold-price-widgets' === $slug): ?>
											<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
												stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
												<line x1="18" y1="20" x2="18" y2="10"></line>
												<line x1="12" y1="20" x2="12" y2="4"></line>
												<line x1="6" y1="20" x2="6" y2="14"></line>
											</svg>
										<?php elseif ('buyback-calculator' === $slug): ?>
											<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
												stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
												<polyline points="17 1 21 5 17 9"></polyline>
												<path d="M3 11V9a4 4 0 0 1 4-4h14"></path>
												<polyline points="7 23 3 19 7 15"></polyline>
												<path d="M21 13v2a4 4 0 0 1-4 4H3"></path>
											</svg>
										<?php elseif ('invoice-generator' === $slug): ?>
											<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
												stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
												<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
												<polyline points="14 2 14 8 20 8"></polyline>
												<line x1="16" y1="13" x2="8" y2="13"></line>
												<line x1="16" y1="17" x2="8" y2="17"></line>
												<polyline points="10 9 9 9 8 9"></polyline>
											</svg>
										<?php elseif ('gold-savings-scheme' === $slug): ?>
											<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
												stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
												<rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
												<line x1="12" y1="8" x2="12" y2="16"></line>
												<line x1="8" y1="12" x2="16" y2="12"></line>
											</svg>
										<?php endif; ?>
									</div>
								<?php endif; ?>
								<div class="preciousuite-product-copy">
									<h3><?php echo esc_html($product['label']); ?></h3>
									<p class="preciousuite-muted"><?php echo esc_html($product['name']); ?></p>
									<p><?php echo esc_html($product['description']); ?></p>
								</div>
							</div>
							<div class="preciousuite-product-actions">
								<?php if ($module): ?>
									<span
										class="preciousuite-badge is-active"><?php esc_html_e('Active', 'preciousuite-metal-rate-charts'); ?></span>
									<a class="button button-primary"
										href="<?php echo esc_url($module['settings_url']); ?>"><?php esc_html_e('Open Settings', 'preciousuite-metal-rate-charts'); ?></a>
								<?php else: ?>
									<a class="button"
										href="<?php echo esc_url(self_admin_url('plugin-install.php?s=' . rawurlencode($product['wporg_slug']) . '&tab=search&type=term')); ?>"><?php esc_html_e('Install Plugin', 'preciousuite-metal-rate-charts'); ?></a>
								<?php endif; ?>
							</div>
						</div>
					<?php endforeach; ?>
				</div>
			</section>
		</main>

		<aside class="preciousuite-dashboard-sidebar">
			<section class="preciousuite-sidebar-card preciousuite-pro-card">
				<h2>
					<?php esc_html_e('Upgrade to Pro', 'preciousuite-metal-rate-charts'); ?>
					<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
						stroke-linecap="round" stroke-linejoin="round">
						<path d="M6 3h12l4 6-10 12L2 9l4-6z"></path>
						<path d="M2 9h20"></path>
						<path d="M12 21L8 9"></path>
						<path d="M12 21l4-12"></path>
						<path d="M6 3l2 6"></path>
						<path d="M18 3l-2 6"></path>
					</svg>
				</h2>
				<div class="preciousuite-pro-features">
					<div class="preciousuite-pro-feature">
						<svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff"
							stroke-width="2">
							<circle cx="12" cy="12" r="11" stroke="none" />
							<path d="M8 12.5l3 3 5-6" />
						</svg>
						<span><?php esc_html_e('Advanced product pricing workflows', 'preciousuite-metal-rate-charts'); ?></span>
					</div>
					<div class="preciousuite-pro-feature">
						<svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff"
							stroke-width="2">
							<circle cx="12" cy="12" r="11" stroke="none" />
							<path d="M8 12.5l3 3 5-6" />
						</svg>
						<span><?php esc_html_e('Professional invoice and certificate templates', 'preciousuite-metal-rate-charts'); ?></span>
					</div>
					<div class="preciousuite-pro-feature">
						<svg width="18" height="18" viewBox="0 0 24 24" fill="#10b981" stroke="#ffffff"
							stroke-width="2">
							<circle cx="12" cy="12" r="11" stroke="none" />
							<path d="M8 12.5l3 3 5-6" />
						</svg>
						<span><?php esc_html_e('Priority support for setup questions', 'preciousuite-metal-rate-charts'); ?></span>
					</div>
				</div>

				<div class="preciousuite-pro-actions">
					<a class="button button-primary"
						href="<?php echo esc_url('https://preciousuite.com/products'); ?>"><?php esc_html_e('View Upgrade', 'preciousuite-metal-rate-charts'); ?></a>
					<small><?php esc_html_e('No forced upsells. Just optional bundle tools.', 'preciousuite-metal-rate-charts'); ?></small>
				</div>
			</section>

			<section class="preciousuite-sidebar-card preciousuite-quick-actions">
				<h2>
					<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
						stroke-linecap="round" stroke-linejoin="round">
						<path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"></path>
						<path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"></path>
					</svg>
					<?php esc_html_e('Quick Actions', 'preciousuite-metal-rate-charts'); ?>
				</h2>
				<div class="preciousuite-action-list">
					<a href="https://www.preciousuite.com/docs/#preciousuite-metal-rate-charts" target="_blank"
						rel="noopener noreferrer">
						<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
							stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
							<path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"></path>
							<path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"></path>
						</svg>
						<?php esc_html_e('Documentation', 'preciousuite-metal-rate-charts'); ?>
					</a>
					<a href="https://wordpress.org/support/plugin/preciousuite-metal-rate-charts/" target="_blank"
						rel="noopener noreferrer">
						<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
							stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
							<path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"></path>
							<path d="M13.73 21a2 2 0 0 1-3.46 0"></path>
						</svg>
						<?php esc_html_e('Support', 'preciousuite-metal-rate-charts'); ?>
					</a>
					<a href="https://wordpress.org/support/plugin/preciousuite-metal-rate-charts/reviews/"
						target="_blank" rel="noopener noreferrer">
						<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor"
							stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
							<polygon
								points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2">
							</polygon>
						</svg>
						<?php esc_html_e('Leave Review', 'preciousuite-metal-rate-charts'); ?>
					</a>
				</div>
			</section>

			<section class="preciousuite-sidebar-card preciousuite-learn-card">
				<h2>
					<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="#4f46e5" stroke-width="2"
						stroke-linecap="round" stroke-linejoin="round">
						<path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"></path>
						<path d="M6.5 2H20v20H6.5A2.5 2.5 0 0 1 4 19.5v-15A2.5 2.5 0 0 1 6.5 2z"></path>
					</svg>
					<?php esc_html_e('Learn Preciousuite', 'preciousuite-metal-rate-charts'); ?>
				</h2>
				<p>
					<?php esc_html_e('Use the docs to connect shortcodes, pricing rules, quotes, invoices, and customer documents.', 'preciousuite-metal-rate-charts'); ?>
				</p>
				<a href="https://www.youtube.com/@preciousuite" target="_blank" rel="noopener noreferrer"
					class="preciousuite-link"><?php esc_html_e('View tutorials &rarr;', 'preciousuite-metal-rate-charts'); ?></a>
			</section>
		</aside>
	</div>
</div>
<?php
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound