<?php
/**
 * Client-facing help page with screenshots (EasySite Client role).
 *
 * @package EasySite_Pro
 */

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

/**
 * Register Help menu for client accounts.
 */
function easysite_client_help_menu() {
	if ( ! easysite_current_user_is_client() ) {
		return;
	}
	add_menu_page(
		__( 'Help & Guides', 'easysite-pro' ),
		__( 'Help & Guides', 'easysite-pro' ),
		'read',
		'easysite-client-help',
		'easysite_render_client_help_page',
		'dashicons-book-alt',
		4
	);
}
add_action( 'admin_menu', 'easysite_client_help_menu' );

/**
 * Enqueue admin styles on client help page.
 *
 * @param string $hook Hook suffix.
 */
function easysite_client_help_assets( $hook ) {
	if ( 'toplevel_page_easysite-client-help' !== $hook ) {
		return;
	}
	wp_enqueue_style(
		'easysite-admin',
		EASYSITE_PRO_URI . '/assets/css/admin.css',
		array(),
		EASYSITE_PRO_VERSION
	);
}
add_action( 'admin_enqueue_scripts', 'easysite_client_help_assets' );

/**
 * URL for a bundled help screenshot.
 *
 * @param string $file Filename in assets/images/help/.
 * @return string
 */
function easysite_client_help_image_url( $file ) {
	return EASYSITE_PRO_URI . '/assets/images/help/' . ltrim( $file, '/' );
}

/**
 * Whether a help screenshot exists in the theme.
 *
 * @param string $file Filename.
 * @return bool
 */
function easysite_client_help_image_exists( $file ) {
	return file_exists( EASYSITE_PRO_DIR . '/assets/images/help/' . ltrim( $file, '/' ) );
}

/**
 * Render client help page.
 */
function easysite_render_client_help_page() {
	$agency   = easysite_get_brand_name();
	$support  = easysite_get_mod( 'easysite_agency_support_email', '' );
	$front_id = (int) get_option( 'page_on_front' );
	$ve_url   = $front_id ? add_query_arg( 'easysite_edit', '1', get_permalink( $front_id ) ) : add_query_arg( 'easysite_edit', '1', home_url( '/' ) );

	$guides = array(
		array(
			'title' => __( 'Edit your website', 'easysite-pro' ),
			'steps' => array(
				__( 'Click “Edit your site visually” below or use the button on your dashboard.', 'easysite-pro' ),
				__( 'Click any text to change it — headlines, paragraphs, and buttons.', 'easysite-pro' ),
				__( 'Click any image to replace it with a photo from your computer.', 'easysite-pro' ),
				__( 'Click Save when finished (or press Ctrl+S / Cmd+S).', 'easysite-pro' ),
			),
			'image' => 'visual-editor.png',
			'cta'   => $ve_url,
			'cta_label' => __( 'Open visual editor', 'easysite-pro' ),
		),
		array(
			'title' => __( 'Logo & brand colors', 'easysite-pro' ),
			'steps' => array(
				__( 'Open Site colors & logo from your dashboard.', 'easysite-pro' ),
				__( 'Upload your logo (PNG or JPG).', 'easysite-pro' ),
				__( 'Pick your main and accent colors, then save.', 'easysite-pro' ),
			),
			'cta'   => admin_url( 'admin.php?page=easysite-site-settings' ),
			'cta_label' => __( 'Site colors & logo', 'easysite-pro' ),
		),
		array(
			'title' => __( 'Pricing & services sections', 'easysite-pro' ),
			'steps' => array(
				__( 'Open the visual editor on your homepage.', 'easysite-pro' ),
				__( 'Scroll to the pricing or services section.', 'easysite-pro' ),
				__( 'Click each price or service name to update it.', 'easysite-pro' ),
			),
			'image' => 'edit-pricing.png',
			'cta'   => $ve_url,
			'cta_label' => __( 'Edit homepage', 'easysite-pro' ),
		),
		array(
			'title' => __( 'Keyboard shortcuts', 'easysite-pro' ),
			'steps' => array(
				__( 'Ctrl+S / Cmd+S — Save changes', 'easysite-pro' ),
				__( 'Ctrl+Z / Cmd+Z — Undo', 'easysite-pro' ),
				__( 'Esc — Close a panel', 'easysite-pro' ),
			),
		),
		array(
			'title' => __( 'Get writing help', 'easysite-pro' ),
			'steps' => array(
				__( 'In the visual editor, look for “Write for me” on text fields.', 'easysite-pro' ),
				__( 'Click it for suggested copy you can edit.', 'easysite-pro' ),
			),
			'cta'   => $ve_url,
			'cta_label' => __( 'Try it now', 'easysite-pro' ),
		),
	);
	?>
	<div class="wrap easysite-help easysite-client-help">
		<h1><?php echo esc_html( sprintf( __( 'How to edit your site — %s', 'easysite-pro' ), $agency ) ); ?></h1>
		<p><?php esc_html_e( 'Step-by-step guides with screenshots. No technical skills needed.', 'easysite-pro' ); ?></p>

		<p class="easysite-client-help-actions">
			<a class="button button-primary button-hero" href="<?php echo esc_url( $ve_url ); ?>"><?php esc_html_e( 'Edit your site visually', 'easysite-pro' ); ?></a>
			<a class="button button-secondary button-hero" href="<?php echo esc_url( admin_url( 'admin.php?page=easysite-site-settings' ) ); ?>"><?php esc_html_e( 'Site colors & logo', 'easysite-pro' ); ?></a>
		</p>

		<?php foreach ( $guides as $guide ) : ?>
			<div class="easysite-client-help-section">
				<h2><?php echo esc_html( $guide['title'] ); ?></h2>
				<?php if ( ! empty( $guide['image'] ) && easysite_client_help_image_exists( $guide['image'] ) ) : ?>
					<figure class="easysite-client-help-figure">
						<img src="<?php echo esc_url( easysite_client_help_image_url( $guide['image'] ) ); ?>" alt="<?php echo esc_attr( $guide['title'] ); ?>" loading="lazy" />
					</figure>
				<?php endif; ?>
				<ol>
					<?php foreach ( $guide['steps'] as $step ) : ?>
						<li><?php echo esc_html( $step ); ?></li>
					<?php endforeach; ?>
				</ol>
				<?php if ( ! empty( $guide['cta'] ) ) : ?>
					<p><a class="button button-secondary" href="<?php echo esc_url( $guide['cta'] ); ?>"><?php echo esc_html( $guide['cta_label'] ); ?></a></p>
				<?php endif; ?>
			</div>
		<?php endforeach; ?>

		<div class="easysite-client-help-section easysite-client-help-support">
			<h2><?php esc_html_e( 'Need more help?', 'easysite-pro' ); ?></h2>
			<p><?php esc_html_e( 'Contact your agency if something looks wrong or you need access to a feature not listed here.', 'easysite-pro' ); ?></p>
			<?php if ( $support && is_email( $support ) ) : ?>
				<p><a class="button" href="<?php echo esc_url( 'mailto:' . $support ); ?>"><?php echo esc_html( sprintf( __( 'Email %s', 'easysite-pro' ), $support ) ); ?></a></p>
			<?php endif; ?>
		</div>
	</div>
	<?php
}
