<?php
/**
 * Admin menu registration.
 *
 * @package SubmissionInbox
 */

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

/**
 * Registers the InboxMend admin area.
 */
class SUBIN_AdminMenu {
	const CAPABILITY = 'manage_options';
	const MENU_SLUG  = 'subin-inbox';

	/**
	 * Settings repository.
	 *
	 * @var SUBIN_SettingsRepository
	 */
	private $settings;

	/**
	 * Form submission context.
	 *
	 * @var \SUBIN\Forms\FormSubmissionContext|null
	 */
	private $form_context;

	/**
	 * Menu hook suffixes and the exact capability required by each hook.
	 *
	 * @var array<string,string>
	 */
	private $hooks = array();

	/**
	 * Constructor.
	 *
	 * @param SUBIN_SettingsRepository                 $settings Settings repository.
	 * @param \SUBIN\Forms\FormSubmissionContext|null $form_context Form context.
	 */
	public function __construct( SUBIN_SettingsRepository $settings, $form_context = null ) {
		$this->settings     = $settings;
		$this->form_context = $form_context;
	}

	/**
	 * Register admin hooks.
	 *
	 * @return void
	 */
	public function register() {
		add_action( 'admin_menu', array( $this, 'register_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'admin_head', array( $this, 'hide_hidden_submenu_items' ) );
		add_filter( 'parent_file', array( $this, 'filter_parent_file' ) );
		add_filter( 'submenu_file', array( $this, 'filter_submenu_file' ) );
	}

	/**
	 * Register menu pages.
	 *
	 * @return void
	 */
	public function register_menu() {
		$inbox_capability = \SUBIN\Security\Capabilities::effective_capability(
			\SUBIN\Security\Capabilities::VIEW_LEADS,
			true
		);
		$email_health_capability = \SUBIN\Security\Capabilities::effective_capability(
			\SUBIN\Security\Capabilities::VIEW_EMAIL_HEALTH,
			true
		);
		$email_log_capability = \SUBIN\Security\Capabilities::effective_capability(
			\SUBIN\Security\Capabilities::VIEW_EMAIL_LOG,
			true
		);
		$manage_settings_capability = \SUBIN\Security\Capabilities::effective_capability(
			\SUBIN\Security\Capabilities::MANAGE_SETTINGS,
			true
		);
		$manage_sending_capability = \SUBIN\Security\Capabilities::effective_capability(
			\SUBIN\Security\Capabilities::MANAGE_SENDING,
			true
		);
		$manage_cloud_capability = \SUBIN\Security\Capabilities::effective_capability(
			\SUBIN\Security\Capabilities::MANAGE_CLOUD,
			true
		);
		$settings_route_capability = $manage_settings_capability;
		if ( '' === $settings_route_capability ) {
			$settings_route_capability = '' !== $manage_sending_capability ? $manage_sending_capability : $manage_cloud_capability;
		}
		$root_capability = $inbox_capability;
		if ( '' === $root_capability ) {
			$root_capability = '' !== $email_health_capability ? $email_health_capability : ( '' !== $email_log_capability ? $email_log_capability : $settings_route_capability );
		}

		if ( '' === $root_capability ) {
			return;
		}

		$inbox_page = new SUBIN_InboxPage( $this->form_context );

		$inbox_hook = add_menu_page(
			__( 'InboxMend', 'submission-inbox' ),
			__( 'InboxMend', 'submission-inbox' ),
			$root_capability,
			self::MENU_SLUG,
			array( $inbox_page, 'render' ),
			'dashicons-email-alt2',
			56
		);

		if ( $inbox_hook ) {
			$this->track_hook( $inbox_hook, $root_capability );
			if ( '' !== $inbox_capability ) {
				add_action( 'load-' . $inbox_hook, array( $inbox_page, 'handle_request' ) );
				add_action( 'load-' . $inbox_hook, array( $inbox_page, 'handle_export_request' ) );
			} else {
				add_action( 'load-' . $inbox_hook, array( $this, 'redirect_to_accessible_email_page' ) );
			}
		}

		if ( '' !== $inbox_capability ) {
			$inbox_submenu_hook = add_submenu_page(
				self::MENU_SLUG,
				__( 'Inbox', 'submission-inbox' ),
				__( 'Inbox', 'submission-inbox' ),
				$inbox_capability,
				self::MENU_SLUG,
				array( $inbox_page, 'render' )
			);

			if ( $inbox_submenu_hook ) {
				$this->track_hook( $inbox_submenu_hook, $inbox_capability );
				if ( $inbox_submenu_hook !== $inbox_hook ) {
					add_action( 'load-' . $inbox_submenu_hook, array( $inbox_page, 'handle_request' ) );
					add_action( 'load-' . $inbox_submenu_hook, array( $inbox_page, 'handle_export_request' ) );
				}
			}
		}

		if ( '' !== $email_health_capability ) {
			$email_status_page = new SUBIN_EmailStatusPage();
			$email_health_title = $this->email_health_menu_title();
			$email_status_hook = add_submenu_page(
				self::MENU_SLUG,
				__( 'Email Health', 'submission-inbox' ),
				$email_health_title,
				$email_health_capability,
				'subin-email-status',
				array( $email_status_page, 'render' )
			);

			if ( $email_status_hook ) {
				$this->track_hook( $email_status_hook, $email_health_capability );
				add_action( 'load-' . $email_status_hook, array( $email_status_page, 'handle_request' ) );
			}
		}

		$email_log_capability = \SUBIN\Security\Capabilities::effective_capability(
			\SUBIN\Security\Capabilities::VIEW_EMAIL_LOG,
			true
		);
		if ( '' !== $email_log_capability ) {
			$email_log_page = new SUBIN_EmailLogPage();
			$email_log_hook = add_submenu_page(
				self::MENU_SLUG,
				__( 'Email Log', 'submission-inbox' ),
				__( 'Email Log', 'submission-inbox' ),
				$email_log_capability,
				'subin-email-log',
				array( $email_log_page, 'render' )
			);

			if ( $email_log_hook ) {
				$this->track_hook( $email_log_hook, $email_log_capability );
				add_action( 'load-' . $email_log_hook, array( $email_log_page, 'handle_request' ) );
				add_action( 'load-' . $email_log_hook, array( $email_log_page, 'handle_export_request' ) );
			}
		}

		if ( '' !== $manage_sending_capability ) {
			$smtp_settings_page = new SUBIN_SmtpSettingsPage( $this->settings );
			$smtp_settings_hook = add_submenu_page(
				'',
				__( 'SMTP Settings', 'submission-inbox' ),
				__( 'SMTP Settings', 'submission-inbox' ),
				$manage_sending_capability,
				'subin-smtp-settings',
				array( $smtp_settings_page, 'render' )
			);

			if ( $smtp_settings_hook ) {
				$this->track_hook( $smtp_settings_hook, $manage_sending_capability );
				add_action( 'load-' . $smtp_settings_hook, array( $this, 'load_smtp_settings_page' ) );
			}
		}

		if ( current_user_can( self::CAPABILITY ) ) {
			$forms_page        = new SUBIN_FormsPage();
			$auto_replies_page = new SUBIN_AutoRepliesPage();

			$forms_hook = add_submenu_page(
				self::MENU_SLUG,
				__( 'Forms', 'submission-inbox' ),
				__( 'Forms', 'submission-inbox' ),
				self::CAPABILITY,
				'submission-inbox-forms',
				array( $forms_page, 'render' )
			);

			if ( $forms_hook ) {
				$this->track_hook( $forms_hook, self::CAPABILITY );
				add_action( 'load-' . $forms_hook, array( $forms_page, 'handle_request' ) );
			}

			$auto_replies_hook = add_submenu_page(
				self::MENU_SLUG,
				__( 'Auto-replies', 'submission-inbox' ),
				__( 'Auto-replies', 'submission-inbox' ),
				self::CAPABILITY,
				'submission-inbox-auto-replies',
				array( $auto_replies_page, 'render' )
			);

			if ( $auto_replies_hook ) {
				$this->track_hook( $auto_replies_hook, self::CAPABILITY );
				add_action( 'load-' . $auto_replies_hook, array( $auto_replies_page, 'handle_request' ) );
				add_action( 'load-' . $auto_replies_hook, array( $this, 'load_auto_replies_page' ) );
			}
		}

		if ( '' !== $settings_route_capability ) {
			$settings_page = new SUBIN_SettingsPage( $this->settings );
			$settings_hook = add_submenu_page(
				self::MENU_SLUG,
				__( 'Settings', 'submission-inbox' ),
				__( 'Settings', 'submission-inbox' ),
				$settings_route_capability,
				'subin-settings',
				array( $settings_page, 'render' )
			);

			if ( $settings_hook ) {
				$this->track_hook( $settings_hook, $settings_route_capability );
				add_action( 'load-' . $settings_hook, array( $settings_page, 'handle_request' ) );
			}
		}
	}

	/**
	 * Route a user without Inbox access to the first plugin page they can use.
	 *
	 * The top-level WordPress menu needs one callable parent route. Keeping this
	 * redirect capability-aware lets VIEW_EMAIL_LOG and VIEW_EMAIL_HEALTH work
	 * independently instead of secretly depending on VIEW_LEADS.
	 *
	 * @return void
	 */
	public function redirect_to_accessible_email_page() {
		if ( \SUBIN\Security\Capabilities::can_current_user( \SUBIN\Security\Capabilities::VIEW_LEADS ) ) {
			return;
		}

		$routes = array(
			\SUBIN\Security\Capabilities::VIEW_EMAIL_HEALTH => 'subin-email-status',
			\SUBIN\Security\Capabilities::VIEW_EMAIL_LOG    => 'subin-email-log',
			\SUBIN\Security\Capabilities::MANAGE_SETTINGS   => 'subin-settings',
			\SUBIN\Security\Capabilities::MANAGE_SENDING    => 'subin-settings',
			\SUBIN\Security\Capabilities::MANAGE_CLOUD      => 'subin-settings',
		);
		foreach ( $routes as $capability => $page ) {
			if ( \SUBIN\Security\Capabilities::can_current_user( $capability ) ) {
				wp_safe_redirect( admin_url( 'admin.php?page=' . $page ) );
				exit;
			}
		}

		wp_die( esc_html__( 'You do not have permission to perform this action.', 'submission-inbox' ) );
	}

	/** Build a cached menu signal without exposing log details. */
	private function email_health_menu_title() {
		$label = __( 'Email Health', 'submission-inbox' );
		if ( ! class_exists( '\SUBIN\Mail\EmailHealthService' ) ) {
			return $label;
		}

		try {
			$signal = ( new \SUBIN\Mail\EmailHealthService() )->current_signal();
		} catch ( \Throwable $throwable ) {
			unset( $throwable );
			return $label;
		}
		$count = absint( $signal['active_issue_count'] ?? 0 );
		if ( $count < 1 ) {
			return $label;
		}

		$is_lower_bound = ! empty( $signal['count_is_lower_bound'] );
		if ( $is_lower_bound ) {
			/* translators: %d: minimum number of distinct active email issues requiring action. */
			$accessible_template = _n( 'At least %d active email issue', 'At least %d active email issues', $count, 'submission-inbox' );
		} else {
			/* translators: %d: number of distinct active email issues requiring action. */
			$accessible_template = _n( '%d active email issue', '%d active email issues', $count, 'submission-inbox' );
		}
		$accessible = sprintf( $accessible_template, $count );
		$visible = $count > 99 ? '99+' : (string) $count . ( $is_lower_bound ? '+' : '' );

		return $label . ' <span class="awaiting-mod count-' . esc_attr( (string) $count ) . '"><span class="plugin-count" aria-hidden="true">' . esc_html( $visible ) . '</span><span class="screen-reader-text">' . esc_html( $accessible ) . '</span></span>';
	}

	/**
	 * Record the access contract for a registered admin hook.
	 *
	 * @param string $hook_suffix Registered WordPress admin hook suffix.
	 * @param string $capability  Exact capability passed to the menu API.
	 * @return void
	 */
	private function track_hook( $hook_suffix, $capability ) {
		$hook_suffix = (string) $hook_suffix;
		$capability  = sanitize_key( (string) $capability );
		if ( '' === $hook_suffix || '' === $capability ) {
			return;
		}

		$this->hooks[ $hook_suffix ] = $capability;
	}

	/**
	 * Hide routed utility pages while keeping them registered for access checks.
	 *
	 * @return void
	 */
	public function hide_hidden_submenu_items() {
	}

	/**
	 * Legacy no-op retained for backward compatibility.
	 *
	 * @param WP_Screen|null $screen Unused current admin screen.
	 * @return void
	 */
	public function suppress_third_party_notices( $screen = null ) {
	}

	/**
	 * Enqueue admin assets on plugin pages.
	 *
	 * @param string $hook_suffix Current admin hook suffix.
	 * @return void
	 */
	public function enqueue_assets( $hook_suffix ) {
		$required_capability = isset( $this->hooks[ $hook_suffix ] ) ? $this->hooks[ $hook_suffix ] : '';
		if ( '' === $required_capability || ! current_user_can( $required_capability ) ) {
			return;
		}

		$is_design_studio      = $this->is_design_studio_editor_request();
		$is_inbox_page         = $this->is_inbox_page_request();
		$is_email_page         = $this->is_email_page_request();
		$is_email_log_page     = $this->is_email_log_page_request();
		$is_settings_page        = $this->is_settings_page_request();
		$is_forms_page           = $this->is_forms_page_request();
		$forms_view              = $is_forms_page ? SUBIN_Request::get_key( 'view' ) : '';
		$is_forms_command_center = $is_forms_page && ! in_array( $forms_view, array( 'templates', 'template-preview', 'edit', 'preview' ), true );
		$is_auto_reply_page      = $this->is_auto_reply_page_request();
		$is_auto_reply_editor  = $this->is_auto_reply_editor_request();
		$is_auto_reply_rule_editor = $this->is_auto_reply_rule_editor_request();
		if ( $is_design_studio ) {
			wp_enqueue_media();
			wp_enqueue_editor();
		}

		if ( $is_auto_reply_editor || $is_auto_reply_rule_editor ) {
			wp_enqueue_media();
			wp_enqueue_editor();
		}

		if ( $is_design_studio ) {
			wp_enqueue_style(
				'subin-grapesjs',
				SUBIN_PLUGIN_URL . 'assets/vendor/grapesjs/grapes.min.css',
				array(),
				'0.22.16'
			);

			wp_enqueue_script(
				'subin-grapesjs',
				SUBIN_PLUGIN_URL . 'assets/vendor/grapesjs/grapes.min.js',
				array(),
				'0.22.16',
				true
			);
		}

		if ( $is_forms_page ) {
			wp_enqueue_style(
				'subin-frontend',
				SUBIN_PLUGIN_URL . 'assets/frontend.css',
				array(),
				SUBIN_PLUGIN_VERSION
			);
		}

		wp_enqueue_style(
			'subin-admin',
			SUBIN_PLUGIN_URL . 'assets/css/admin.css',
			$is_forms_page ? array( 'subin-frontend' ) : array(),
			(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/css/admin.css' )
		);

		if ( $is_inbox_page ) {
			wp_enqueue_style(
				'subin-admin-inbox',
				SUBIN_PLUGIN_URL . 'assets/css/admin/inbox.css',
				array( 'subin-admin' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/css/admin/inbox.css' )
			);
		}

		if ( $is_forms_page ) {
			wp_enqueue_style(
				'subin-admin-forms',
				SUBIN_PLUGIN_URL . 'assets/css/admin/forms.css',
				array( 'subin-admin' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/css/admin/forms.css' )
			);
		}

		if ( $is_forms_command_center ) {
			wp_enqueue_style(
				'subin-admin-forms-command-center',
				SUBIN_PLUGIN_URL . 'assets/css/admin/forms-command-center.css',
				array( 'subin-admin-forms' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/css/admin/forms-command-center.css' )
			);
		}

		if ( $is_email_log_page && file_exists( SUBIN_PLUGIN_DIR . 'assets/js/email-log.js' ) ) {
			wp_enqueue_script(
				'subin-email-log',
				SUBIN_PLUGIN_URL . 'assets/js/email-log.js',
				array( 'subin-admin' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/js/email-log.js' ),
				true
			);
		}

		if ( $is_settings_page || $is_forms_page || $is_auto_reply_page ) {
			wp_enqueue_style(
				'subin-admin-settings-controls',
				SUBIN_PLUGIN_URL . 'assets/css/admin/settings-controls.css',
				array( 'subin-admin' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/css/admin/settings-controls.css' )
			);
		}

		if ( $is_settings_page ) {
			wp_enqueue_style(
				'subin-admin-settings',
				SUBIN_PLUGIN_URL . 'assets/css/admin/settings.css',
				array( 'subin-admin', 'subin-admin-settings-controls' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/css/admin/settings.css' )
			);
		}

		if ( $is_email_page ) {
			wp_enqueue_style(
				'subin-admin-email',
				SUBIN_PLUGIN_URL . 'assets/css/admin/email.css',
				array( 'subin-admin' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/css/admin/email.css' )
			);
		}

		if ( $is_design_studio ) {
			wp_enqueue_style(
				'subin-design-studio-css',
				SUBIN_PLUGIN_URL . 'assets/css/design-studio/design-studio.css',
				array( 'subin-grapesjs', 'subin-admin' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/css/design-studio/design-studio.css' )
			);
		}

		if ( $is_auto_reply_page ) {
			wp_enqueue_style(
				'subin-auto-reply-editor-css',
				SUBIN_PLUGIN_URL . 'assets/css/auto-reply/auto-reply.css',
				array( 'subin-admin' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/css/auto-reply/auto-reply.css' )
			);
		}

		wp_enqueue_script(
			'subin-admin',
			SUBIN_PLUGIN_URL . 'assets/js/admin.js',
			array(),
			(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/js/admin.js' ),
			true
		);

		if ( $is_forms_command_center ) {
			wp_enqueue_script(
				'subin-forms-command-center',
				SUBIN_PLUGIN_URL . 'assets/js/forms-command-center.js',
				array( 'subin-admin' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/js/forms-command-center.js' ),
				true
			);
		}

		if ( $is_design_studio ) {
			wp_enqueue_script(
				'subin-conditional-logic',
				SUBIN_PLUGIN_URL . 'assets/js/conditional-logic.js',
				array(),
				(string) filemtime( SUBIN_PLUGIN_DIR . 'assets/js/conditional-logic.js' ),
				true
			);

			$design_studio_deps = array( 'subin-grapesjs', 'jquery', 'media-editor', 'media-views', 'wp-util', 'subin-conditional-logic' );
			$design_studio_path = 'assets/js/design-studio/';
			$design_studio_files = array(
				'subin-design-studio-components' => 'design-studio-components.js',
				'subin-design-studio-traits'     => 'design-studio-traits.js',
				'subin-design-studio-rendering'  => 'design-studio-rendering.js',
				'subin-design-studio-inspector'  => 'design-studio-inspector.js',
				'subin-design-studio-data'       => 'design-studio-data.js',
				'subin-design-studio-logic'      => 'design-studio-logic.js',
				'subin-design-studio-publishing' => 'design-studio-publishing.js',
			);

			foreach ( $design_studio_files as $handle => $file ) {
				wp_enqueue_script(
					$handle,
					SUBIN_PLUGIN_URL . $design_studio_path . $file,
					$design_studio_deps,
					(string) filemtime( SUBIN_PLUGIN_DIR . $design_studio_path . $file ),
					true
				);
				$design_studio_deps = array( $handle );
			}

			wp_enqueue_script(
				'subin-design-studio',
				SUBIN_PLUGIN_URL . $design_studio_path . 'design-studio.js',
				$design_studio_deps,
				(string) filemtime( SUBIN_PLUGIN_DIR . $design_studio_path . 'design-studio.js' ),
				true
			);
		}

		if ( $is_auto_reply_editor ) {
			$auto_reply_path = 'assets/js/auto-reply/';
			wp_enqueue_script(
				'subin-auto-reply-wp-editor',
				SUBIN_PLUGIN_URL . $auto_reply_path . 'auto-reply-wp-editor.js',
				array( 'jquery' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . $auto_reply_path . 'auto-reply-wp-editor.js' ),
				true
			);
		}

		if ( $is_auto_reply_rule_editor ) {
			$auto_reply_path = 'assets/js/auto-reply/';
			wp_enqueue_script(
				'subin-auto-reply-rules',
				SUBIN_PLUGIN_URL . $auto_reply_path . 'auto-reply-rules.js',
				array( 'subin-admin' ),
				(string) filemtime( SUBIN_PLUGIN_DIR . $auto_reply_path . 'auto-reply-rules.js' ),
				true
			);
			wp_localize_script(
				'subin-auto-reply-rules',
				'subinAutoReplyRules',
				array(
					'ajaxUrl' => admin_url( 'admin-ajax.php' ),
					'nonce'   => wp_create_nonce( 'subin_auto_reply_api' ),
					'urls'    => array(
						'emailLog' => admin_url( 'admin.php?page=subin-email-log' ),
					),
					'actions' => array(
						'initialState' => 'subin_auto_reply_initial_state',
						'listDesigns'  => 'subin_auto_reply_list_designs',
						'loadDesign'   => 'subin_auto_reply_load_design',
						'save'         => 'subin_auto_reply_save_rule',
						'preview'      => 'subin_auto_reply_preview',
						'simulate'     => 'subin_auto_reply_simulate',
						'testSend'     => 'subin_auto_reply_test_send',
					),
					'strings' => array(
						'requestFailed'  => __( 'The auto-reply request could not be completed. Reload the page and try again.', 'submission-inbox' ),
						'invalidResponse' => __( 'The server returned an invalid auto-reply response.', 'submission-inbox' ),
						'loading'        => __( 'Loading…', 'submission-inbox' ),
						'loadingDesign'  => __( 'Loading design…', 'submission-inbox' ),
						'loadingDesigns' => __( 'Loading saved designs…', 'submission-inbox' ),
						'designInserted' => __( 'Design content inserted. Subject and preview text were kept.', 'submission-inbox' ),
						'replaceMessage' => __( 'Replace the current email body with this design?', 'submission-inbox' ),
						'savedDesign'    => __( 'Saved', 'submission-inbox' ),
						'savedDesignDescription' => __( 'Reusable email layout and content', 'submission-inbox' ),
						'insertDesign'   => __( 'Insert and customize', 'submission-inbox' ),
						'saving'         => __( 'Saving…', 'submission-inbox' ),
						'saved'          => __( 'Rule saved.', 'submission-inbox' ),
						'savedWithNewerChanges' => __( 'Saved. You still have newer unsaved changes.', 'submission-inbox' ),
						'unsaved'        => __( 'You have unsaved changes.', 'submission-inbox' ),
						'previewing'     => __( 'Generating preview…', 'submission-inbox' ),
						'simulating'     => __( 'Running safe simulation…', 'submission-inbox' ),
						'sending'        => __( 'Sending test…', 'submission-inbox' ),
						'always'         => __( 'Always', 'submission-inbox' ),
						'chooseForm'     => __( 'Select form', 'submission-inbox' ),
						'chooseEmail'    => __( 'Select visitor email field', 'submission-inbox' ),
						'chooseField'    => __( 'Choose field', 'submission-inbox' ),
						'value'          => __( 'Value', 'submission-inbox' ),
						'matched'        => __( 'Rule matched the sample submission.', 'submission-inbox' ),
						'notMatched'     => __( 'Rule did not match the sample submission.', 'submission-inbox' ),
						'fieldType'      => __( 'Field type', 'submission-inbox' ),
						'operator'       => __( 'Operator', 'submission-inbox' ),
						'removeCondition' => __( 'Remove condition', 'submission-inbox' ),
						'and'            => __( 'and', 'submission-inbox' ),
						'checked'        => __( 'Checked', 'submission-inbox' ),
						'notChecked'     => __( 'Not checked', 'submission-inbox' ),
						'sampleChooseForm' => __( 'Choose a form to add sample values.', 'submission-inbox' ),
						'previewTitle'   => __( 'Auto-reply preview', 'submission-inbox' ),
						'previewReady'   => __( 'Preview ready. No merge-tag warnings.', 'submission-inbox' ),
						'previewWarning' => __( 'Preview warning', 'submission-inbox' ),
						'previewSelect'  => __( 'Select a form and visitor email field to preview.', 'submission-inbox' ),
						'testSent'       => __( 'Test email sent.', 'submission-inbox' ),
						'openEmailLog'   => __( 'Open Email Log', 'submission-inbox' ),
						'publishedEnabled' => __( 'Published · enabled', 'submission-inbox' ),
						'publishedDisabled' => __( 'Published · disabled', 'submission-inbox' ),
						'draft'          => __( 'Draft', 'submission-inbox' ),
						'publishedRule'  => __( 'Published rule', 'submission-inbox' ),
						'draftRule'      => __( 'Draft rule', 'submission-inbox' ),
						'saveChanges'    => __( 'Save changes', 'submission-inbox' ),
						'publishEnable'  => __( 'Publish & enable', 'submission-inbox' ),
					),
				)
			);
		}
	}

	/**
	 * Keep hidden auto-reply routes under the InboxMend menu.
	 *
	 * @param string $parent_file Current parent file.
	 * @return string
	 */
	public function filter_parent_file( $parent_file ) {
		$page = SUBIN_Request::get_key( 'page' );
		if ( 'subin-smtp-settings' === $page ) {
			return self::MENU_SLUG;
		}

		if ( $this->is_auto_reply_page_request() ) {
			return self::MENU_SLUG;
		}

		return $parent_file;
	}

	/**
	 * Highlight the current InboxMend submenu item for routed pages.
	 *
	 * @param string $submenu_file Current submenu file.
	 * @return string
	 */
	public function filter_submenu_file( $submenu_file ) {
		if ( $this->is_auto_reply_page_request() ) {
			return 'submission-inbox-auto-replies';
		}

		$page = SUBIN_Request::get_key( 'page' );
		if ( 'subin-smtp-settings' === $page ) {
			return 'subin-settings';
		}

		return $submenu_file;
	}

	/**
	 * Set a title for the hidden auto-reply route before admin-header.php renders.
	 *
	 * @return void
	 */
	public function load_auto_replies_page() {
		global $title;

		$title = __( 'Auto-replies', 'submission-inbox' );
	}

	/**
	 * Redirect the legacy SMTP settings route before the admin header renders.
	 *
	 * @return void
	 */
	public function load_smtp_settings_page() {
		if ( ! \SUBIN\Security\Capabilities::can_current_user( \SUBIN\Security\Capabilities::MANAGE_SENDING ) ) {
			wp_die( esc_html__( 'You do not have permission to perform this action.', 'submission-inbox' ) );
		}

		wp_safe_redirect( admin_url( 'admin.php?page=subin-settings&subin_settings_tab=sending' ) );
		exit;
	}

	/**
	 * Check whether the current admin request is the form Design Studio editor.
	 *
	 * @return bool
	 */
	private function is_design_studio_editor_request() {
		$page    = SUBIN_Request::get_key( 'page' );
		$view    = SUBIN_Request::get_key( 'view' );
		$tab     = SUBIN_Request::get_key( 'tab' );
		$form_id = SUBIN_Request::get_absint( 'form_id' );

		return 'submission-inbox-forms' === $page && 'edit' === $view && $form_id > 0 && ( '' === $tab || 'design_studio' === $tab );
	}

	/**
	 * Check if the current request is any Forms admin page.
	 *
	 * @return bool
	 */
	private function is_forms_page_request() {
		$page = SUBIN_Request::get_key( 'page' );

		return 'submission-inbox-forms' === $page;
	}

	/**
	 * Check if the current request is any Inbox admin page.
	 *
	 * @return bool
	 */
	private function is_inbox_page_request() {
		$page = SUBIN_Request::get_key( 'page' );

		return self::MENU_SLUG === $page;
	}

	/**
	 * Check if the current request is an Email Health or Email Log page.
	 *
	 * @return bool
	 */
	private function is_email_page_request() {
		$page = SUBIN_Request::get_key( 'page' );

		return in_array( $page, array( 'subin-email-status', 'subin-email-log' ), true );
	}

	/**
	 * Check if the current request is the Email Log page.
	 *
	 * @return bool
	 */
	private function is_email_log_page_request() {
		return 'subin-email-log' === SUBIN_Request::get_key( 'page' );
	}

	/**
	 * Check if the current request is a Settings admin page.
	 *
	 * @return bool
	 */
	private function is_settings_page_request() {
		$page = SUBIN_Request::get_key( 'page' );

		return in_array( $page, array( 'subin-settings', 'subin-smtp-settings' ), true );
	}

	/**
	 * Check whether the current admin request is the auto-reply editor.
	 *
	 * @return bool
	 */
	private function is_auto_reply_editor_request() {
		$page = SUBIN_Request::get_key( 'page' );
		$view = SUBIN_Request::get_key( 'view' );

		return 'submission-inbox-auto-replies' === $page && in_array( $view, array( 'editor', 'edit' ), true );
	}

	/**
	 * Check whether the current request is the rules policy editor.
	 *
	 * @return bool
	 */
	private function is_auto_reply_rule_editor_request() {
		$page      = SUBIN_Request::get_key( 'page' );
		$rule_view = SUBIN_Request::get_key( 'rule_view' );

		return 'submission-inbox-auto-replies' === $page && in_array( $rule_view, array( 'new', 'edit' ), true );
	}

	/**
	 * Check if the current request is any auto-reply admin page.
	 *
	 * @return bool
	 */
	private function is_auto_reply_page_request() {
		$page = SUBIN_Request::get_key( 'page' );

		return 'submission-inbox-auto-replies' === $page;
	}

}
