<?php
/**
 * Admin Documentation menu — redirects to hosted docs with UTM.
 *
 * @package SHC4WC
 */

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

class SHC4WC_Docs_Settings {
	const PAGE_SLUG = 'payments-square-connect-documentation';

	/** @var self|null */
	private static $instance = null;

	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		add_action( 'admin_menu', array( $this, 'register_menu' ), 12 );
	}

	public function register_menu() {
		add_submenu_page(
			SHC4WC_Admin_Menu::MENU_SLUG,
			__( 'Documentation', 'payments-connect-square' ),
			__( 'Documentation', 'payments-connect-square' ),
			'manage_options',
			self::PAGE_SLUG,
			array( $this, 'redirect_to_docs' )
		);
	}

	public function redirect_to_docs() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to access this page.', 'payments-connect-square' ) );
		}

		wp_safe_redirect( SHC4WC_Docs::admin_url( '', 'docs_menu' ) );
		exit;
	}
}
