<?php
/**
 * Class Noesia Cs Admin.
 *
 * @package Noesia_Commerce_Suite
 */

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

/**
 * Class implementation.
 */
class Noesia_CS_Admin {
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $passport_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $analytics_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $doctor_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $impact_map_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $email_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $reorder_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $data_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $feature_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $system_status_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $care_action_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $settings_binding_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $commerce_view_service;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $module_registry;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $action_router;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $renderer;
	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $page_hook = '';

	/**
	 * Runtime dependency or state value.
	 *
	 * @var mixed
	 */
	private $tabs = array(
		'dashboard'               => 'Dashboard',
		'order-care-hub'          => 'Order Care Hub',
		'reorder-reminders'       => 'Reorder Reminders',
		'reviews-support'         => 'Reviews Support',
		'intelligence-analytics'  => 'Intelligence & Analytics',
		'impact-map'              => 'Impact Map',
		'ecommerce-doctor'        => 'Ecommerce Doctor',
		'emails'                  => 'Emails',
		'settings'                => 'Settings',
	);

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $passport_service Passport service.
	 * @param mixed $analytics_service Analytics service.
	 * @param mixed $doctor_service Doctor service.
	 * @param mixed $impact_map_service Impact map service.
	 * @param mixed $email_service Email service.
	 * @param mixed $reorder_service Reorder service.
	 * @param mixed $data_service Data service.
	 * @param mixed $feature_service Feature service.
	 * @param mixed $system_status_service System status service.
	 * @param mixed $care_action_service Care action service.
	 * @param mixed $settings_binding_service Settings binding service.
	 * @param mixed $container Container.
	 */
	public function __construct( Noesia_CS_Passport_Service $passport_service, Noesia_CS_Analytics_Service $analytics_service, Noesia_CS_Doctor_Service $doctor_service, Noesia_CS_Impact_Map_Service $impact_map_service, Noesia_CS_Email_Service $email_service, Noesia_CS_Reorder_Service $reorder_service, Noesia_CS_Data_Service $data_service, Noesia_CS_Feature_Service $feature_service, Noesia_CS_System_Status_Service $system_status_service, Noesia_CS_Care_Action_Service $care_action_service, Noesia_CS_Settings_Binding_Service $settings_binding_service, Noesia_CS_Container $container = null ) {
		$this->passport_service    = $passport_service;
		$this->analytics_service   = $analytics_service;
		$this->doctor_service      = $doctor_service;
		$this->impact_map_service  = $impact_map_service;
		$this->email_service       = $email_service;
		$this->reorder_service     = $reorder_service;
		$this->data_service        = $data_service;
		$this->feature_service     = $feature_service;
		$this->system_status_service = $system_status_service;
		$this->care_action_service = $care_action_service;
		$this->settings_binding_service = $settings_binding_service;
		$this->commerce_view_service = $container && method_exists( $container, 'get' ) ? $container->get( 'commerce_view_service' ) : null;
		if ( ! $this->commerce_view_service && class_exists( 'Noesia_CS_Commerce_View_Service' ) ) {
			$this->commerce_view_service = new Noesia_CS_Commerce_View_Service( null, $this->passport_service );
		}
		$this->module_registry = class_exists( 'Noesia_CS_Module_Registry' ) ? new Noesia_CS_Module_Registry( $container ) : null;
		if ( $this->module_registry ) {
			$this->tabs = $this->module_registry->tabs();
		}
		$this->renderer = class_exists( 'Noesia_CS_Admin_Renderer' ) ? new Noesia_CS_Admin_Renderer( $container, $this->module_registry ) : null;
		if ( $container && class_exists( 'Noesia_CS_Admin_Action_Router' ) ) {
			$this->action_router = new Noesia_CS_Admin_Action_Router( $container, $this, $this->module_registry );
		}
	}

	/**
	 * Return admin tabs with late Pro/filter support.
	 *
	 * Pro modules are loaded independently and can register their tabs through
	 * this filter without relying on constructor timing.
	 *
	 * @return array<string,string>
	 */
	private function get_tabs() {
		$tabs = $this->tabs;

		if ( $this->module_registry ) {
			$tabs = $this->module_registry->tabs();
		}

		return apply_filters( 'noesia_cs_admin_tabs', $tabs, $this );
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function hooks() {
		add_action( 'admin_menu', array( $this, 'admin_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		if ( $this->action_router ) {
			$this->action_router->hooks();
		}
		add_action( 'add_meta_boxes', array( $this, 'add_order_metabox' ), 20, 2 );
		add_action( 'admin_post_noesia_cs_open_care_page', array( $this, 'handle_open_care_page' ) );
		add_action( 'show_user_profile', array( $this, 'render_customer_profile_care_button' ) );
		add_action( 'edit_user_profile', array( $this, 'render_customer_profile_care_button' ) );
		add_filter( 'plugin_action_links_' . NOESIA_CS_BASENAME, array( $this, 'plugin_action_links' ) );
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function admin_menu() {
		$this->page_hook = add_menu_page(
			__( 'Noesia Commerce Suite', 'noesia-commerce-suite' ),
			__( 'Noesia Commerce', 'noesia-commerce-suite' ),
			NOESIA_CS_CAPABILITY,
			'noesia-commerce-suite',
			array( $this, 'render_page' ),
			'dashicons-chart-area',
			56
		);
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $links Links.
	 */
	public function plugin_action_links( $links ) {
		$settings_url = admin_url( 'admin.php?page=noesia-commerce-suite&tab=settings' );
		array_unshift( $links, '<a href="' . esc_url( $settings_url ) . '">' . esc_html__( 'Settings', 'noesia-commerce-suite' ) . '</a>' );
		return $links;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $hook Hook.
	 */
	public function enqueue_assets( $hook ) {
		$screen              = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		$id                  = $screen ? $screen->id : '';
		$is_app             = $hook === $this->page_hook || false !== strpos( $id, 'noesia-commerce-suite' );
		$is_order_screen     = in_array( $id, array( 'shop_order', 'woocommerce_page_wc-orders' ), true );
		$is_customer_screen  = in_array( $id, array( 'profile', 'user-edit' ), true );

		if ( ! $is_app && ! $is_order_screen && ! $is_customer_screen ) {
			return;
		}

		wp_enqueue_style( 'noesia-cs-admin', NOESIA_CS_URL . 'assets/css/noesia-cs-admin.css', array(), NOESIA_CS_VERSION );

		if ( $is_app && function_exists( 'wp_enqueue_media' ) ) {
			wp_enqueue_media();
		}

		if ( $is_customer_screen && ! $is_app && ! $is_order_screen ) {
			return;
		}

		wp_register_script( 'noesia-cs-qrcode', NOESIA_CS_URL . 'assets/libs/qrcode/noesia-cs-qrcode.js', array(), NOESIA_CS_VERSION, true );
		wp_enqueue_script( 'noesia-cs-settings-nav', NOESIA_CS_URL . 'assets/js/noesia-cs-settings-nav.js', array(), NOESIA_CS_VERSION, true );
		wp_enqueue_script( 'noesia-cs-admin', NOESIA_CS_URL . 'assets/js/noesia-cs-admin.js', array( 'noesia-cs-qrcode', 'noesia-cs-settings-nav' ), NOESIA_CS_VERSION, true );
		wp_localize_script(
			'noesia-cs-admin',
			'noesiaCommerceSuiteAdmin',
			array(
				'copyLabel'    => __( 'Copied', 'noesia-commerce-suite' ),
				'failedLabel'  => __( 'Copy failed', 'noesia-commerce-suite' ),
				'qr'           => array(
					'size'         => max( 80, min( 420, absint( Noesia_CS_Settings::get( 'qr_size', 160 ) ) ) ),
					'foreground'   => Noesia_CS_Settings::get( 'qr_foreground_color', '#111827' ),
					'background'   => Noesia_CS_Settings::get( 'qr_background_color', '#ffffff' ),
				),
				'dateStorageKey'                 => 'noesia_cs_dashboard_date_range',
				'loadingLabel'                   => __( 'Loading', 'noesia-commerce-suite' ),
				'ajaxUrl'                        => admin_url( 'admin-ajax.php' ),
				'impactRefreshingLabel'          => __( 'Refreshing', 'noesia-commerce-suite' ),
				'impactDateStorageKey'           => 'noesia_cs_impact_snapshot_range',
				'doctorScanLoadingLabel'         => __( 'Please wait. The scan may take a few minutes on large stores. Do not reload this page.', 'noesia-commerce-suite' ),
				'dashboardProductsNonce'         => wp_create_nonce( 'noesia_cs_dashboard_top_products' ),
				'dashboardDateNonce'             => wp_create_nonce( 'noesia_cs_dashboard_range' ),
				'dashboardProductsLoadingLabel'  => __( 'Loading products', 'noesia-commerce-suite' ),
				'adminSearchNonce'               => wp_create_nonce( 'noesia_cs_admin_search' ),
				'lastHourOrdersNonce'            => wp_create_nonce( 'noesia_cs_last_hour_orders' ),
				'adminSearchMinChars'            => 2,
				'adminSearchLoadingLabel'        => __( 'Searching', 'noesia-commerce-suite' ),
				'adminSearchEmptyLabel'          => __( 'No matching orders, passports or products found.', 'noesia-commerce-suite' ),
				'adminSearchCopiedLabel'         => __( 'Copied', 'noesia-commerce-suite' ),
				'settingsSavingLabel'            => __( 'Saving', 'noesia-commerce-suite' ),
				'settingsSavedLabel'             => __( 'Saved', 'noesia-commerce-suite' ),
			)
		);
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $post_type Post type.
	 * @param mixed $post_or_order Post or order.
	 */
	public function add_order_metabox( $post_type, $post_or_order = null ) {
		if ( ! class_exists( 'WooCommerce' ) || 'yes' !== Noesia_CS_Settings::get( 'enable_product_passport_qr', 'yes' ) || 'yes' !== Noesia_CS_Settings::get( 'show_qr_order_admin', 'yes' ) || 'yes' !== Noesia_CS_Settings::get( 'order_admin_metabox', 'yes' ) ) {
			return;
		}

		add_meta_box(
			'noesia-cs-order-passport',
			__( 'Noesia Order Care Hub', 'noesia-commerce-suite' ),
			array( $this, 'render_order_metabox' ),
			'shop_order',
			'side',
			'high'
		);

		add_meta_box(
			'noesia-cs-order-passport',
			__( 'Noesia Order Care Hub', 'noesia-commerce-suite' ),
			array( $this, 'render_order_metabox' ),
			'woocommerce_page_wc-orders',
			'side',
			'high'
		);
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $order_object Order object.
	 */
	public function render_order_metabox( $order_object ) {
		$order = $this->resolve_order_from_admin_object( $order_object );
		if ( ! $order ) {
			esc_html_e( 'Order unavailable.', 'noesia-commerce-suite' );
			return;
		}

		$has_token  = $this->passport_service->order_has_active_token( $order );
		$url        = $has_token ? $this->passport_service->get_existing_care_url_for_order( $order ) : '';
		$order_id   = absint( $order->get_id() );
		$generate_url = wp_nonce_url(
			admin_url( 'admin-post.php?action=noesia_cs_generate_token&order_id=' . $order_id ),
			'noesia_cs_generate_token_' . $order_id
		);
		$regenerate_url = wp_nonce_url(
			admin_url( 'admin-post.php?action=noesia_cs_regenerate_token&order_id=' . $order_id ),
			'noesia_cs_regenerate_token_' . $order_id
		);
		$open_care_url  = $this->open_care_page_action_url( $order_id );
		$suite_url      = admin_url( 'admin.php?page=noesia-commerce-suite' );
		?>
		<div class="noesia-cs-order-box">
			<?php if ( 'yes' === Noesia_CS_Settings::get( 'show_woo_care_button', 'yes' ) ) : ?>
				<div class="noesia-cs-care-launch">
					<div class="noesia-cs-care-launch__actions">
						<a class="button button-primary noesia-cs-open-care-button" href="<?php echo esc_url( $open_care_url ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php esc_html_e( 'Open Noesia Care Hub', 'noesia-commerce-suite' ); ?></a>
						<a class="button noesia-cs-open-suite-button" href="<?php echo esc_url( $suite_url ); ?>"><?php esc_html_e( 'Open Noesia Suite', 'noesia-commerce-suite' ); ?></a>
					</div>
					<span><?php esc_html_e( 'Secure care hub for this order and direct access to the Noesia Commerce Suite backend.', 'noesia-commerce-suite' ); ?></span>
				</div>
			<?php endif; ?>
			<?php if ( $has_token && $url ) : ?>
				<div class="noesia-cs-order-box__qr" data-noesia-cs-qr="<?php echo esc_url( $url ); ?>" data-noesia-cs-qr-size="<?php echo esc_attr( max( 80, min( 420, absint( Noesia_CS_Settings::get( 'qr_size', 160 ) ) ) ) ); ?>" data-noesia-cs-qr-foreground="<?php echo esc_attr( Noesia_CS_Settings::get( 'qr_foreground_color', '#111827' ) ); ?>" data-noesia-cs-qr-background="<?php echo esc_attr( Noesia_CS_Settings::get( 'qr_background_color', '#ffffff' ) ); ?>"></div>
				<p><strong><?php esc_html_e( 'Secure Order Care Link', 'noesia-commerce-suite' ); ?></strong></p>
				<input type="text" class="widefat noesia-cs-copy-source" readonly value="<?php echo esc_url( $url ); ?>">
				<p class="noesia-cs-order-actions">
					<a class="button button-primary" href="<?php echo esc_url( $url ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php esc_html_e( 'View Care Hub', 'noesia-commerce-suite' ); ?></a>
					<button type="button" class="button noesia-cs-copy-button" data-noesia-cs-copy="<?php echo esc_url( $url ); ?>"><?php esc_html_e( 'Copy Link', 'noesia-commerce-suite' ); ?></button>
					<?php if ( 'yes' === Noesia_CS_Settings::get( 'allow_printable_qr', 'no' ) ) : ?>
						<a class="button" href="<?php echo esc_url( $this->printable_qr_url( $order_id ) ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Print QR', 'noesia-commerce-suite' ); ?></a>
					<?php endif; ?>
				</p>
				<p><a class="button" href="<?php echo esc_url( $regenerate_url ); ?>" onclick="return confirm('<?php echo esc_js( __( 'Regenerate this token? The previous QR link will stop working.', 'noesia-commerce-suite' ) ); ?>');"><?php esc_html_e( 'Regenerate Token', 'noesia-commerce-suite' ); ?></a></p>
			<?php else : ?>
				<p><strong><?php esc_html_e( 'No Order Care Hub token yet.', 'noesia-commerce-suite' ); ?></strong></p>
				<p><?php esc_html_e( 'Generate a secure order-level care token before sharing a QR link.', 'noesia-commerce-suite' ); ?></p>
				<p><a class="button button-primary" href="<?php echo esc_url( $generate_url ); ?>"><?php esc_html_e( 'Generate Hub Link', 'noesia-commerce-suite' ); ?></a></p>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function handle_open_care_page() {
		if ( ! current_user_can( NOESIA_CS_CAPABILITY ) ) {
			wp_die( esc_html__( 'You do not have permission to open this care hub.', 'noesia-commerce-suite' ) );
		}

		$order_id = isset( $_GET['order_id'] ) ? absint( wp_unslash( $_GET['order_id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is verified immediately after the order ID is known.
		if ( ! $order_id ) {
			wp_die( esc_html__( 'Order ID is missing.', 'noesia-commerce-suite' ) );
		}

		check_admin_referer( 'noesia_cs_open_care_page_' . $order_id );

		if ( 'yes' !== Noesia_CS_Settings::get( 'show_woo_care_button', 'yes' ) ) {
			wp_die( esc_html__( 'The Woo care button is disabled in Noesia Commerce Suite settings.', 'noesia-commerce-suite' ) );
		}

		$order = function_exists( 'wc_get_order' ) ? wc_get_order( $order_id ) : null;
		if ( ! $order || ! is_a( $order, 'WC_Order' ) ) {
			wp_die( esc_html__( 'Order unavailable.', 'noesia-commerce-suite' ) );
		}

		$url = $this->passport_service->get_care_url_for_order( $order );
		if ( ! $url ) {
			wp_die( esc_html__( 'Unable to open the order care hub for this order.', 'noesia-commerce-suite' ) );
		}

		wp_safe_redirect( $url );
		exit;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $user User.
	 */
	public function render_customer_profile_care_button( $user ) {
		if ( ! current_user_can( NOESIA_CS_CAPABILITY ) || 'yes' !== Noesia_CS_Settings::get( 'show_woo_care_button', 'yes' ) || ! class_exists( 'WooCommerce' ) || ! function_exists( 'wc_get_orders' ) ) {
			return;
		}

		$user_id = $user && isset( $user->ID ) ? absint( $user->ID ) : 0;
		if ( ! $user_id ) {
			return;
		}

		$orders = wc_get_orders(
			array(
				'customer_id'  => $user_id,
				'limit'        => 1,
				'orderby'      => 'date',
				'order'        => 'DESC',
				'return'       => 'objects',
				'status'       => array( 'completed', 'processing', 'on-hold' ),
			)
		);

		if ( empty( $orders ) || ! is_a( $orders[0], 'WC_Order' ) ) {
			return;
		}

		$order      = $orders[0];
		$order_id   = absint( $order->get_id() );
		$suite_url  = admin_url( 'admin.php?page=noesia-commerce-suite' );
		?>
		<h2><?php esc_html_e( 'Noesia Commerce Suite', 'noesia-commerce-suite' ); ?></h2>
		<table class="form-table" role="presentation">
			<tr>
				<th><label><?php esc_html_e( 'Order Care Hub', 'noesia-commerce-suite' ); ?></label></th>
				<td>
					<p class="noesia-cs-care-launch noesia-cs-care-launch--profile">
						<a class="button button-primary noesia-cs-open-care-button" href="<?php echo esc_url( $this->open_care_page_action_url( $order_id ) ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php esc_html_e( 'Open Noesia Care Hub', 'noesia-commerce-suite' ); ?></a>
						<a class="button noesia-cs-open-suite-button" href="<?php echo esc_url( $suite_url ); ?>"><?php esc_html_e( 'Open Noesia Suite', 'noesia-commerce-suite' ); ?></a>
					</p>
					<p class="description"><?php printf( /* translators: %s: dynamic value. */ esc_html__( 'Opens the latest valid WooCommerce order care hub for this customer: #%s. A secure token is created only by this privileged admin action if missing. Open Noesia Suite returns to the plugin backend.', 'noesia-commerce-suite' ), esc_html( $order->get_order_number() ) ); ?></p>
				</td>
			</tr>
		</table>
		<?php
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $order_id Order id.
	 */
	public function open_care_page_action_url( $order_id ) {
		$order_id = absint( $order_id );
		return wp_nonce_url(
			admin_url( 'admin-post.php?action=noesia_cs_open_care_page&order_id=' . $order_id ),
			'noesia_cs_open_care_page_' . $order_id
		);
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $order_object Order object.
	 */
	private function resolve_order_from_admin_object( $order_object ) {
		if ( is_a( $order_object, 'WC_Order' ) ) {
			return $order_object;
		}
		if ( is_object( $order_object ) && isset( $order_object->ID ) ) {
			return wc_get_order( absint( $order_object->ID ) );
		}
		$order_id = isset( $_GET['id'] ) ? absint( wp_unslash( $_GET['id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only order resolution inside WooCommerce admin context.
		if ( ! $order_id && isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only order resolution inside WooCommerce admin context.
			$order_id = absint( wp_unslash( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only order resolution inside WooCommerce admin context.
		}
		return $order_id ? wc_get_order( $order_id ) : null;
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function render_page() {
		if ( ! current_user_can( NOESIA_CS_CAPABILITY ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'noesia-commerce-suite' ) );
		}

		$commerce_view = isset( $_GET['view'] ) ? sanitize_key( wp_unslash( $_GET['view'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin view selector.
		$commerce_view = in_array( $commerce_view, array( 'orders', 'customers' ), true ) ? $commerce_view : '';
		$tab      = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'dashboard'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only plugin tab selector.
		$tabs     = $this->get_tabs();
		if ( in_array( $tab, array( 'product-passport-qr', 'order-care-page' ), true ) ) {
			$tab = 'order-care-hub';
		}
		$tab       = array_key_exists( $tab, $tabs ) ? $tab : 'dashboard';
		$settings  = Noesia_CS_Settings::get_all();
		$stats     = $this->analytics_service->get_dashboard_stats();
		$notice   = isset( $_GET['noesia_cs_notice'] ) ? sanitize_key( wp_unslash( $_GET['noesia_cs_notice'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin notice code.
		$doctor              = $this->doctor_service->get_last_report();
		$doctor_warnings     = is_array( $doctor ) && isset( $doctor['items'] ) ? count( $doctor['items'] ) : 0;
		$pro_active          = $this->feature_service->is_pro_active();
		$upgrade_url         = $this->feature_service->get_upgrade_url();
		$current_user        = wp_get_current_user();
		$user_label          = $current_user && $current_user->exists() ? $current_user->display_name : __( 'Administrator', 'noesia-commerce-suite' );
		$user_initial        = function_exists( 'mb_substr' ) ? mb_substr( $user_label, 0, 1 ) : substr( $user_label, 0, 1 );
		$user_initial        = $user_initial ? strtoupper( $user_initial ) : 'N';
		$last_order_summary  = $this->get_latest_order_summary();
		$last_hour_orders    = $this->get_last_hour_order_count();
		?>
		<div class="noesia-cs-app" data-noesia-cs-app="1" style="--noesia-cs-primary:<?php echo esc_attr( $settings['primary_color'] ); ?>;--noesia-cs-accent:<?php echo esc_attr( $settings['accent_color'] ); ?>;">
			<aside class="noesia-cs-sidebar">
				<div class="noesia-cs-brand">
					<div class="noesia-cs-brand__mark"><span>N</span></div>
					<div><strong>NOESIA</strong><em><?php esc_html_e( 'Commerce Suite', 'noesia-commerce-suite' ); ?></em><small><?php esc_html_e( 'for WooCommerce', 'noesia-commerce-suite' ); ?></small></div>
				</div>
				<nav class="noesia-cs-nav">
					<?php foreach ( $tabs as $key => $label ) : ?>
						<?php
						$nav_classes = array();
						if ( ! $commerce_view && $tab === $key ) {
							$nav_classes[] = 'is-active';
						}
						if ( $this->module_registry && $this->module_registry->is_primary( $key ) ) {
							$nav_classes[] = 'noesia-cs-nav__primary-app';
						}
						?>
						<?php
						$nav_note = apply_filters( 'noesia_cs_admin_tab_nav_note', '', $key );
						$nav_note = is_string( $nav_note ) ? $nav_note : '';
						?>
						<a class="<?php echo esc_attr( implode( ' ', $nav_classes ) ); ?>" href="<?php echo esc_url( $this->tab_url( $key ) ); ?>" data-noesia-cs-tab-link="<?php echo esc_attr( $key ); ?>">
							<span class="noesia-cs-nav__icon"><?php echo esc_html( $this->icon_for_tab( $key ) ); ?></span>
							<?php if ( '' !== $nav_note ) : ?>
								<span class="noesia-cs-nav__body">
									<span class="noesia-cs-nav__label"><?php echo esc_html( $label ); ?></span>
									<span class="noesia-cs-nav__note"><?php echo esc_html( $nav_note ); ?></span>
								</span>
							<?php else : ?>
								<?php echo esc_html( $label ); ?>
							<?php endif; ?>
						</a>
					<?php endforeach; ?>
					<?php if ( ! $pro_active ) : ?>
						<div class="noesia-cs-nav__pro-edition-group" aria-label="<?php esc_attr_e( 'Explore Pro Edition areas', 'noesia-commerce-suite' ); ?>">
							<span class="noesia-cs-nav__pro-edition-title"><?php esc_html_e( 'Explore Pro Edition', 'noesia-commerce-suite' ); ?></span>
							<a class="noesia-cs-nav__pro-edition-item" href="<?php echo esc_url( $upgrade_url ); ?>"><span class="noesia-cs-nav__icon">⌂</span><span><?php esc_html_e( 'Home', 'noesia-commerce-suite' ); ?></span><em><?php esc_html_e( 'Explore', 'noesia-commerce-suite' ); ?></em></a>
							<a class="noesia-cs-nav__pro-edition-item" href="<?php echo esc_url( $upgrade_url ); ?>"><span class="noesia-cs-nav__icon">◆</span><span><?php esc_html_e( 'Business', 'noesia-commerce-suite' ); ?></span><em><?php esc_html_e( 'Explore', 'noesia-commerce-suite' ); ?></em></a>
							<a class="noesia-cs-nav__pro-edition-item" href="<?php echo esc_url( $upgrade_url ); ?>"><span class="noesia-cs-nav__icon">◉</span><span><?php esc_html_e( 'Checkout Confidence', 'noesia-commerce-suite' ); ?></span><em><?php esc_html_e( 'Explore', 'noesia-commerce-suite' ); ?></em></a>
							<a class="noesia-cs-nav__pro-edition-item" href="<?php echo esc_url( $upgrade_url ); ?>"><span class="noesia-cs-nav__icon">◇</span><span><?php esc_html_e( 'Care Experience · EU DPP', 'noesia-commerce-suite' ); ?></span><em><?php esc_html_e( 'Explore', 'noesia-commerce-suite' ); ?></em></a>
							<a class="noesia-cs-nav__pro-edition-item" href="<?php echo esc_url( $upgrade_url ); ?>"><span class="noesia-cs-nav__icon">▦</span><span><?php esc_html_e( 'Customer QR Studio', 'noesia-commerce-suite' ); ?></span><em><?php esc_html_e( 'Explore', 'noesia-commerce-suite' ); ?></em></a>
							<a class="noesia-cs-nav__pro-edition-item" href="<?php echo esc_url( $upgrade_url ); ?>"><span class="noesia-cs-nav__icon">✦</span><span><?php esc_html_e( 'Smart Recovery Contacts', 'noesia-commerce-suite' ); ?></span><em><?php esc_html_e( 'Explore', 'noesia-commerce-suite' ); ?></em></a>
						</div>
					<?php endif; ?>
				</nav>
				<div class="noesia-cs-commerce-views" aria-label="<?php esc_attr_e( 'Noesia commerce operational views', 'noesia-commerce-suite' ); ?>">
					<div class="noesia-cs-commerce-views__title"><span><?php esc_html_e( 'Commerce Views', 'noesia-commerce-suite' ); ?></span><em><?php esc_html_e( 'Woo data', 'noesia-commerce-suite' ); ?></em></div>
					<a class="noesia-cs-commerce-view-button noesia-cs-commerce-view-button--orders <?php echo 'orders' === $commerce_view ? 'is-active' : ''; ?>" href="<?php echo esc_url( $this->commerce_view_url( 'orders' ) ); ?>" data-noesia-cs-commerce-view-link="orders"><i>◈</i><span><?php esc_html_e( 'Orders View', 'noesia-commerce-suite' ); ?></span></a>
					<a class="noesia-cs-commerce-view-button noesia-cs-commerce-view-button--customers <?php echo 'customers' === $commerce_view ? 'is-active' : ''; ?>" href="<?php echo esc_url( $this->commerce_view_url( 'customers' ) ); ?>" data-noesia-cs-commerce-view-link="customers"><i>◆</i><span><?php esc_html_e( 'Customers View', 'noesia-commerce-suite' ); ?></span></a>
				</div>
				<div class="noesia-cs-woo-navigator" data-noesia-cs-woo-navigator="1">
					<button type="button" class="noesia-cs-woo-navigator__toggle" data-noesia-cs-woo-nav-toggle aria-expanded="true"><span><?php esc_html_e( 'Woo Navigator', 'noesia-commerce-suite' ); ?></span><b>⌄</b></button>
					<div class="noesia-cs-woo-navigator__body" data-noesia-cs-woo-nav-body>
						<label class="noesia-cs-woo-navigator__mode"><input type="checkbox" data-noesia-cs-woo-target-toggle> <span><?php esc_html_e( 'Open links in new tab', 'noesia-commerce-suite' ); ?></span></label>
						<a data-noesia-cs-woo-link href="<?php echo esc_url( admin_url( 'admin.php?page=wc-orders' ) ); ?>"><span>↗</span><?php esc_html_e( 'Open Woo Orders Page', 'noesia-commerce-suite' ); ?></a>
						<a data-noesia-cs-woo-link href="<?php echo esc_url( admin_url( 'admin.php?page=wc-admin&path=/customers' ) ); ?>"><span>↗</span><?php esc_html_e( 'Customers Page', 'noesia-commerce-suite' ); ?></a>
						<a data-noesia-cs-woo-link href="<?php echo esc_url( admin_url( 'admin.php?page=wc-settings' ) ); ?>"><span>↗</span><?php esc_html_e( 'Woo Settings', 'noesia-commerce-suite' ); ?></a>
					</div>
				</div>
				<div class="noesia-cs-plan-card <?php echo $pro_active ? 'is-pro' : 'is-free'; ?>">
					<strong><?php echo esc_html( $this->feature_service->get_plan_label() ); ?></strong>
					<span><?php echo esc_html( $this->feature_service->get_plan_description() ); ?></span>
					<?php if ( $pro_active ) : ?>
						<a href="<?php echo esc_url( $this->tab_url( 'upgrade-pro' ) ); ?>"><?php esc_html_e( 'Open Pro Page', 'noesia-commerce-suite' ); ?></a>
					<?php else : ?>
						<a href="<?php echo esc_url( $upgrade_url ); ?>"><?php esc_html_e( 'Explore Pro Edition', 'noesia-commerce-suite' ); ?></a>
					<?php endif; ?>
				</div>
			</aside>

			<main class="noesia-cs-main">
				<header class="noesia-cs-topbar">
					<div class="noesia-cs-topbar__left"><button type="button" class="noesia-cs-menu-button" aria-label="<?php esc_attr_e( 'Toggle menu', 'noesia-commerce-suite' ); ?>" aria-expanded="true">☰</button><button type="button" class="noesia-cs-chrome-button" data-noesia-cs-wp-chrome-toggle aria-pressed="false" aria-label="<?php esc_attr_e( 'Focus view without WordPress chrome', 'noesia-commerce-suite' ); ?>">⛶</button></div>
					<div class="noesia-cs-command-search" data-noesia-cs-command-search="1">
						<label class="screen-reader-text" for="noesia-cs-command-search-input"><?php esc_html_e( 'Search orders, passports and products', 'noesia-commerce-suite' ); ?></label>
						<span class="noesia-cs-command-search__icon" aria-hidden="true">⌕</span>
						<input id="noesia-cs-command-search-input" type="search" autocomplete="off" placeholder="<?php esc_attr_e( 'Search orders, passports, products…', 'noesia-commerce-suite' ); ?>" data-noesia-cs-command-input>
						<div class="noesia-cs-command-search__results" data-noesia-cs-command-results hidden></div>
					</div>
					<div class="noesia-cs-topbar__right">
						<?php $this->render_calculator_launcher(); ?>
						<?php do_action( 'noesia_cs_admin_header_actions' ); ?>
						<a class="noesia-cs-doctor-pill" href="<?php echo esc_url( $this->tab_url( 'ecommerce-doctor' ) ); ?>" aria-label="<?php echo esc_attr( sprintf( /* translators: %d: numeric value. */ __( 'Open Ecommerce Doctor. Current warnings: %d', 'noesia-commerce-suite' ), $doctor_warnings ) ); ?>"><span><?php esc_html_e( 'Doctor Warnings', 'noesia-commerce-suite' ); ?></span><strong><?php echo esc_html( number_format_i18n( $doctor_warnings ) ); ?></strong></a>
						<?php if ( $pro_active ) : ?>
							<a class="noesia-cs-pro-badge" href="<?php echo esc_url( $this->tab_url( 'upgrade-pro' ) ); ?>"><?php esc_html_e( 'Pro Edition', 'noesia-commerce-suite' ); ?></a>
						<?php else : ?>
							<a class="noesia-cs-pro-button" href="<?php echo esc_url( $upgrade_url ); ?>"><?php esc_html_e( 'Explore Pro Edition', 'noesia-commerce-suite' ); ?></a>
						<?php endif; ?>
						<a class="noesia-cs-live-status noesia-cs-last-order-status <?php echo ! empty( $last_order_summary['has_order'] ) ? 'is-ok' : 'is-muted'; ?>" href="<?php echo esc_url( $last_order_summary['url'] ); ?>" aria-label="<?php echo esc_attr( $last_order_summary['aria'] ); ?>"><i></i><span><?php echo esc_html( $last_order_summary['label'] ); ?></span></a>
						<a class="noesia-cs-last-hour-pill" data-noesia-cs-last-hour-pill="1" href="<?php echo esc_url( $this->tab_url( 'intelligence-analytics' ) ); ?>" aria-label="<?php echo esc_attr__( 'Orders created in the last hour', 'noesia-commerce-suite' ); ?>"><strong data-noesia-cs-last-hour-count><?php echo esc_html( number_format_i18n( $last_hour_orders ) ); ?></strong><span><?php esc_html_e( 'orders / 1h', 'noesia-commerce-suite' ); ?></span></a>
						<div class="noesia-cs-store-select noesia-cs-store-pill"><span><?php esc_html_e( 'Store', 'noesia-commerce-suite' ); ?></span><strong><?php echo esc_html( $settings['company_name'] ); ?></strong></div>
					</div>
				</header>
				<?php $this->render_calculator_workspace(); ?>

				<?php if ( $notice ) : ?>
					<div class="noesia-cs-notice is-success"><?php echo esc_html( $this->notice_text( $notice ) ); ?></div>
				<?php endif; ?>


				<?php $commerce_view ? $this->render_commerce_view( $commerce_view ) : $this->render_tab_view( $tab, $stats, $settings ); ?>
			</main>
		</div>
		<?php
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $view View.
	 * @param mixed $args Args.
	 */
	private function commerce_view_url( $view, $args = array() ) {
		$view = sanitize_key( $view );
		if ( ! in_array( $view, array( 'orders', 'customers' ), true ) ) {
			$view = 'orders';
		}
		$url_args = array_merge(
			array(
				'page'  => 'noesia-commerce-suite',
				'view'  => $view,
			),
			is_array( $args ) ? $args : array()
		);
		return add_query_arg( $url_args, admin_url( 'admin.php' ) );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $view View.
	 */
	private function render_commerce_view( $view ) {
		if ( ! $this->commerce_view_service ) {
			$this->empty_panel( __( 'Commerce view service unavailable.', 'noesia-commerce-suite' ) );
			return;
		}

		if ( 'customers' === $view ) {
			$this->render_customers_view();
			return;
		}

		$this->render_orders_view();
	}

	/**
	 * Handles the related plugin operation.
	 */
	private function render_orders_view() {
		$status = isset( $_GET['noesia_cs_order_status'] ) ? sanitize_key( wp_unslash( $_GET['noesia_cs_order_status'] ) ) : 'all'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$page   = isset( $_GET['noesia_cs_orders_page'] ) ? absint( wp_unslash( $_GET['noesia_cs_orders_page'] ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$allowed_statuses  = array( 'all', 'processing', 'completed', 'on-hold', 'pending', 'cancelled', 'refunded', 'failed' );
		$status            = in_array( $status, $allowed_statuses, true ) ? $status : 'all';
		$data   = $this->commerce_view_service->get_orders_view(
			array(
				'page'    => $page,
				'limit'   => 20,
				'status'  => $status,
			)
		);
		$orders        = isset( $data['orders'] ) && is_array( $data['orders'] ) ? $data['orders'] : array();
		$total         = absint( $data['total'] ?? 0 );
		$max_pages     = max( 1, absint( $data['max_num_pages'] ?? 1 ) );
		$current_page  = max( 1, absint( $data['current_page'] ?? 1 ) );
		?>
		<section class="noesia-cs-commerce-view noesia-cs-commerce-view--orders">
			<div class="noesia-cs-feature-hero noesia-cs-commerce-hero">
				<div>
					<span><?php esc_html_e( 'Operational View', 'noesia-commerce-suite' ); ?></span>
					<h2><?php esc_html_e( 'Orders View', 'noesia-commerce-suite' ); ?></h2>
					<p><?php esc_html_e( 'A premium read-only control layer for recent WooCommerce orders. Edit, refunds and deep order operations remain safely inside native WooCommerce.', 'noesia-commerce-suite' ); ?></p>
				</div>
				<a class="noesia-cs-native-link" href="<?php echo esc_url( admin_url( 'admin.php?page=wc-orders' ) ); ?>"><?php esc_html_e( 'Open native Woo Orders', 'noesia-commerce-suite' ); ?> ↗</a>
			</div>
			<div class="noesia-cs-commerce-toolbar noesia-cs-panel">
				<div><strong><?php echo esc_html( number_format_i18n( $total ) ); ?></strong><span><?php esc_html_e( 'orders found', 'noesia-commerce-suite' ); ?></span></div>
				<nav aria-label="<?php esc_attr_e( 'Filter orders by status', 'noesia-commerce-suite' ); ?>">
					<?php foreach ( $allowed_statuses as $filter_status ) : ?>
						<a class="<?php echo $status === $filter_status ? 'is-active' : ''; ?>" href="<?php echo esc_url( $this->commerce_view_url( 'orders', array( 'noesia_cs_order_status' => $filter_status ) ) ); ?>" data-noesia-cs-commerce-view-link="orders"><?php echo esc_html( 'all' === $filter_status ? __( 'All', 'noesia-commerce-suite' ) : ( function_exists( 'wc_get_order_status_name' ) ? wc_get_order_status_name( $filter_status ) : $filter_status ) ); ?></a>
					<?php endforeach; ?>
				</nav>
			</div>
			<div class="noesia-cs-commerce-list noesia-cs-panel">
				<?php if ( empty( $orders ) ) : ?>
					<p><?php esc_html_e( 'No orders found for this status.', 'noesia-commerce-suite' ); ?></p>
				<?php else : ?>
					<?php foreach ( $orders as $order ) : ?>
						<article class="noesia-cs-commerce-order-row">
							<div class="noesia-cs-commerce-order-row__main">
								<span class="noesia-cs-commerce-thumb"><?php if ( ! empty( $order['thumbnail'] ) ) : ?><img src="<?php echo esc_url( $order['thumbnail'] ); ?>" alt=""><?php else : ?><b>□</b><?php endif; ?></span>
								<div>
									<strong><?php printf( /* translators: %s: dynamic value. */ esc_html__( 'Order #%s', 'noesia-commerce-suite' ), esc_html( $order['order_number'] ?? '' ) ); ?></strong>
									<small><?php echo esc_html( $order['customer_label'] ?? '' ); ?> · <?php printf( /* translators: %d: numeric value. */ esc_html__( '%d items', 'noesia-commerce-suite' ), absint( $order['items_count'] ?? 0 ) ); ?> · <?php echo esc_html( $order['time_ago'] ?? '' ); ?></small>
								</div>
							</div>
							<div class="noesia-cs-commerce-order-row__meta">
								<strong><?php echo function_exists( 'wc_price' ) ? wp_kses_post( wc_price( (float) ( $order['total'] ?? 0 ) ) ) : esc_html( number_format_i18n( (float) ( $order['total'] ?? 0 ), 2 ) ); ?></strong>
								<span class="noesia-cs-commerce-status is-<?php echo esc_attr( sanitize_key( $order['status'] ?? '' ) ); ?>"><?php echo esc_html( $order['status_label'] ?? '' ); ?></span>
								<em class="<?php echo ! empty( $order['has_passport'] ) ? 'is-on' : 'is-off'; ?>"><?php echo ! empty( $order['has_passport'] ) ? esc_html__( 'Passport active', 'noesia-commerce-suite' ) : esc_html__( 'No passport yet', 'noesia-commerce-suite' ); ?></em>
							</div>
							<div class="noesia-cs-commerce-row-actions">
								<a href="<?php echo esc_url( $this->open_care_page_action_url( absint( $order['order_id'] ?? 0 ) ) ); ?>" target="_blank" rel="noopener noreferrer nofollow"><?php esc_html_e( 'Open Care', 'noesia-commerce-suite' ); ?></a>
								<?php if ( ! empty( $order['url'] ) ) : ?><a href="<?php echo esc_url( $order['url'] ); ?>"><?php esc_html_e( 'Woo Order', 'noesia-commerce-suite' ); ?> ↗</a><?php endif; ?>
							</div>
						</article>
					<?php endforeach; ?>
				<?php endif; ?>
			</div>
			<?php $this->render_commerce_pagination( 'orders', $current_page, $max_pages, array( 'noesia_cs_order_status' => $status ), 'noesia_cs_orders_page' ); ?>
		</section>
		<?php
	}

	/**
	 * Handles the related plugin operation.
	 */
	private function render_customers_view() {
		$page = isset( $_GET['noesia_cs_customers_page'] ) ? absint( wp_unslash( $_GET['noesia_cs_customers_page'] ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$data = $this->commerce_view_service->get_customers_view(
			array(
				'page'   => $page,
				'limit'  => 20,
			)
		);
		$customers     = isset( $data['customers'] ) && is_array( $data['customers'] ) ? $data['customers'] : array();
		$total         = absint( $data['total'] ?? 0 );
		$max_pages     = max( 1, absint( $data['max_num_pages'] ?? 1 ) );
		$current_page  = max( 1, absint( $data['current_page'] ?? 1 ) );
		?>
		<section class="noesia-cs-commerce-view noesia-cs-commerce-view--customers">
			<div class="noesia-cs-feature-hero noesia-cs-commerce-hero noesia-cs-commerce-hero--customers">
				<div>
					<span><?php esc_html_e( 'Operational View', 'noesia-commerce-suite' ); ?></span>
					<h2><?php esc_html_e( 'Customers View', 'noesia-commerce-suite' ); ?></h2>
					<p><?php esc_html_e( 'A focused customer intelligence view based on WooCommerce customer lookup data. It stays read-only and links back to native customer records where available.', 'noesia-commerce-suite' ); ?></p>
				</div>
				<a class="noesia-cs-native-link" href="<?php echo esc_url( admin_url( 'admin.php?page=wc-admin&path=/customers' ) ); ?>"><?php esc_html_e( 'Open native Woo Customers', 'noesia-commerce-suite' ); ?> ↗</a>
			</div>
			<div class="noesia-cs-commerce-toolbar noesia-cs-panel">
				<div><strong><?php echo esc_html( number_format_i18n( $total ) ); ?></strong><span><?php esc_html_e( 'customers with valid commerce orders', 'noesia-commerce-suite' ); ?></span></div>
				<p><?php esc_html_e( 'Noesia keeps this view read-only. Sensitive customer details stay in WooCommerce native screens.', 'noesia-commerce-suite' ); ?></p>
			</div>
			<div class="noesia-cs-commerce-list noesia-cs-panel">
				<?php if ( empty( $customers ) ) : ?>
					<p><?php echo esc_html( $data['note'] ?? __( 'No customer lookup data found yet.', 'noesia-commerce-suite' ) ); ?></p>
				<?php else : ?>
					<?php foreach ( $customers as $customer ) : ?>
						<article class="noesia-cs-commerce-customer-row">
							<div class="noesia-cs-commerce-customer-row__identity">
								<span class="noesia-cs-commerce-avatar"><?php echo esc_html( strtoupper( substr( (string) ( $customer['label'] ?? 'C' ), 0, 1 ) ) ); ?></span>
								<div><strong><?php echo esc_html( $customer['label'] ?? '' ); ?></strong><small><?php echo esc_html( $customer['location'] ?? '' ); ?></small></div>
							</div>
							<div class="noesia-cs-commerce-customer-row__stats">
								<span><b><?php echo esc_html( number_format_i18n( absint( $customer['orders_count'] ?? 0 ) ) ); ?></b><?php esc_html_e( 'Orders', 'noesia-commerce-suite' ); ?></span>
								<span><b><?php echo function_exists( 'wc_price' ) ? wp_kses_post( wc_price( (float) ( $customer['total_spend'] ?? 0 ) ) ) : esc_html( number_format_i18n( (float) ( $customer['total_spend'] ?? 0 ), 2 ) ); ?></b><?php esc_html_e( 'Spend', 'noesia-commerce-suite' ); ?></span>
								<span><b><?php echo ! empty( $customer['is_repeat'] ) ? esc_html__( 'Repeat', 'noesia-commerce-suite' ) : esc_html__( 'New', 'noesia-commerce-suite' ); ?></b><?php esc_html_e( 'Customer', 'noesia-commerce-suite' ); ?></span>
							</div>
							<div class="noesia-cs-commerce-row-actions">
								<?php if ( ! empty( $customer['url'] ) ) : ?><a href="<?php echo esc_url( $customer['url'] ); ?>"><?php esc_html_e( 'Open Native', 'noesia-commerce-suite' ); ?> ↗</a><?php endif; ?>
							</div>
						</article>
					<?php endforeach; ?>
				<?php endif; ?>
			</div>
			<?php $this->render_commerce_pagination( 'customers', $current_page, $max_pages, array(), 'noesia_cs_customers_page' ); ?>
		</section>
		<?php
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $view View.
	 * @param mixed $current_page Current page.
	 * @param mixed $max_pages Max pages.
	 * @param mixed $args Args.
	 * @param mixed $page_key Page key.
	 */
	private function render_commerce_pagination( $view, $current_page, $max_pages, $args = array(), $page_key = 'noesia_cs_page' ) {
		if ( $max_pages <= 1 ) {
			return;
		}
		$current_page  = max( 1, absint( $current_page ) );
		$max_pages     = max( 1, absint( $max_pages ) );
		?>
		<div class="noesia-cs-commerce-pagination">
			<?php if ( $current_page > 1 ) : ?>
				<a href="<?php echo esc_url( $this->commerce_view_url( $view, array_merge( $args, array( $page_key => $current_page - 1 ) ) ) ); ?>" data-noesia-cs-commerce-view-link="<?php echo esc_attr( $view ); ?>"><?php esc_html_e( 'Previous', 'noesia-commerce-suite' ); ?></a>
			<?php endif; ?>
			<span><?php printf( /* translators: 1: first numeric value; 2: second numeric value. */ esc_html__( 'Page %1$d of %2$d', 'noesia-commerce-suite' ), absint( $current_page ), absint( $max_pages ) ); ?></span>
			<?php if ( $current_page < $max_pages ) : ?>
				<a href="<?php echo esc_url( $this->commerce_view_url( $view, array_merge( $args, array( $page_key => $current_page + 1 ) ) ) ); ?>" data-noesia-cs-commerce-view-link="<?php echo esc_attr( $view ); ?>"><?php esc_html_e( 'Next', 'noesia-commerce-suite' ); ?></a>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $tab Tab.
	 * @param mixed $stats Stats.
	 * @param mixed $settings Settings.
	 */
	private function render_tab_view( $tab, $stats, $settings ) {
		$context = array(
			'passport_service'          => $this->passport_service,
			'analytics_service'         => $this->analytics_service,
			'doctor_service'            => $this->doctor_service,
			'impact_map_service'        => $this->impact_map_service,
			'email_service'             => $this->email_service,
			'reorder_service'           => $this->reorder_service,
			'data_service'              => $this->data_service,
			'feature_service'           => $this->feature_service,
			'system_status_service'     => $this->system_status_service,
			'care_action_service'       => $this->care_action_service,
			'settings_binding_service'  => $this->settings_binding_service,
			'stats'                     => $stats,
			'settings'                  => $settings,
			'admin'                     => $this,
			'tab'                       => $tab,
		);

		$rendered = apply_filters( 'noesia_cs_render_admin_tab', false, $tab, $context, $this );
		if ( true === $rendered ) {
			return;
		}

		if ( $this->module_registry && $this->module_registry->render( $tab, $context ) ) {
			return;
		}

		$this->empty_panel( __( 'Module view unavailable.', 'noesia-commerce-suite' ) );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $tab Tab.
	 */
	public function tab_url( $tab ) {
		return $this->renderer->tab_url( $tab );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $order_id Order id.
	 */
	public function printable_qr_url( $order_id ) {
		return $this->renderer->printable_qr_url( $order_id );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $message Message.
	 */
	public function empty_panel( $message ) {
		$this->renderer->empty_panel( $message );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $title Title.
	 * @param mixed $value Value.
	 * @param mixed $trend_icon Trend icon.
	 * @param mixed $trend Trend.
	 * @param mixed $icon Icon.
	 */
	public function metric_card( $title, $value, $trend_icon, $trend, $icon ) {
		$this->renderer->metric_card( $title, $value, $trend_icon, $trend, $icon );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $stats Stats.
	 * @param mixed $display_value Display value.
	 * @param mixed $range_label Range label.
	 */
	public function revenue_metric_card( $stats, $display_value, $range_label ) {
		$this->renderer->revenue_metric_card( $stats, $display_value, $range_label );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $skin Skin.
	 * @param mixed $title Title.
	 * @param mixed $text Text.
	 * @param mixed $value Value.
	 * @param mixed $label Label.
	 * @param mixed $tab Tab.
	 * @param mixed $prompt Prompt.
	 */
	public function module_panel( $skin, $title, $text, $value, $label, $tab, $prompt = '' ) {
		$this->renderer->module_panel( $skin, $title, $text, $value, $label, $tab, $prompt );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $title Title.
	 * @param mixed $description Description.
	 * @param mixed $feature Feature.
	 */
	public function pro_edition_card( $title, $description, $feature = '' ) {
		$this->renderer->pro_edition_card( $title, $description, $feature );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $series Series.
	 */
	public function render_revenue_svg( $series ) {
		$this->renderer->render_revenue_svg( $series );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $stats Stats.
	 */
	public function render_order_health( $stats ) {
		$this->renderer->render_order_health( $stats );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $summary Summary.
	 * @param mixed $range_label Range label.
	 */
	public function render_support_preview( $summary = array(), $range_label = '' ) {
		$this->renderer->render_support_preview( $summary, $range_label );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $items Items.
	 */
	public function render_top_products( $items ) {
		$this->renderer->render_top_products( $items );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $snapshot Snapshot.
	 * @param mixed $display_group_by Display group by.
	 */
	public function render_map_preview( $snapshot, $display_group_by = '' ) {
		$this->renderer->render_map_preview( $snapshot, $display_group_by );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $items Items.
	 * @param mixed $active_range Active range.
	 * @param mixed $tab Tab.
	 */
	public function render_dashboard_top_products( $items, $active_range = 'all', $tab = 'dashboard' ) {
		$this->renderer->render_dashboard_top_products( $items, $active_range, $tab );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $orders Orders.
	 */
	public function render_recent_orders_preview( $orders ) {
		$this->renderer->render_recent_orders_preview( $orders );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $stats Stats.
	 */
	public function render_dashboard_order_health( $stats ) {
		$this->renderer->render_dashboard_order_health( $stats );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $summary Summary.
	 * @param mixed $range_label Range label.
	 */
	public function render_customer_retention( $summary, $range_label = '' ) {
		$this->renderer->render_customer_retention( $summary, $range_label );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $snapshot Snapshot.
	 * @param mixed $display_group_by Display group by.
	 */
	public function render_impact_admin_table( $snapshot, $display_group_by = 'country' ) {
		$this->renderer->render_impact_admin_table( $snapshot, $display_group_by );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $snapshot Snapshot.
	 * @param mixed $display_group_by Display group by.
	 */
	public function prepare_impact_areas_for_display( $snapshot, $display_group_by = 'country' ) {
		return $this->renderer->prepare_impact_areas_for_display( $snapshot, $display_group_by );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $group_by Group by.
	 */
	public function impact_group_label( $group_by ) {
		return $this->renderer->impact_group_label( $group_by );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $payment_methods Payment methods.
	 * @param mixed $range_label Range label.
	 */
	public function render_payment_preview( $payment_methods, $range_label ) {
		$this->renderer->render_payment_preview( $payment_methods, $range_label );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $stats Stats.
	 */
	public function render_dashboard_date_filter( $stats ) {
		$this->renderer->render_dashboard_date_filter( $stats );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $forecast Forecast.
	 */
	public function render_weather_forecast( $forecast ) {
		$this->renderer->render_weather_forecast( $forecast );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $area Area.
	 * @param mixed $display_group_by Display group by.
	 */
	public function area_display_label( $area, $display_group_by = 'country' ) {
		return $this->renderer->area_display_label( $area, $display_group_by );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $country_code Country code.
	 */
	public function country_flag( $country_code ) {
		return $this->renderer->country_flag( $country_code );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $report Report.
	 */
	public function render_doctor_preview( $report ) {
		$this->renderer->render_doctor_preview( $report );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $label Label.
	 */
	public function safe_customer_label( $label ) {
		return $this->renderer->safe_customer_label( $label );
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function render_system_status_panel() {
		$this->renderer->render_system_status_panel();
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function render_upgrade_summary() {
		$this->renderer->render_upgrade_summary();
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function render_settings_binding_panel() {
		$this->renderer->render_settings_binding_panel();
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function render_service_boundary_panel() {
		$this->renderer->render_service_boundary_panel();
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $context Context.
	 */
	public function settings_sections( $context = 'all' ) {
		$sections = $this->renderer->settings_sections( $context );

		return apply_filters( 'noesia_cs_settings_sections', $sections, $context, $this );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $skin Skin.
	 * @param mixed $title Title.
	 * @param mixed $description Description.
	 * @param mixed $fields Fields.
	 * @param mixed $settings Settings.
	 * @param mixed $tab Tab.
	 */
	public function render_settings_group( $skin, $title, $description, $fields, $settings, $tab = '' ) {
		$this->renderer->render_settings_group( $skin, $title, $description, $fields, $settings, $tab );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $title Title.
	 * @param mixed $fields Fields.
	 * @param mixed $settings Settings.
	 * @param mixed $description Description.
	 * @param mixed $section_id Section id.
	 */
	public function settings_card( $title, $fields, $settings, $description = '', $section_id = '' ) {
		$this->renderer->settings_card( $title, $fields, $settings, $description, $section_id );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $key Key.
	 * @param mixed $value Value.
	 */
	public function render_field( $key, $value ) {
		$this->renderer->render_field( $key, $value );
	}

	/**
	 * Handles the related plugin operation.
	 */
	public function field_labels() {
		return $this->renderer->field_labels();
	}

	/**
	 * Handles the related plugin operation.
	 */
	private function get_latest_order_summary() {
		return $this->renderer->get_latest_order_summary();
	}

	/**
	 * Handles the related plugin operation.
	 */
	private function get_last_hour_order_count() {
		return $this->renderer->get_last_hour_order_count();
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $tab Tab.
	 */
	private function icon_for_tab( $tab ) {
		return $this->renderer->icon_for_tab( $tab );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $tab Tab.
	 */
	private function headline_for_tab( $tab ) {
		return $this->renderer->headline_for_tab( $tab );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $tab Tab.
	 */
	private function subtitle_for_tab( $tab ) {
		return $this->renderer->subtitle_for_tab( $tab );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $notice Notice.
	 */
	private function notice_text( $notice ) {
		return $this->renderer->notice_text( $notice );
	}


	/**
	 * Render the free shell calculator launcher.
	 */
	private function render_calculator_launcher() {
		?>
		<button type="button" class="noesia-cs-calculator-launcher" data-noesia-cs-calculator-open aria-label="<?php esc_attr_e( 'Open calculator', 'noesia-commerce-suite' ); ?>" title="<?php esc_attr_e( 'Open calculator', 'noesia-commerce-suite' ); ?>">
			<span class="noesia-cs-calculator-launcher__icon" aria-hidden="true">
				<svg viewBox="0 0 24 24" focusable="false" role="img" xmlns="http://www.w3.org/2000/svg">
					<rect x="5" y="3" width="14" height="18" rx="3"></rect>
					<path d="M8 7h8M8 11h2M12 11h2M16 11h0M8 15h2M12 15h2M16 15h0M8 18h2M12 18h4"></path>
				</svg>
			</span>
		</button>
		<?php
	}

	/**
	 * Render the free calculator workspace and template.
	 */
	private function render_calculator_workspace() {
		?>
		<div class="noesia-cs-calculator-layer" data-noesia-cs-calculator-layer aria-live="polite"></div>
		<div class="noesia-cs-calculator-minibar" data-noesia-cs-calculator-minibar aria-live="polite"></div>
		<template id="noesia-cs-calculator-template">
			<section class="noesia-cs-calculator-window" data-noesia-cs-calculator-window role="dialog" aria-modal="false" aria-label="<?php esc_attr_e( 'Noesia calculator', 'noesia-commerce-suite' ); ?>">
				<header class="noesia-cs-calculator-window__header" data-noesia-cs-calculator-drag-handle>
					<div>
						<strong><?php esc_html_e( 'Business Calculator', 'noesia-commerce-suite' ); ?></strong>
						<span><?php esc_html_e( 'Free shell utility', 'noesia-commerce-suite' ); ?></span>
					</div>
					<div class="noesia-cs-calculator-window__actions">
						<button type="button" data-noesia-cs-calculator-tape-toggle aria-label="<?php esc_attr_e( 'Toggle calculation tape', 'noesia-commerce-suite' ); ?>">≡</button>
						<button type="button" data-noesia-cs-calculator-duplicate aria-label="<?php esc_attr_e( 'Duplicate calculator', 'noesia-commerce-suite' ); ?>">⧉</button>
						<button type="button" data-noesia-cs-calculator-minimize aria-label="<?php esc_attr_e( 'Minimize calculator', 'noesia-commerce-suite' ); ?>">–</button>
						<button type="button" data-noesia-cs-calculator-close aria-label="<?php esc_attr_e( 'Close calculator', 'noesia-commerce-suite' ); ?>">×</button>
					</div>
				</header>
				<div class="noesia-cs-calculator-window__body">
					<div class="noesia-cs-calculator-main">
						<div class="noesia-cs-calculator-display">
							<span data-noesia-cs-calculator-expression>0</span>
							<strong data-noesia-cs-calculator-value>0</strong>
						</div>
						<div class="noesia-cs-calculator-memory" aria-label="<?php esc_attr_e( 'Business memory slots', 'noesia-commerce-suite' ); ?>">
							<button type="button" data-noesia-cs-calculator-memory-save="cost"><?php esc_html_e( 'Save Cost', 'noesia-commerce-suite' ); ?></button>
							<button type="button" data-noesia-cs-calculator-memory-use="cost"><span><?php esc_html_e( 'Cost', 'noesia-commerce-suite' ); ?></span><strong data-noesia-cs-calculator-memory-value="cost">0</strong></button>
							<button type="button" data-noesia-cs-calculator-memory-save="margin"><?php esc_html_e( 'Save Margin', 'noesia-commerce-suite' ); ?></button>
							<button type="button" data-noesia-cs-calculator-memory-use="margin"><span><?php esc_html_e( 'Margin', 'noesia-commerce-suite' ); ?></span><strong data-noesia-cs-calculator-memory-value="margin">0</strong></button>
						</div>
						<div class="noesia-cs-calculator-grid">
							<button type="button" data-noesia-cs-calculator-clear><?php esc_html_e( 'AC', 'noesia-commerce-suite' ); ?></button>
							<button type="button" data-noesia-cs-calculator-backspace>⌫</button>
							<button type="button" data-noesia-cs-calculator-input="%">%</button>
							<button type="button" data-noesia-cs-calculator-input="/">÷</button>
							<button type="button" data-noesia-cs-calculator-input="7">7</button>
							<button type="button" data-noesia-cs-calculator-input="8">8</button>
							<button type="button" data-noesia-cs-calculator-input="9">9</button>
							<button type="button" data-noesia-cs-calculator-input="*">×</button>
							<button type="button" data-noesia-cs-calculator-input="4">4</button>
							<button type="button" data-noesia-cs-calculator-input="5">5</button>
							<button type="button" data-noesia-cs-calculator-input="6">6</button>
							<button type="button" data-noesia-cs-calculator-input="-">−</button>
							<button type="button" data-noesia-cs-calculator-input="1">1</button>
							<button type="button" data-noesia-cs-calculator-input="2">2</button>
							<button type="button" data-noesia-cs-calculator-input="3">3</button>
							<button type="button" data-noesia-cs-calculator-input="+">+</button>
							<button type="button" data-noesia-cs-calculator-input="0" class="is-wide">0</button>
							<button type="button" data-noesia-cs-calculator-input=".">.</button>
							<button type="button" data-noesia-cs-calculator-equals>=</button>
						</div>
					</div>
					<aside class="noesia-cs-calculator-tape" data-noesia-cs-calculator-tape>
						<div><strong><?php esc_html_e( 'Calculation Tape', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Last 10 results', 'noesia-commerce-suite' ); ?></span></div>
						<ol data-noesia-cs-calculator-history></ol>
					</aside>
				</div>
			</section>
		</template>
		<?php
	}

}
