<?php
/**
 * Onboarding wizard (free). RTEC-aligned layout; see EvgeOnboardingService.
 *
 * @package WPEventGenius
 */

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

use WPEventGenius\Admin\Services\EvgeOnboardingService;
use WPEventGenius\Common\Utils\Icon;

$success_event_url = ( $step === 3 ) ? get_transient( EvgeOnboardingService::TRANSIENT_SUCCESS_URL ) : '';
$success_event_id  = 0;
if ( 3 === (int) $step ) {
	$success_event_id = absint( get_transient( EvgeOnboardingService::TRANSIENT_SUCCESS_EVENT_ID ) );
	if ( ! $success_event_id ) {
		$success_event_id = EvgeOnboardingService::get_finish_setup_pending_event_id();
	}
}
$onboarding_no_reg_url = '';
if ( $success_event_id > 0 && EVGE_EVENT_POST_TYPE === get_post_type( $success_event_id ) && current_user_can( 'edit_post', $success_event_id ) ) {
	$onboarding_no_reg_url = wp_nonce_url(
		add_query_arg(
			array(
				'evge_onboarding_no_reg' => '1',
				'event_id'               => $success_event_id,
			),
			admin_url( 'admin.php' )
		),
		'evge_onboarding_no_reg'
	);
}
$registrations_url = admin_url( 'admin.php?page=evge-registrations' );
$pricing_url       = 'https://wpeventgenius.com/pricing/?utm_campaign=evge-free&utm_source=onboarding&utm_medium=success-step&utm_content=LearnAboutPro';

$logo_url                  = EVGE_PLUGIN_URL . 'assets/images/admin/eg-logo-svg.svg';
$welcome_illustration_url  = EVGE_PLUGIN_URL . 'assets/images/admin/example-event-listing.png';
$exit_setup_url = wp_nonce_url( add_query_arg( 'evge_exit_onboarding', '1', admin_url( 'admin.php?page=' . EvgeOnboardingService::PAGE_SLUG ) ), 'evge_exit_onboarding' );

$default_date = gmdate( 'Y-m-d', strtotime( '+1 day' ) );
if ( function_exists( 'wp_timezone' ) ) {
	try {
		$d = new \DateTime( 'now', wp_timezone() );
		$d->modify( '+1 day' );
		$default_date = $d->format( 'Y-m-d' );
	} catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
	}
}
?>
<div class="wrap evge-admin-wrap evge-onboarding-page">
	<a href="<?php echo esc_url( $exit_setup_url ); ?>" class="button button-secondary evge-onboarding-exit-setup"><?php esc_html_e( 'Exit Setup', 'event-genius' ); ?></a>
	<div class="evge-onboarding-outer evge-onboarding-outer--step-<?php echo (int) $step; ?>">
		<header class="evge-onboarding-header">
			<div class="evge-onboarding-header-main">
				<div class="evge-onboarding-header-brand">
					<img src="<?php echo esc_url( $logo_url ); ?>" alt="" class="evge-onboarding-logo" width="45" height="45">
				</div>
				<nav class="evge-onboarding-dots" role="tablist" aria-label="<?php esc_attr_e( 'Onboarding progress', 'event-genius' ); ?>">
					<?php for ( $i = 1; $i <= $total_steps; $i++ ) : ?>
						<span class="evge-onboarding-dot <?php echo (int) $i === (int) $progress ? 'evge-onboarding-dot-current' : ''; ?> <?php echo (int) $i < (int) $progress ? 'evge-onboarding-dot-done' : ''; ?>" aria-current="<?php echo (int) $i === (int) $progress ? 'step' : 'false'; ?>"></span>
					<?php endfor; ?>
				</nav>
			</div>
		</header>

		<div class="evge-onboarding-wrap evge-welcome-screen <?php echo 1 === (int) $step ? 'evge-onboarding-wrap--step-welcome' : ''; ?>">
			<div class="evge-onboarding-content evge-welcome-text">
				<?php if ( $step === 1 ) : ?>
					<div class="evge-onboarding-welcome-layout">
						<div class="evge-onboarding-welcome-primary">
							<h2 class="evge-onboarding-step-title"><?php esc_html_e( 'Welcome to Event Genius', 'event-genius' ); ?></h2>
							<p class="evge-onboarding-step-body"><?php esc_html_e( "You'll create one live event with registration enabled, then you can open your event page and try a test signup.", 'event-genius' ); ?></p>
							<p class="evge-onboarding-step-body"><?php esc_html_e( "This only takes a minute. You can add more detail anytime after you're live.", 'event-genius' ); ?></p>
							<div class="evge-onboarding-cta-footer evge-onboarding-welcome-cta">
								<a href="<?php echo esc_url( add_query_arg( array( 'page' => EvgeOnboardingService::PAGE_SLUG, 'step' => 2 ), admin_url( 'admin.php' ) ) ); ?>" class="button button-primary evge-onboarding-btn-continue evge-onboarding-cta-with-chevron">
									<span class="evge-button-text"><?php esc_html_e( 'Continue', 'event-genius' ); ?></span>
									<span class="evge-button-carat" aria-hidden="true">&#8250;</span>
								</a>
							</div>
						</div>
						<div class="evge-onboarding-welcome-secondary">
							<p class="evge-onboarding-welcome-example-label"><?php esc_html_e( 'Example event', 'event-genius' ); ?></p>
							<div class="evge-onboarding-welcome-illustration">
								<img src="<?php echo esc_url( $welcome_illustration_url ); ?>" alt="<?php echo esc_attr__( 'Example event listing preview', 'event-genius' ); ?>" loading="lazy" decoding="async">
							</div>
						</div>
					</div>
				<?php endif; ?>

				<?php if ( $step === 2 ) : ?>
					<h2 class="evge-onboarding-step-title"><?php esc_html_e( 'Set up your first event', 'event-genius' ); ?></h2>
					<p class="evge-onboarding-step-body"><?php esc_html_e( 'Registration will be turned on for this event automatically.', 'event-genius' ); ?></p>
					<div class="evge-onboarding-form">
						<p class="evge-onboarding-form-field">
							<label for="evge-onboarding-event-title"><?php esc_html_e( 'Event name', 'event-genius' ); ?></label>
							<input type="text" id="evge-onboarding-event-title" class="evge-onboarding-input evge-onboarding-input-text" required value="<?php echo esc_attr__( 'My first event', 'event-genius' ); ?>">
						</p>
						<p class="evge-onboarding-form-field">
							<label for="evge-onboarding-event-start-date"><?php esc_html_e( 'Start date', 'event-genius' ); ?></label>
							<span class="evge-onboarding-date-time-wrap">
								<input type="date" id="evge-onboarding-event-start-date" class="evge-onboarding-input" value="<?php echo esc_attr( $default_date ); ?>">
								<input type="time" id="evge-onboarding-event-start-time" class="evge-onboarding-input" value="09:00">
							</span>
						</p>
						<p class="evge-onboarding-form-field">
							<label for="evge-onboarding-event-end-date"><?php esc_html_e( 'End date', 'event-genius' ); ?></label>
							<span class="evge-onboarding-date-time-wrap">
								<input type="date" id="evge-onboarding-event-end-date" class="evge-onboarding-input" value="<?php echo esc_attr( $default_date ); ?>">
								<input type="time" id="evge-onboarding-event-end-time" class="evge-onboarding-input" value="17:00">
							</span>
						</p>
						<p class="evge-onboarding-form-note"><?php esc_html_e( 'You can change all of this later. This just gets you up and running.', 'event-genius' ); ?></p>
					</div>
					<div class="evge-onboarding-create-event-footer">
						<button type="button" class="button button-primary evge-onboarding-create-event evge-onboarding-create-continue evge-onboarding-cta-with-chevron" id="evge-onboarding-create-event">
							<span class="evge-button-text"><?php esc_html_e( 'Create event and continue', 'event-genius' ); ?></span>
							<span class="evge-button-carat" aria-hidden="true">&#8250;</span>
						</button>
					</div>
					<p class="evge-onboarding-ajax-message" aria-live="polite"></p>
				<?php endif; ?>

				<?php if ( $step === 3 ) : ?>
					<h2 class="evge-onboarding-step-title">🎉 <?php esc_html_e( "You're live!", 'event-genius' ); ?></h2>
					<p class="evge-onboarding-step-body"><?php esc_html_e( 'Your event is accepting registrations.', 'event-genius' ); ?></p>
					<ul class="evge-onboarding-success-list">
						<li><span class="evge-onboarding-success-list-icon" aria-hidden="true"><?php echo Icon::get( 'check' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span><?php esc_html_e( 'Visitors will see a registration form on the event page', 'event-genius' ); ?></li>
						<li><span class="evge-onboarding-success-list-icon" aria-hidden="true"><?php echo Icon::get( 'check' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span><?php esc_html_e( 'They will receive a confirmation email', 'event-genius' ); ?></li>
						<li><span class="evge-onboarding-success-list-icon" aria-hidden="true"><?php echo Icon::get( 'check' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span><?php esc_html_e( 'They will appear in your registrations list', 'event-genius' ); ?></li>
					</ul>
					<p class="evge-onboarding-step-body"><?php esc_html_e( 'Try submitting a test registration on your event page to see how everything works, including confirmation emails.', 'event-genius' ); ?></p>
					<div class="evge-onboarding-cta-footer evge-onboarding-success-primary">
						<?php if ( $success_event_url ) : ?>
							<a href="<?php echo esc_url( $success_event_url ); ?>" class="button button-primary evge-onboarding-cta-with-chevron" target="_blank" rel="noopener noreferrer"><span class="evge-button-text"><?php esc_html_e( 'View event page', 'event-genius' ); ?></span><span class="evge-button-carat" aria-hidden="true">&#8250;</span></a>
						<?php endif; ?>
					</div>
					<p class="evge-onboarding-success-cta-note"><?php esc_html_e( 'Open your event page to see the registration form and submit a test entry.', 'event-genius' ); ?></p>
					<h3 class="evge-onboarding-success-other-heading"><?php esc_html_e( 'Other actions', 'event-genius' ); ?></h3>
					<div class="evge-onboarding-actions evge-onboarding-success-secondary">
						<a href="<?php echo esc_url( $registrations_url ); ?>" class="button button-secondary evge-onboarding-btn-small"><?php esc_html_e( 'Manage registrations', 'event-genius' ); ?></a>
						<a href="<?php echo esc_url( $exit_setup_url ); ?>" class="button button-secondary evge-onboarding-btn-small"><?php esc_html_e( 'Close wizard', 'event-genius' ); ?></a>
					</div>
					<div class="evge-onboarding-pro-cta">
						<p><?php esc_html_e( 'Want to accept payments and unlock more features?', 'event-genius' ); ?></p>
						<a href="<?php echo esc_url( $pricing_url ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'View Event Genius Pro', 'event-genius' ); ?></a>
					</div>
					<?php if ( $onboarding_no_reg_url ) : ?>
						<p class="evge-onboarding-offramp-no-reg">
							<?php esc_html_e( 'Prefer to use Event Genius without registrations?', 'event-genius' ); ?><br>
							<a href="<?php echo esc_url( $onboarding_no_reg_url ); ?>"><?php esc_html_e( 'Disable registration and manage events', 'event-genius' ); ?> &#8594;</a>
						</p>
					<?php endif; ?>
				<?php endif; ?>
			</div>
		</div>
	</div>
</div>
