<?php
/**
 * Help page — user manual for SkyServers Sync.
 *
 * @package SkyEmagSync
 */

namespace SkyEmagSync\Admin;

use SkyEmagSync\Options;

defined( 'ABSPATH' ) || exit;

class HelpPage {

	public function register(): void {
		// No AJAX actions needed.
	}

	public static function render(): void {
		if ( ! current_user_can( Menu::CAPABILITY ) ) {
			return;
		}

		$server_ip      = self::detect_server_ip();
		$plugin_version = defined( 'SKYEMAG_VERSION' ) ? SKYEMAG_VERSION : '';
		// Premium-only documentation blocks (order import, AWB labels,
		// Buy Button tracking, etc.) describe code paths that ship only
		// in the Premium add-on and are physically absent from this
		// build's ZIP when running on Free. They are wrapped in the
		// $is_premium flag below so the Free user sees a descriptive
		// upsell paragraph instead of step-by-step instructions for
		// admin pages that do not exist on their install.
		$is_premium  = \SkyEmagSync\Plugin::is_premium();
		$upgrade_url = Menu::upgrade_url();
		?>
		<div class="wrap skyemag-help-wrap">
			<h1><?php esc_html_e( 'SkyServers Sync — Help &amp; Documentation', 'skyservers-product-sync-for-emag' ); ?></h1>

			<div class="skyemag-help-content" style="max-width:900px;">

				<p style="font-size:13px;color:#646970;margin:4px 0 18px;">
					<?php
					if ( $plugin_version ) {
						printf(
							/* translators: %s: plugin version number */
							esc_html__( 'Plugin version: %s', 'skyservers-product-sync-for-emag' ),
							'<code>' . esc_html( $plugin_version ) . '</code>'
						);
					}
					?>
				</p>

				<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" style="margin:0 0 22px;">
					<?php wp_nonce_field( SetupWizard::NONCE ); ?>
					<input type="hidden" name="action" value="<?php echo esc_attr( SetupWizard::POST_RESTART ); ?>">
					<button type="submit" class="button button-primary">
						<?php esc_html_e( 'Run setup wizard', 'skyservers-product-sync-for-emag' ); ?>
					</button>
					<span style="font-size:12px;color:#646970;margin-left:10px;">
						<?php esc_html_e( 'Walk through the configuration steps again — useful after changing API credentials, migrating to a new server, or re-evaluating defaults.', 'skyservers-product-sync-for-emag' ); ?>
					</span>
				</form>

				<p>
					<?php if ( $is_premium ) : ?>
						<?php esc_html_e( 'SkyServers Sync connects your WooCommerce store to eMAG Marketplace. It synchronizes price and stock for existing offers, publishes new products from WordPress, and imports orders + issues AWB shipping labels — so you never need to leave WordPress for daily fulfillment.', 'skyservers-product-sync-for-emag' ); ?>
					<?php else : ?>
						<?php esc_html_e( 'SkyServers Sync connects your WooCommerce store to eMAG Marketplace. It synchronizes price and stock for existing offers on the schedule you configure. The Premium add-on extends the plugin with new-product publishing, automatic order import, AWB shipping label issuance, Buy Button rank tracking and per-event email notifications.', 'skyservers-product-sync-for-emag' ); ?>
					<?php endif; ?>
				</p>

				<!-- ── Table of contents ────────────────────────────── -->
				<nav class="skyemag-help-toc" style="background:#f6f7f7;border:1px solid #dcdcde;padding:10px 16px;margin:16px 0;">
					<strong><?php esc_html_e( 'Jump to section', 'skyservers-product-sync-for-emag' ); ?>:</strong>
					<ul style="margin:6px 0 0 16px;list-style:disc;">
						<li><a href="#skyemag-quick-start"><?php esc_html_e( 'Quick start (5 steps)', 'skyservers-product-sync-for-emag' ); ?></a></li>
						<li><a href="#skyemag-common-issues"><?php esc_html_e( 'Common issues at first run', 'skyservers-product-sync-for-emag' ); ?></a></li>
						<li><a href="#skyemag-how-it-works"><?php esc_html_e( 'How sync works', 'skyservers-product-sync-for-emag' ); ?></a></li>
						<li>
							<a href="#skyemag-orders-awb">
								<?php echo $is_premium
									? esc_html__( 'Daily fulfillment — Orders & AWB', 'skyservers-product-sync-for-emag' )
									: esc_html__( 'Daily fulfillment — Premium add-on', 'skyservers-product-sync-for-emag' );
								?>
							</a>
						</li>
						<li><a href="#skyemag-menu-pages"><?php esc_html_e( 'Reference: menu pages', 'skyservers-product-sync-for-emag' ); ?></a></li>
						<li><a href="#skyemag-other-errors"><?php esc_html_e( 'Other errors &amp; tech support', 'skyservers-product-sync-for-emag' ); ?></a></li>
						<li><a href="#skyemag-technical"><?php esc_html_e( 'Technical details', 'skyservers-product-sync-for-emag' ); ?></a></li>
					</ul>
				</nav>

				<!-- ── 1. Quick start ───────────────────────────────── -->
				<h2 id="skyemag-quick-start"><?php esc_html_e( 'Quick start', 'skyservers-product-sync-for-emag' ); ?></h2>
				<p><?php esc_html_e( 'Follow these five steps in order. Most first-run problems happen when one of them is skipped.', 'skyservers-product-sync-for-emag' ); ?></p>
				<ol>
					<li>
						<strong><?php esc_html_e( 'Whitelist your server IP in eMAG.', 'skyservers-product-sync-for-emag' ); ?></strong>
						<?php esc_html_e( 'See the box below for your server IP and the exact steps in your eMAG seller account. Without this, every API call returns 401/403.', 'skyservers-product-sync-for-emag' ); ?>
					</li>
					<li>
						<strong><?php esc_html_e( 'Enter API credentials and run Test Connection.', 'skyservers-product-sync-for-emag' ); ?></strong>
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: %s: italic menu path "SkyServers Sync → Settings → eMAG API Credentials" */
								esc_html__( 'In %s, fill in username, password, and select your region (RO, BG, HU). The API Code field is for your reference only — not used in authentication. A green response with latency confirms credentials + IP are good.', 'skyservers-product-sync-for-emag' ),
								'<em>' . esc_html__( 'SkyServers Sync → Settings → eMAG API Credentials', 'skyservers-product-sync-for-emag' ) . '</em>'
							)
						);
						?>
					</li>
					<li>
						<strong><?php esc_html_e( 'Import categories.', 'skyservers-product-sync-for-emag' ); ?></strong>
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: %s: italic menu section "Settings → eMAG Categories" */
								esc_html__( 'In %s, click "Import Categories". Downloads ~3000 categories with their characteristics and family types. Required before mapping or publishing.', 'skyservers-product-sync-for-emag' ),
								'<em>' . esc_html__( 'Settings → eMAG Categories', 'skyservers-product-sync-for-emag' ) . '</em>'
							)
						);
						?>
					</li>
					<li>
						<strong><?php esc_html_e( 'Import existing offers from eMAG.', 'skyservers-product-sync-for-emag' ); ?></strong>
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: %1$s: italic menu section "Settings → eMAG Data Import"; %2$s: code "_sku → part_number" */
								esc_html__( 'In %1$s, click "Import from eMAG". Reads every offer you already have on eMAG and matches each to a WooCommerce product by %2$s. For each match, the plugin stores the eMAG offer id, VAT rate, min/max price bounds, handling time, Buy Button rank, and validation status. Run again any time you publish products directly on eMAG that also exist in WooCommerce.', 'skyservers-product-sync-for-emag' ),
								'<em>' . esc_html__( 'Settings → eMAG Data Import', 'skyservers-product-sync-for-emag' ) . '</em>',
								'<code>_sku → part_number</code>'
							)
						);
						?>
					</li>
					<li>
						<strong><?php esc_html_e( 'Map categories, fill characteristics, enable sync, then flip the Master Switch.', 'skyservers-product-sync-for-emag' ); ?></strong>
						<?php esc_html_e( 'Three small actions, all in admin:', 'skyservers-product-sync-for-emag' ); ?>
						<ul style="margin:6px 0 6px 20px;list-style:disc;">
							<li>
								<?php
								echo wp_kses_post(
									sprintf(
										/* translators: %s: italic menu path "Category Map" / "Char. Map" / "Family Map" */
										esc_html__( 'Open %s and link your WooCommerce categories to eMAG categories. Sub-categories inherit parent mapping. Open the Char. Map and fill mandatory characteristics (mark them as defaults to apply globally).', 'skyservers-product-sync-for-emag' ),
										'<em>' . esc_html__( 'SkyServers Sync → Category Map', 'skyservers-product-sync-for-emag' ) . '</em>'
									)
								);
								?>
							</li>
							<li>
								<?php
								echo wp_kses_post(
									sprintf(
										/* translators: %s: italic "Products" page name */
										esc_html__( 'Open %s, enable the Sync toggle on each product you want synchronized.', 'skyservers-product-sync-for-emag' ),
										'<em>' . esc_html__( 'SkyServers Sync → Products', 'skyservers-product-sync-for-emag' ) . '</em>'
									)
								);
								?>
							</li>
							<li><?php esc_html_e( 'Turn ON the Master Switch in Settings → automatic cron sync starts.', 'skyservers-product-sync-for-emag' ); ?></li>
						</ul>
					</li>
				</ol>

				<!-- Server IP box -->
				<div class="notice notice-info inline" style="margin:16px 0;padding:12px 16px;">
					<h3 style="margin-top:0;"><?php esc_html_e( 'Your server IP address', 'skyservers-product-sync-for-emag' ); ?></h3>
					<p style="font-size:18px;font-weight:600;font-family:monospace;margin:8px 0;">
						<?php echo esc_html( $server_ip ?: __( 'Could not detect — check manually', 'skyservers-product-sync-for-emag' ) ); ?>
					</p>
					<p>
						<strong><?php esc_html_e( 'This IP must be whitelisted in your eMAG seller account before any API call will work.', 'skyservers-product-sync-for-emag' ); ?></strong>
					</p>
					<p><?php esc_html_e( 'How to whitelist:', 'skyservers-product-sync-for-emag' ); ?></p>
					<ol>
						<li>
							<?php
							echo wp_kses_post(
								sprintf(
									/* translators: %s: link to marketplace.emag.ro */
									esc_html__( 'Log in to %s.', 'skyservers-product-sync-for-emag' ),
									'<a href="https://marketplace.emag.ro" target="_blank" rel="noopener">marketplace.emag.ro</a>'
								)
							);
							?>
						</li>
						<li>
							<?php
							echo wp_kses_post(
								sprintf(
									/* translators: %1$s: bold "My Account → Technical Details"; %2$s: bold "IP Addresses" */
									esc_html__( 'Go to %1$s tab and scroll to %2$s.', 'skyservers-product-sync-for-emag' ),
									'<strong>' . esc_html__( 'My Account → Technical Details', 'skyservers-product-sync-for-emag' ) . '</strong>',
									'<strong>' . esc_html__( 'IP Addresses', 'skyservers-product-sync-for-emag' ) . '</strong>'
								)
							);
							?>
						</li>
						<li>
							<?php
							echo wp_kses_post(
								sprintf(
									/* translators: %s: bold button label "Add a new IP" */
									esc_html__( 'Click the green %s button.', 'skyservers-product-sync-for-emag' ),
									'<strong>' . esc_html__( 'Add a new IP', 'skyservers-product-sync-for-emag' ) . '</strong>'
								)
							);
							?>
						</li>
						<li><?php esc_html_e( 'Enter the IP address shown above and save.', 'skyservers-product-sync-for-emag' ); ?></li>
					</ol>
					<p>
						<small>
							<?php esc_html_e( 'Note: some hosting providers use a different outgoing (egress) IP than the server IP shown above. If you still get HTTP 401/403 after whitelisting this IP, ask your hosting provider for the correct outgoing IP. The eMAG response also sometimes echoes the IP that attempted the request — check the Logs page.', 'skyservers-product-sync-for-emag' ); ?>
						</small>
					</p>
				</div>

				<!-- ── 2. Common issues at first run ────────────────── -->
				<h2 id="skyemag-common-issues"><?php esc_html_e( 'Common issues at first run', 'skyservers-product-sync-for-emag' ); ?></h2>
				<p><?php esc_html_e( 'Ordered by how often new installs hit them. Each entry lists the most likely cause first, so work top to bottom.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Test Connection fails', 'skyservers-product-sync-for-emag' ); ?></h3>
				<ol>
					<li><?php esc_html_e( 'Wrong username or password — typo, copied a space, or eMAG account locked.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'IP not whitelisted yet, or the wrong IP was whitelisted (server IP vs egress IP).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Wrong region selected — your account is RO and you picked HU (or similar).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Hosting firewall or proxy blocking outbound HTTPS to marketplace-api.emag.ro.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ol>
				<p><?php esc_html_e( 'How to verify: open Logs and read the last entry — the eMAG raw response usually contains the exact reason (invalid credentials, IP not allowed, etc.).', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Products show "Not Ready" — nothing publishes', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Hover the "Not Ready" badge on the Products or Publish page — the tooltip lists every reason. Most common:', 'skyservers-product-sync-for-emag' ); ?></p>
				<ol>
					<li><?php esc_html_e( 'Mandatory characteristic missing — open Char. Map for that category and either map a WC attribute or set a default.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Category not mapped — open Category Map and link the WC category to an eMAG one.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Brand or EAN missing — set Brand Source / EAN Source in Publish Settings or fill the meta on the product.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Image too small (Pro) — main product image below the Min image size px setting.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ol>

				<h3><?php esc_html_e( 'Sync runs but nothing changes on eMAG', 'skyservers-product-sync-for-emag' ); ?></h3>
				<ol>
					<li><?php esc_html_e( 'Master Switch in Settings is OFF — automatic cron sync is paused.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Per-product Sync toggle is off on the Products page.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Sync filters in Settings are excluding the product (min price, max weight, SKU regex, skip-zero-stock).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Stock % = 0 sends 0 (the offer is hidden but not deactivated).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'WP-Cron is not firing on your host — switch to a real system cron and trigger a manual sync to test.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ol>

				<h3><?php esc_html_e( 'Price rejected: "below minimum" / "above maximum"', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'eMAG validates each new sale_price against the currently stored min_sale_price / max_sale_price. To lower a price below the existing minimum, the plugin sends two requests in sequence: first to lower the minimum, then to send the new price. This happens automatically whenever your computed price falls below the stored minimum.', 'skyservers-product-sync-for-emag' ); ?></p>
				<p><?php esc_html_e( 'If you still see the error: check the Min / Max Margin % values on the product (or global) — empty or impossible values cause silent skips. Tip: set Min % low from the start (5-10%) so you never need the dance for normal price moves.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'EAN rejected after publish', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Means another seller already attached an offer to that EAN with conflicting product data (different brand, different name, etc.). eMAG locks the EAN and rejects mismatched submissions.', 'skyservers-product-sync-for-emag' ); ?></p>
				<ul>
					<li><?php esc_html_e( 'Use the "Check EANs on eMAG" button on the Publish page before sending — it tells you whether each EAN is already in eMAG\'s catalogue.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'If you own the brand, escalate via eMAG support (Mira chatbot in your seller account). They can reset the EAN binding.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Otherwise: change the product\'s EAN/GTIN to one that is yours and republish.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>

				<h3><?php esc_html_e( 'Publish rejected: "You are trying to change both part_number and name at the same time"', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'This error from eMAG means your seller account already contains an offer with the same internal id as the product you are publishing — usually created earlier by another integration, feed, or a previous shop setup. The plugin uses the WooCommerce product ID as the eMAG internal id, so on accounts with a pre-existing catalogue the two number ranges can overlap. eMAG then treats the publish as an update of the older offer and rejects it, because changing both the SKU and the name of an existing offer in one request is not allowed.', 'skyservers-product-sync-for-emag' ); ?></p>
				<ul>
					<li><?php esc_html_e( 'If the product already exists on eMAG: do not use Publish. Run Settings → eMAG Data Import instead — it links your existing eMAG offers to WooCommerce products by SKU, and price/stock sync takes over from there.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'If it is a genuinely new product that just collides with an old id: set a Publish ID offset in Settings → Publish Settings (for example 1000000) and publish again. The offset shifts all future publish ids above your existing range; products already published or imported keep their ids and are not affected.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>
				<p><?php esc_html_e( 'The plugin also checks for this collision before sending and blocks the publish with a clear message, so with a correct offset configured you should never see the raw eMAG error.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Validation: "Documentation pending" never moves forward', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'eMAG runs human review on new offers. 5-15 days is normal. The plugin can\'t speed this up, but it can avoid common mistakes that trigger rejections:', 'skyservers-product-sync-for-emag' ); ?></p>
				<ul>
					<li><?php esc_html_e( 'Brand must be recognised by eMAG. Use the Detect button in Brand Source to verify your meta keys.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Main image must be high resolution (≥600 px on longest side by default — adjust in Publish Settings).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Description and characteristics should match the actual product — eMAG checks consistency.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>
				<p><?php esc_html_e( 'If status moves to "Documentation rejected" — open the offer in your eMAG seller account, read the rejection reason, fix it on the WC product, and republish.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Error: "Duplicate product found, please submit each product only once"', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'This error from eMAG means the same offer id was sent twice in one sync batch — two or more WooCommerce products are mapped to the same eMAG offer. The usual cause: a mapped product was duplicated with WooCommerce\'s "Duplicate" action on an older plugin version, and the copy inherited the original\'s eMAG mapping. Since version 3.9.2 duplication no longer copies the mapping, but duplicates created earlier still need a one-time repair.', 'skyservers-product-sync-for-emag' ); ?></p>
				<p><?php esc_html_e( 'Why it matters beyond the error: when the wrongly mapped copy is synced on its own (outside a batch), eMAG accepts the request and the copy silently overwrites the original offer\'s price and stock. Fix duplicate mappings even if the error only appears occasionally.', 'skyservers-product-sync-for-emag' ); ?></p>
				<p><strong><?php esc_html_e( 'How to repair — in this order:', 'skyservers-product-sync-for-emag' ); ?></strong></p>
				<ol>
					<li><?php esc_html_e( 'Open the Products page. If duplicate mappings exist, a warning box lists every affected group and marks which product keeps the mapping (the one whose SKU matches the eMAG part_number). Click "Fix automatically" — every wrongly mapped product is unlinked in one step.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Run "Re-import from eMAG" (Settings → eMAG Data Import). Unlinked products that have their own separate offer on eMAG are re-matched automatically by SKU; products without an eMAG offer simply stay unmapped.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Run a manual sync and check the Activity Logs — the "Duplicate product found" errors should be gone.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ol>
				<ul>
					<li><?php esc_html_e( 'If a group is marked ambiguous (no SKU matches the eMAG part_number), the repair tool skips it on purpose. Decide yourself which product is the right one and click "Unlink" on the others — the link sits next to the eMAG # badge on the Products page.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( '"Unlink" only removes the local mapping (offer id, part_number, price bounds). The offer on eMAG is never touched, and your margin settings on the product are kept.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'During sync the plugin also blocks any product whose offer id is claimed by another product, so a bad mapping can no longer overwrite the wrong offer — the blocked product is listed in the Activity Logs with the reason.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>

				<!-- ── 3. How sync works ────────────────────────────── -->
				<h2 id="skyemag-how-it-works"><?php esc_html_e( 'How sync works', 'skyservers-product-sync-for-emag' ); ?></h2>

				<h3><?php esc_html_e( 'Pricing', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><code>eMAG sale_price = WC price (ex. VAT) × (1 + effective_margin / 100)</code></p>
				<p>
					<?php
					echo wp_kses_post(
						sprintf(
							/* translators: %1$s: bold "per-product"; %2$s: bold "parent (variations)"; %3$s: bold "global" */
							esc_html__( 'Margin cascade: %1$s → %2$s → %3$s. The first non-empty value wins.', 'skyservers-product-sync-for-emag' ),
							'<strong>' . esc_html__( 'per-product', 'skyservers-product-sync-for-emag' ) . '</strong>',
							'<strong>' . esc_html__( 'parent (variations)', 'skyservers-product-sync-for-emag' ) . '</strong>',
							'<strong>' . esc_html__( 'global', 'skyservers-product-sync-for-emag' ) . '</strong>'
						)
					);
					?>
				</p>
				<div class="notice notice-warning inline" style="margin:12px 0;">
					<h4 style="margin-top:8px;"><?php esc_html_e( 'eMAG limitation: price bound order matters', 'skyservers-product-sync-for-emag' ); ?></h4>
					<p><?php esc_html_e( 'eMAG validates the new sale_price against the currently stored min/max on their servers — not against the new bounds you send in the same request. The plugin auto-handles this with a two-call sequence whenever the new price falls below the stored minimum: first call ships only the new min_sale_price, the second call sends the actual price.', 'skyservers-product-sync-for-emag' ); ?></p>
					<p><?php esc_html_e( 'Best practice: set Min % low (5-10%) from the start so normal price moves stay inside bounds.', 'skyservers-product-sync-for-emag' ); ?></p>
				</div>

				<h3><?php esc_html_e( 'Stock', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><code>eMAG stock = ceil(WC stock × effective_stock_percent / 100)</code></p>
				<ul>
					<li><strong><?php esc_html_e( 'Stock % = 0', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'sends 0 (offer hidden but still active, fast to revert).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Stock % empty', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'uses the global default.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Min WC Stock threshold', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'if WC stock ≤ threshold, sends 0 (safety buffer).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'WC stock = 0', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'product skipped entirely.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>

				<h3><?php esc_html_e( 'Deactivating products on eMAG', 'skyservers-product-sync-for-emag' ); ?></h3>
				<ul>
					<li><strong><?php esc_html_e( 'Stock % = 0', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'soft hide (out of stock). Easily reversible by setting a stock % > 0.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Deactivate (status=0)', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'completely removes the offer from eMAG. Reactivate with the green "Activate" button (status=1).', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>

				<?php if ( $is_premium ) : ?>
				<h3><?php esc_html_e( 'Buy Button tracking', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Updated every 6 hours by cron, or on demand from the Products page. Tracks your rank against competing offers, the number of sellers, and the best competing price.', 'skyservers-product-sync-for-emag' ); ?></p>
				<ul>
					<li><?php esc_html_e( 'Rank 1 (gold) — your offer owns the "Add to Cart" button.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Rank 2-4 — you are visible in the seller list but not the default choice.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Rank > 4 or absent — buyers must scroll to find you.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>
				<p><?php esc_html_e( 'The plugin sends an email when your rank drops from 1 to 2+ on a product, and another when a competitor undercuts you below your set threshold. Throttled so you do not get spammed during a price war.', 'skyservers-product-sync-for-emag' ); ?></p>
				<?php endif; ?>

				<h3><?php esc_html_e( 'Validation status (after publish)', 'skyservers-product-sync-for-emag' ); ?></h3>
				<ul>
					<li><strong><?php esc_html_e( 'Approved (9)', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'live, can be sold.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Documentation pending (4)', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'eMAG human review queued (5-15 days normal).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Documentation rejected (8)', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'fix the rejection reason in your eMAG seller account and republish.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Brand rejected (5)', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'brand not accepted on eMAG; either correct the brand or escalate via support.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'EAN rejected (6)', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'EAN conflict with an existing offer; offer deactivated until fixed.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Blocked (10)', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'blocked by eMAG admin or automated flow; check the offer details on eMAG for the reason.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>

				<h3><?php esc_html_e( 'EAN matching', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'When publishing, if your product\'s EAN already exists in eMAG\'s catalogue, your offer attaches to that existing product automatically — you become another seller on the same listing. Use the Check EANs button on the Publish page to verify before sending; the result lets you spot collisions early.', 'skyservers-product-sync-for-emag' ); ?></p>

				<!-- ── 4. Daily fulfillment — Orders & AWB ──────────── -->
				<?php if ( ! $is_premium ) : ?>
				<h2 id="skyemag-orders-awb"><?php esc_html_e( 'Daily fulfillment — Premium add-on', 'skyservers-product-sync-for-emag' ); ?></h2>
				<div class="notice notice-info inline" style="margin:12px 0;padding:14px 18px;">
					<p style="margin:0 0 8px;">
						<?php esc_html_e( 'Order import (webhook + manual by id), AWB shipping label issuance and printing, Buy Button rank tracking with drop alerts, and per-event email notifications (sync errors, blocked products, competitor undercuts, new orders, AWB issued) are part of the separate Premium add-on. With it, daily fulfillment runs entirely inside WordPress — incoming eMAG orders land in WooCommerce automatically, you issue and print AWBs (A4 / A5 / A6 / ZPL) directly from the WC order edit screen, and you receive operational emails when something needs your attention.', 'skyservers-product-sync-for-emag' ); ?>
					</p>
					<p style="margin:8px 0;">
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: %s: mailto link to contact@sky-servers.com */
								esc_html__( 'Bonus for every new Pro customer: a free 1-2 hour onboarding video call. Email %s after activation to book it — we configure your eMAG API credentials together, run the initial offer bootstrap, set up your category mapping, and walk through your first sync.', 'skyservers-product-sync-for-emag' ),
								'<a href="mailto:contact@sky-servers.com">contact@sky-servers.com</a>'
							)
						);
						?>
					</p>
					<p style="margin:8px 0 0;">
						<a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-primary" target="_blank" rel="noopener">
							<?php esc_html_e( 'Upgrade to Premium →', 'skyservers-product-sync-for-emag' ); ?>
						</a>
					</p>
				</div>
				<?php else : ?>
				<h2 id="skyemag-orders-awb"><?php esc_html_e( 'Daily fulfillment — Orders &amp; AWB', 'skyservers-product-sync-for-emag' ); ?></h2>
				<p><?php esc_html_e( 'Bring eMAG orders into WooCommerce automatically as they happen, then issue and print shipping labels (AWBs) directly from the WC order edit screen. You never need to open marketplace.emag.ro for daily fulfillment.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Setup', 'skyservers-product-sync-for-emag' ); ?></h3>
				<ol>
					<li>
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: %1$s: italic "SkyServers Sync → Orders & AWB"; %2$s: bold toggle name */
								esc_html__( 'Open %1$s and enable %2$s.', 'skyservers-product-sync-for-emag' ),
								'<em>' . esc_html__( 'SkyServers Sync → Orders &amp; AWB', 'skyservers-product-sync-for-emag' ) . '</em>',
								'<strong>' . esc_html__( 'Import orders from eMAG', 'skyservers-product-sync-for-emag' ) . '</strong>'
							)
						);
						?>
					</li>
					<li><?php esc_html_e( 'Review the Status mapping (eMAG codes 0-5 → WC statuses). Defaults: 1/2/3 → processing, 4 → completed, 0 → cancelled, 5 → refunded.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Decide on the customer setting (default: import as guest checkouts).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Optionally enable the admin email for new imported orders (uses the address from the Notifications page).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li>
						<?php esc_html_e( 'Copy the two webhook callback URLs from the Orders page and paste them in your eMAG seller account, under the technical settings section:', 'skyservers-product-sync-for-emag' ); ?>
						<ul style="margin:6px 0 6px 20px;list-style:disc;">
							<li><strong><?php esc_html_e( 'Order callback URL', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'fired when a new order arrives.', 'skyservers-product-sync-for-emag' ); ?></li>
							<li><strong><?php esc_html_e( 'Order cancellation URL', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'fired when an order is cancelled.', 'skyservers-product-sync-for-emag' ); ?></li>
						</ul>
						<p style="margin:6px 0;font-size:90%;">
							<?php
							echo wp_kses_post(
								sprintf(
									/* translators: %1$s: code "&order_id="; %2$s: code "https://example.com/path?order_id=123" */
									esc_html__( 'URL format: both URLs intentionally end with %1$s. eMAG appends the actual order id directly to the end of the URL when firing the callback (e.g. %2$s). Do not trim the trailing query string when pasting.', 'skyservers-product-sync-for-emag' ),
									'<code>&amp;order_id=</code>',
									'<code>https://example.com/path?order_id=123</code>'
								)
							);
							?>
						</p>
					</li>
				</ol>
				<div class="notice notice-info inline" style="margin:12px 0;padding:10px 14px;">
					<p style="margin:0;">
						<strong><?php esc_html_e( 'If the callback URL fields are not editable on your account', 'skyservers-product-sync-for-emag' ); ?></strong>
						— <?php esc_html_e( 'eMAG support has to register them on your behalf. Open a ticket via the Mira chatbot in your seller account, paste both URLs, and ask them to set up the callbacks for your account. Until they do, only manual import works — the webhook handler is implemented and ready, it just needs eMAG to start firing.', 'skyservers-product-sync-for-emag' ); ?>
					</p>
				</div>

				<h3><?php esc_html_e( 'Manual import by ID', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'The Orders page has a Manual import by ID field. Useful for:', 'skyservers-product-sync-for-emag' ); ?></p>
				<ul>
					<li><?php esc_html_e( 'Orders that existed on eMAG before you configured callbacks (historical backfill).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Spot-checking the import flow during setup.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Recovering an order the webhook missed (rare, but happens).', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>
				<p><?php esc_html_e( 'Enter the eMAG order id, click Import. The plugin re-uses the same pipeline as the webhook (fetch, map, save, acknowledge) and reports the resulting WC order id. Already-imported orders are detected and skipped.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Dry-run mode', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'The Dry-run toggle on the Orders page is ON by default. While ON, the plugin reads orders from eMAG and writes what it would have created into the Activity Log, but does not insert anything into the database. Use it to validate the mapping (line items, totals, status, customer) before turning it OFF and running for real.', 'skyservers-product-sync-for-emag' ); ?></p>
				<p><strong><?php esc_html_e( 'Recommended workflow:', 'skyservers-product-sync-for-emag' ); ?></strong></p>
				<ol>
					<li><?php esc_html_e( 'Leave dry-run ON. Trigger a manual import by ID for a known order.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Open Logs, filter by "order_import". Verify the mapping matches what you see on eMAG.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'When happy, turn dry-run OFF. From now on, imports persist as real WC orders.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ol>

				<div class="notice notice-warning inline" style="margin:12px 0;padding:10px 14px;">
					<h4 style="margin-top:8px;"><?php esc_html_e( 'Stock auto-decrement — leave OFF if invoicing software already decrements stock', 'skyservers-product-sync-for-emag' ); ?></h4>
					<p><?php esc_html_e( 'The Auto-decrement WooCommerce stock toggle is OFF by default and should stay OFF if any other integration already reduces your WC stock for eMAG orders. Common cases:', 'skyservers-product-sync-for-emag' ); ?></p>
					<ul style="margin:4px 0 4px 20px;list-style:disc;">
						<li><?php esc_html_e( 'Romanian invoicing services (SmartBill, Oblio, FGO and similar) — they hook WooCommerce stock on invoice issuance.', 'skyservers-product-sync-for-emag' ); ?></li>
						<li>
							<?php
							echo wp_kses_post(
								sprintf(
									/* translators: %s: code "woocommerce_order_status_changed" */
									esc_html__( 'An ERP or custom script — anything that subscribes to %s and decrements stock.', 'skyservers-product-sync-for-emag' ),
									'<code>woocommerce_order_status_changed</code>'
								)
							);
							?>
						</li>
					</ul>
					<p><?php esc_html_e( 'Turning this ON in those cases causes double stock reduction — your offers go out_of_stock artificially. Only enable when this plugin is the single source of truth for WC stock changes triggered by eMAG orders.', 'skyservers-product-sync-for-emag' ); ?></p>
					<p>
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: %1$s: bold "idempotent"; %2$s: code "_skyemag_stock_decremented"; %3$s: bold "does not auto-restore on cancellation" */
								esc_html__( 'When enabled, decrement is %1$s (each order is decremented at most once, tracked via the %2$s meta) and %3$s — if an order is cancelled or refunded after stock was decremented, you get a warning in the Logs and decide manually whether the goods came back.', 'skyservers-product-sync-for-emag' ),
								'<strong>' . esc_html__( 'idempotent', 'skyservers-product-sync-for-emag' ) . '</strong>',
								'<code>_skyemag_stock_decremented</code>',
								'<strong>' . esc_html__( 'does not auto-restore on cancellation', 'skyservers-product-sync-for-emag' ) . '</strong>'
							)
						);
						?>
					</p>
				</div>

				<h3><?php esc_html_e( 'AWB shipping', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Once an order is imported you can issue and print the eMAG AWB without leaving WordPress. The plugin does not pick which courier you use — eMAG uses your preferred courier account for home delivery, and forces the matching locker courier for pickup orders.', 'skyservers-product-sync-for-emag' ); ?></p>
				<ol>
					<li>
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: %s: bold "eMAG Shipping" meta box label */
								esc_html__( 'Open the WooCommerce order — sidebar shows the %s meta box.', 'skyservers-product-sync-for-emag' ),
								'<strong>' . esc_html__( 'eMAG Shipping', 'skyservers-product-sync-for-emag' ) . '</strong>'
							)
						);
						?>
					</li>
					<li>
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: %s: bold "Issue AWB" button label */
								esc_html__( 'Click %s. The plugin sends the request, eMAG generates the AWB and automatically moves the order status to "finalized" (mapped to your WC "completed" status).', 'skyservers-product-sync-for-emag' ),
								'<strong>' . esc_html__( 'Issue AWB', 'skyservers-product-sync-for-emag' ) . '</strong>'
							)
						);
						?>
					</li>
					<li>
						<?php
						echo wp_kses_post(
							sprintf(
								/* translators: %1$s: bold "Print AWB" button; %2$s: italic "Other format ▾" link */
								esc_html__( 'Click %1$s to download the shipping label in your default format (A6 by default for thermal printers). Use %2$s for A4, A5 or ZPL — your choice becomes the new default.', 'skyservers-product-sync-for-emag' ),
								'<strong>' . esc_html__( 'Print AWB', 'skyservers-product-sync-for-emag' ) . '</strong>',
								'<em>' . esc_html__( 'Other format ▾', 'skyservers-product-sync-for-emag' ) . '</em>'
							)
						);
						?>
					</li>
				</ol>
				<p><?php esc_html_e( 'Note on courier selection: if you want a different courier than eMAG defaults to, change the preferred courier account in your eMAG seller settings. The plugin will pick that one up automatically on the next AWB.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Troubleshooting AWB issuance', 'skyservers-product-sync-for-emag' ); ?></h3>
				<ul>
					<li><strong><?php esc_html_e( 'Sender phone is required', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'set it under Orders & AWB → AWB shipping. Any human format is accepted (it gets normalised on save).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'No default pickup address found', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'mark a pickup address as default in your eMAG seller account, then click "Refresh from eMAG" in the AWB shipping settings.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Invalid pickup point (locker)', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'the locker may have closed temporarily; verify in your eMAG account and re-issue.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Order is finalized, cannot issue another', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'AWB was already issued elsewhere. Click "Refresh status" to sync the existing AWB into WC.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'FBE orders (fulfilled by eMAG)', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'shipped by eMAG. No AWB needed from you.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>
				<?php endif; /* end $is_premium daily-fulfillment block */ ?>

				<!-- ── 5. Reference: menu pages ─────────────────────── -->
				<h2 id="skyemag-menu-pages"><?php esc_html_e( 'Reference: menu pages', 'skyservers-product-sync-for-emag' ); ?></h2>
				<p><?php esc_html_e( 'Quick lookup for what each page does. For step-by-step setup, use the Quick start section above.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Settings', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Global configuration. Master Switch, API credentials, margin / stock defaults, sync schedule, sync filters, eMAG Categories import, eMAG Data Import, Publish Settings (warranty, resupply days, brand/EAN sources, send-to-eMAG-live, publish ID offset).', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Products', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Per-product Sync toggle, Margin / Min / Max % overrides, Stock %, Buy Button rank, validation status, Activate / Deactivate buttons, Margin history. Filters: All, Sync on/off, Mapped/Unmapped, Ready, Not Ready, Inactive, Rejected, Errors.', 'skyservers-product-sync-for-emag' ); ?></p>

				<?php if ( $is_premium ) : ?>
				<h3><?php esc_html_e( 'Category Map', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Links WooCommerce categories to eMAG categories. Sub-categories inherit parent mapping. Required before publishing.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Characteristics Map', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Maps eMAG characteristics (per category) to WC attributes or default values. Mandatory characteristics without a mapping block publishing.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Family Map', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Maps eMAG family types to WC variation attributes for variable products.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Publish', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Inline edit margins / stock before publishing. Preview JSON payload per product. Dry Run / Publish individually or in bulk. Export JSON for offline review. Check EANs on eMAG to spot conflicts before sending. Search by name/SKU/EAN, sort by name/SKU/price/stock.', 'skyservers-product-sync-for-emag' ); ?></p>

				<h3><?php esc_html_e( 'Orders &amp; AWB', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p>
					<?php
					echo wp_kses_post(
						sprintf(
							/* translators: %s: anchor link to Daily fulfillment section */
							esc_html__( 'Configures eMAG order import + AWB shipping. Imported orders appear in the native WooCommerce → Orders screen with a source badge; each order gets an "eMAG Shipping" meta box for issuing and printing AWBs. See %s for details.', 'skyservers-product-sync-for-emag' ),
							'<a href="#skyemag-orders-awb">' . esc_html__( 'Daily fulfillment', 'skyservers-product-sync-for-emag' ) . '</a>'
						)
					);
					?>
				</p>

				<h3><?php esc_html_e( 'Notifications', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Sets the email recipient for sync errors, blocked products, competitor undercuts, Buy Button drops, and new eMAG order events. Per-event toggles. 1-hour throttle prevents notification storms.', 'skyservers-product-sync-for-emag' ); ?></p>
				<?php endif; ?>

				<h3><?php esc_html_e( 'Logs', 'skyservers-product-sync-for-emag' ); ?></h3>
				<p><?php esc_html_e( 'Activity log of every operation, error, and event. Filterable by action (sync, publish, order_import, awb, sanitize, notify) and severity. Auto-prune retention configurable from 7 to 365 days.', 'skyservers-product-sync-for-emag' ); ?></p>

				<!-- ── 6. Other errors & tech support ───────────────── -->
				<h2 id="skyemag-other-errors"><?php esc_html_e( 'Other errors &amp; tech support', 'skyservers-product-sync-for-emag' ); ?></h2>
				<ul>
					<li><strong>HTTP 401 / 403</strong> — <?php esc_html_e( 'wrong credentials or IP not whitelisted (or wrong IP whitelisted — check egress IP from your hosting).', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong>HTTP 429</strong> — <?php esc_html_e( 'rate limited (3 req/sec, 180 req/min). The plugin auto-retries with backoff; if you see many of these, slow down manual operations.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong>HTTP 5xx</strong> — <?php esc_html_e( 'eMAG service is temporarily unavailable. The plugin retries; if it persists for hours, check the eMAG status page or open a Mira ticket.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><strong><?php esc_html_e( 'Timeout / connection failed', 'skyservers-product-sync-for-emag' ); ?></strong> — <?php esc_html_e( 'firewall or proxy blocking outbound HTTPS to marketplace-api.emag.ro, or DNS resolution issue on your host.', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>
				<p>
					<strong><?php esc_html_e( 'Before opening a support ticket', 'skyservers-product-sync-for-emag' ); ?>:</strong>
					<?php esc_html_e( 'open the Logs page first. Most errors are logged with the eMAG raw response — that text is exactly what eMAG support needs. Copy the relevant entry into a Mira ticket on your seller account, or paste it in your email to the plugin support address.', 'skyservers-product-sync-for-emag' ); ?>
				</p>

				<!-- ── 7. Technical details ─────────────────────────── -->
				<h2 id="skyemag-technical"><?php esc_html_e( 'Technical details', 'skyservers-product-sync-for-emag' ); ?></h2>
				<ul>
					<?php if ( $plugin_version ) : ?>
						<li>
							<?php
							printf(
								/* translators: %s: plugin version number */
								esc_html__( 'Plugin version: %s', 'skyservers-product-sync-for-emag' ),
								'<code>' . esc_html( $plugin_version ) . '</code>'
							);
							?>
						</li>
					<?php endif; ?>
					<li><?php esc_html_e( 'PHP 7.4+ (8.x compatible) — WooCommerce 7.0+', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'eMAG Marketplace API v4.5.1', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Rate limits: 3 req/sec, 180 req/min — EAN lookup: 5 req/sec', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'Auth: HTTP Basic. Password stored AES-256-CBC encrypted with your AUTH_KEY salt.', 'skyservers-product-sync-for-emag' ); ?></li>
					<li><?php esc_html_e( 'WooCommerce HPOS compatible. Full data cleanup on uninstall (toggleable, default OFF — keeps Free→Pro migrations safe).', 'skyservers-product-sync-for-emag' ); ?></li>
				</ul>

			</div>
		</div>
		<?php
	}

	/**
	 * Detect the server's external IP address.
	 */
	public static function detect_server_ip(): string {
		$cached = get_transient( 'skyemag_server_ip' );
		if ( $cached ) {
			return $cached;
		}

		$ip = '';

		// Method 1: external service (most reliable for outgoing IP).
		$response = wp_remote_get( 'https://api.ipify.org', [
			'timeout'   => 5,
			'sslverify' => true,
		] );
		if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
			$ip = trim( wp_remote_retrieve_body( $response ) );
		}

		// Method 2: fallback to SERVER_ADDR.
		if ( empty( $ip ) && ! empty( $_SERVER['SERVER_ADDR'] ) ) {
			$ip = sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) );
		}

		if ( $ip ) {
			set_transient( 'skyemag_server_ip', $ip, HOUR_IN_SECONDS );
		}

		return $ip;
	}
}
