<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
const DROP_PRINT_ONBOARDING_PAGE_SLUG = 'drop-print-onboarding';
const DROP_PRINT_ONBOARDING_EMAIL_META = 'drop_print_onboarding_email';
const DROP_PRINT_ONBOARDING_MAX_CONNECT_ATTEMPTS = 3;
function drop_print_onboarding_icc_url(): string {
	return (string) apply_filters(
		'drop_print_onboarding_icc_url',
		'https://github.com/dropprint/alden-delta-rgb/releases/download/1.0/AldenDelta.icc'
	);
}
function drop_print_onboarding_page_url( string $step = '', array $extra = array() ): string {
	$args = array( 'page' => DROP_PRINT_ONBOARDING_PAGE_SLUG );
	if ( '' !== $step ) {
		$args['step'] = $step;
	}
	return add_query_arg( array_merge( $args, $extra ), admin_url( 'admin.php' ) );
}
function drop_print_onboarding_register_page() {
	add_dashboard_page( '', '', 'manage_options', DROP_PRINT_ONBOARDING_PAGE_SLUG, 'drop_print_onboarding_render_page' );
}
add_action( 'admin_menu', 'drop_print_onboarding_register_page', 20 );
function drop_print_onboarding_is_current(): bool {
	return is_admin()
		&& ! empty( $_GET['page'] )
		&& DROP_PRINT_ONBOARDING_PAGE_SLUG === sanitize_key( wp_unslash( $_GET['page'] ) );
}
function drop_print_onboarding_user_is_connected( int $user_id ): bool {
	if ( $user_id <= 0 ) {
		return false;
	}
	$settings = drop_print_get_user_settings( $user_id );
	return ! empty( $settings['auth_username_b64'] ) && ! empty( $settings['auth_password_b64'] );
}
function drop_print_onboarding_get_steps( int $user_id ): array {
	$connected = drop_print_onboarding_user_is_connected( $user_id );
	$steps     = array();
	if ( ! $connected ) {
		$steps['email']    = array( 'label' => __( 'Account', 'drop-print' ) );
		$steps['currency'] = array( 'label' => __( 'Currency', 'drop-print' ) );
	}
	$steps['units']    = array( 'label' => __( 'Units', 'drop-print' ) );
	$steps['intent']   = array( 'label' => __( 'Intent', 'drop-print' ) );
	$steps['location'] = array( 'label' => __( 'Location', 'drop-print' ) );
	$steps['shipping'] = array( 'label' => __( 'Shipping', 'drop-print' ) );
	$steps['colour']   = array( 'label' => __( 'Colour', 'drop-print' ) );
	if ( ! $connected ) {
		$steps['connect'] = array( 'label' => __( 'Connect', 'drop-print' ) );
	}
	$steps['billing'] = array( 'label' => __( 'Billing', 'drop-print' ) );
	$steps['done'] = array( 'label' => __( 'Done', 'drop-print' ) );
	return $steps;
}
function drop_print_onboarding_current_step( array $steps ): string {
	$requested = isset( $_GET['step'] ) ? sanitize_key( wp_unslash( $_GET['step'] ) ) : '';
	if ( '' !== $requested && isset( $steps[ $requested ] ) ) {
		return $requested;
	}
	$slugs = array_keys( $steps );
	return isset( $slugs[0] ) ? (string) $slugs[0] : '';
}
function drop_print_onboarding_next_step( array $steps, string $current ): string {
	$slugs = array_keys( $steps );
	$index = array_search( $current, $slugs, true );
	if ( false === $index || ! isset( $slugs[ $index + 1 ] ) ) {
		return '';
	}
	return $slugs[ $index + 1 ];
}
function drop_print_onboarding_admin_body_class( string $classes ): string {
	if ( drop_print_onboarding_is_current() ) {
		$classes .= ' drop-print-onboarding-page';
	}
	return $classes;
}
add_filter( 'admin_body_class', 'drop_print_onboarding_admin_body_class' );
function drop_print_onboarding_suppress_notices() {
	if ( drop_print_onboarding_is_current() ) {
		remove_all_actions( 'admin_notices' );
		remove_all_actions( 'all_admin_notices' );
	}
}
add_action( 'in_admin_header', 'drop_print_onboarding_suppress_notices', 0 );
function drop_print_onboarding_enqueue_assets() {
	if ( ! drop_print_onboarding_is_current() ) {
		return;
	}
	$path = DROP_PRINT_PLUGIN_PATH . 'assets/css/onboarding.css';
	wp_enqueue_style(
		'drop-print-onboarding',
		DROP_PRINT_PLUGIN_URL . 'assets/css/onboarding.css',
		array(),
		file_exists( $path ) ? filemtime( $path ) : DROP_PRINT_VERSION
	);
}
add_action( 'admin_enqueue_scripts', 'drop_print_onboarding_enqueue_assets' );
function drop_print_onboarding_maybe_activation_redirect() {
	$user_id = (int) get_transient( 'drop_print_activation_redirect' );
	if ( $user_id <= 0 ) {
		return;
	}
	if ( wp_doing_ajax() || ( defined( 'WP_CLI' ) && WP_CLI ) || is_network_admin() ) {
		return;
	}
	if ( isset( $_GET['activate-multi'] ) || get_current_user_id() !== $user_id ) {
		return;
	}
	delete_transient( 'drop_print_activation_redirect' );
	if ( ! current_user_can( 'manage_options' ) || drop_print_onboarding_is_current() ) {
		return;
	}
	wp_safe_redirect( drop_print_onboarding_page_url() );
	exit;
}
add_action( 'admin_init', 'drop_print_onboarding_maybe_activation_redirect', 5 );
function drop_print_onboarding_guard_billing_step() {
	if ( ! drop_print_onboarding_is_current() || wp_doing_ajax() ) {
		return;
	}
	if ( ! isset( $_GET['step'] ) || 'billing' !== sanitize_key( wp_unslash( $_GET['step'] ) ) ) {
		return;
	}
	$user_id = get_current_user_id();
	if ( ! current_user_can( 'manage_options' ) || drop_print_onboarding_user_is_connected( $user_id ) ) {
		return;
	}
	$steps = drop_print_onboarding_get_steps( $user_id );
	$extra = array();
	if ( isset( $_GET['attempt'] ) ) {
		$extra['attempt'] = absint( wp_unslash( $_GET['attempt'] ) );
	}
	wp_safe_redirect( drop_print_onboarding_page_url( isset( $steps['connect'] ) ? 'connect' : 'done', $extra ) );
	exit;
}
add_action( 'admin_init', 'drop_print_onboarding_guard_billing_step', 6 );
function drop_print_onboarding_handle_post() {
	if ( ! is_admin() || empty( $_POST['drop_print_onboarding_step'] ) ) {
		return;
	}
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( esc_html__( 'You do not have sufficient permissions to do this.', 'drop-print' ) );
	}
	check_admin_referer( 'drop_print_onboarding' );
	$user_id = get_current_user_id();
	$steps   = drop_print_onboarding_get_steps( $user_id );
	$step    = sanitize_key( wp_unslash( $_POST['drop_print_onboarding_step'] ) );
	if ( ! isset( $steps[ $step ] ) ) {
		return;
	}
	$settings = drop_print_get_user_settings( $user_id );
	$defaults = drop_print_get_default_user_settings();
	$dirty    = false;
	switch ( $step ) {
		case 'email':
			$email = isset( $_POST['dp_email'] ) ? sanitize_email( wp_unslash( $_POST['dp_email'] ) ) : '';
			if ( '' !== $email && is_email( $email ) ) {
				update_user_meta( $user_id, DROP_PRINT_ONBOARDING_EMAIL_META, $email );
			}
			break;
		case 'currency':
			$currency = isset( $_POST['dp_currency'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['dp_currency'] ) ) ) : '';
			$valid    = function_exists( 'get_woocommerce_currencies' ) ? array_keys( get_woocommerce_currencies() ) : array();
			if ( '' !== $currency && ( empty( $valid ) || in_array( $currency, $valid, true ) ) ) {
				$settings['billing_currency'] = $currency;
				$dirty                        = true;
			}
			break;
		case 'units':
			$unit = isset( $_POST['dp_measurement_unit'] ) ? sanitize_key( wp_unslash( $_POST['dp_measurement_unit'] ) ) : '';
			if ( in_array( $unit, array( 'in', 'cm' ), true ) ) {
				update_option( 'woocommerce_dimension_unit', $unit );
			}
			break;
		case 'intent':
			$intent                     = isset( $_POST['dp_default_intent'] ) ? sanitize_key( wp_unslash( $_POST['dp_default_intent'] ) ) : '';
			$settings['default_intent'] = in_array( $intent, array( 'photo', 'art_repro', 'ask' ), true ) ? $intent : $defaults['default_intent'];
			$dirty                      = true;
			break;
		case 'location':
			$country                                = isset( $_POST['dp_default_customer_location'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['dp_default_customer_location'] ) ) ) : '';
			$settings['default_customer_location']  = ( 2 === strlen( $country ) && ctype_alpha( $country ) ) ? $country : $defaults['default_customer_location'];
			$dirty                                  = true;
			break;
		case 'shipping':
			$deferred = ! empty( $_POST['dp_skip'] )
				|| ( isset( $_POST['dp_shipping_consent'] ) && 'later' === sanitize_key( wp_unslash( $_POST['dp_shipping_consent'] ) ) );
			if ( $deferred ) {
				update_option( 'drop_print_shipping_reminder_snooze_until', time() + DAY_IN_SECONDS );
				break;
			}
			delete_option( 'drop_print_shipping_reminder_snooze_until' );
			if ( drop_print_onboarding_user_is_connected( $user_id ) && function_exists( 'drop_print_enroll_shipping_zones' ) ) {
				drop_print_enroll_shipping_zones( $user_id );
			}
			break;
		case 'connect':
			if ( ! drop_print_use_headless_connect() ) {
				break; 
			}
			drop_print_onboarding_handle_connect_post( $user_id, $steps );
			return;
		case 'billing':
			if ( ! empty( $_POST['dp_recheck'] ) ) {
				wp_safe_redirect( drop_print_onboarding_page_url( 'billing' ) );
				exit;
			}
			if ( empty( $_POST['dp_enable_auto_production'] ) ) {
				break;
			}
			$card_state = drop_print_onboarding_get_payment_method_state( $user_id );
			if ( empty( $card_state['ok'] ) || empty( $card_state['has_card'] ) ) {
				wp_safe_redirect( drop_print_onboarding_page_url( 'billing', array( 'dp_card_error' => 1 ) ) );
				exit;
			}
			$settings['auto_production'] = 'on';
			$dirty                       = true;
			break;
	}
	if ( $dirty ) {
		update_user_meta( $user_id, 'drop_print_user_settings', $settings );
	}
	$next = drop_print_onboarding_next_step( $steps, $step );
	wp_safe_redirect( '' === $next ? drop_print_onboarding_page_url( 'done' ) : drop_print_onboarding_page_url( $next ) );
	exit;
}
add_action( 'admin_init', 'drop_print_onboarding_handle_post' );
function drop_print_onboarding_render_page() {
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'drop-print' ) );
	}
	$user_id = get_current_user_id();
	$steps   = drop_print_onboarding_get_steps( $user_id );
	$current = drop_print_onboarding_current_step( $steps );
	$slugs   = array_keys( $steps );
	$index   = (int) array_search( $current, $slugs, true );
	?>
	<?php
	$logo_url = file_exists( DROP_PRINT_PLUGIN_PATH . 'assets/images/text-logo-small-dark.png' )
		? DROP_PRINT_PLUGIN_URL . 'assets/images/text-logo-small-dark.png'
		: '';
	?>
	<div class="dp-onboarding">
		<ul class="dp-onboarding__progress">
			<?php foreach ( $slugs as $i => $slug ) : ?>
				<li class="<?php echo esc_attr( $i < $index ? 'is-done' : ( $i === $index ? 'is-current' : '' ) ); ?>"></li>
			<?php endforeach; ?>
		</ul>
		<header class="dp-onboarding__masthead">
			<?php if ( '' !== $logo_url ) : ?>
				<img class="dp-onboarding__logo" src="<?php echo esc_url( $logo_url ); ?>" alt="<?php esc_attr_e( 'Drop Print', 'drop-print' ); ?>" width="140" height="37" />
			<?php else : ?>
				<span class="dp-onboarding__logo-text"><?php esc_html_e( 'Drop Print', 'drop-print' ); ?></span>
			<?php endif; ?>
			<span class="dp-onboarding__pipe" aria-hidden="true"></span>
			<span class="dp-onboarding__by"><?php esc_html_e( 'by Alden Fine Print & Co.', 'drop-print' ); ?></span>
		</header>
		<div class="dp-onboarding__inner">
			<div class="dp-onboarding__card">
				<p class="dp-onboarding__stepcount">
					<?php
					printf(
						esc_html__( 'Step %1$d of %2$d — %3$s', 'drop-print' ),
						(int) $index + 1,
						count( $slugs ),
						esc_html( $steps[ $current ]['label'] )
					);
					?>
				</p>
				<?php
				$renderer = 'drop_print_onboarding_render_step_' . $current;
				if ( function_exists( $renderer ) ) {
					call_user_func( $renderer, $user_id, $steps );
				}
				?>
			</div>
		</div>
	</div>
	<?php
}
function drop_print_onboarding_form_open( string $step ) {
	echo '<form method="post" action="' . esc_url( drop_print_onboarding_page_url( $step ) ) . '">';
	wp_nonce_field( 'drop_print_onboarding' );
	echo '<input type="hidden" name="drop_print_onboarding_step" value="' . esc_attr( $step ) . '" />';
}
function drop_print_onboarding_actions( string $submit_label, string $skip_to = '', bool $skip_posts = false, string $skip_label = '' ) {
	$label = '' !== $skip_label ? $skip_label : __( 'Skip', 'drop-print' );
	?>
	<div class="dp-onboarding__actions">
		<div class="dp-onboarding__actions-right">
			<?php if ( $skip_posts ) : ?>
				<button type="submit" name="dp_skip" value="1" class="dp-link-skip dp-link-skip--button"><?php echo esc_html( $label ); ?></button>
			<?php elseif ( '' !== $skip_to ) : ?>
				<a class="dp-link-skip" href="<?php echo esc_url( drop_print_onboarding_page_url( $skip_to ) ); ?>"><?php echo esc_html( $label ); ?></a>
			<?php endif; ?>
			<button type="submit" class="dp-btn"><?php echo esc_html( $submit_label ); ?></button>
		</div>
	</div>
	</form>
	<?php
}
function drop_print_onboarding_render_step_email( int $user_id, array $steps ) {
	$saved = (string) get_user_meta( $user_id, DROP_PRINT_ONBOARDING_EMAIL_META, true );
	if ( '' === $saved ) {
		$wp_user = get_userdata( $user_id );
		$saved   = $wp_user ? $wp_user->user_email : '';
	}
	?>
	<h1><?php esc_html_e( 'Let’s set up Drop Print', 'drop-print' ); ?></h1>
	<p><?php esc_html_e( 'A few questions about how you work, then we’ll connect your account. It takes about a minute.', 'drop-print' ); ?></p>
	<?php drop_print_onboarding_form_open( 'email' ); ?>
		<div class="dp-onboarding__field">
			<label class="dp-onboarding__label" for="dp_email"><?php esc_html_e( 'Email for your Drop Print account', 'drop-print' ); ?></label>
			<input type="email" id="dp_email" name="dp_email" value="<?php echo esc_attr( $saved ); ?>" autocomplete="email" />
			<p class="dp-onboarding__hint"><?php esc_html_e( 'We’ll carry this over to the sign-up form at the end so you don’t have to type it twice.', 'drop-print' ); ?></p>
		</div>
	<?php drop_print_onboarding_actions( __( 'Continue', 'drop-print' ), drop_print_onboarding_next_step( $steps, 'email' ) ); ?>
	<?php
}
function drop_print_onboarding_render_step_currency( int $user_id, array $steps ) {
	$settings   = drop_print_get_user_settings( $user_id );
	$selected   = ! empty( $settings['billing_currency'] )
		? $settings['billing_currency']
		: ( function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : 'GBP' );
	$currencies = function_exists( 'get_woocommerce_currencies' ) ? get_woocommerce_currencies() : array( 'GBP' => 'Pound sterling', 'USD' => 'US dollar', 'EUR' => 'Euro' );
	?>
	<h1><?php esc_html_e( 'What currency do you sell in?', 'drop-print' ); ?></h1>
	<p><?php esc_html_e( 'This is the currency you’ll be billed in and the one your prices are set in.', 'drop-print' ); ?></p>
	<?php drop_print_onboarding_form_open( 'currency' ); ?>
		<div class="dp-onboarding__field">
			<label class="dp-onboarding__label" for="dp_currency"><?php esc_html_e( 'Currency', 'drop-print' ); ?></label>
			<select id="dp_currency" name="dp_currency">
				<?php foreach ( $currencies as $code => $name ) : ?>
					<option value="<?php echo esc_attr( $code ); ?>" <?php selected( $selected, $code ); ?>>
						<?php echo esc_html( $name . ' (' . $code . ')' ); ?>
					</option>
				<?php endforeach; ?>
			</select>
		</div>
	<?php drop_print_onboarding_actions( __( 'Continue', 'drop-print' ), drop_print_onboarding_next_step( $steps, 'currency' ) ); ?>
	<?php
}
function drop_print_onboarding_render_step_units( int $user_id, array $steps ) {
	$raw     = get_option( 'woocommerce_dimension_unit', 'in' );
	$map     = array( 'cm' => 'cm', 'm' => 'cm', 'mm' => 'cm', 'in' => 'in', 'yd' => 'in', 'ft' => 'in' );
	$unit    = $map[ $raw ] ?? 'in';
	?>
	<h1><?php esc_html_e( 'Inches or centimetres?', 'drop-print' ); ?></h1>
	<p><?php esc_html_e( 'How print sizes are shown to you when choosing papers and setting prices. Your customers always see sizes in the units of their own region.', 'drop-print' ); ?></p>
	<p class="dp-onboarding__hint"><?php esc_html_e( 'This is your store’s WooCommerce measurement unit, so changing it here also changes it for your other products.', 'drop-print' ); ?></p>
	<?php drop_print_onboarding_form_open( 'units' ); ?>
		<div class="dp-onboarding__choices">
			<label class="dp-onboarding__choice">
				<input type="radio" name="dp_measurement_unit" value="in" <?php checked( $unit, 'in' ); ?> />
				<span class="dp-onboarding__choice-body">
					<span class="dp-onboarding__choice-title"><?php esc_html_e( 'Inches', 'drop-print' ); ?></span>
					<span class="dp-onboarding__choice-desc"><?php esc_html_e( 'The trade standard for fine-art paper sizes.', 'drop-print' ); ?></span>
				</span>
			</label>
			<label class="dp-onboarding__choice">
				<input type="radio" name="dp_measurement_unit" value="cm" <?php checked( $unit, 'cm' ); ?> />
				<span class="dp-onboarding__choice-body">
					<span class="dp-onboarding__choice-title"><?php esc_html_e( 'Centimetres', 'drop-print' ); ?></span>
					<span class="dp-onboarding__choice-desc"><?php esc_html_e( 'Metric, including A-series sizes.', 'drop-print' ); ?></span>
				</span>
			</label>
		</div>
	<?php drop_print_onboarding_actions( __( 'Continue', 'drop-print' ), drop_print_onboarding_next_step( $steps, 'units' ) ); ?>
	<?php
}
function drop_print_onboarding_render_step_intent( int $user_id, array $steps ) {
	$settings = drop_print_get_user_settings( $user_id );
	$intent   = $settings['default_intent'] ?? 'ask';
	?>
	<h1><?php esc_html_e( 'What do you mostly print?', 'drop-print' ); ?></h1>
	<p><?php esc_html_e( 'This changes how we judge which papers suit your work. Reproducing a physical painting and printing a photograph want different things from a paper, so we weigh them differently.', 'drop-print' ); ?></p>
	<?php drop_print_onboarding_form_open( 'intent' ); ?>
		<div class="dp-onboarding__choices">
			<label class="dp-onboarding__choice">
				<input type="radio" name="dp_default_intent" value="photo" <?php checked( $intent, 'photo' ); ?> />
				<span class="dp-onboarding__choice-body">
					<span class="dp-onboarding__choice-title"><?php esc_html_e( 'Photography', 'drop-print' ); ?></span>
					<span class="dp-onboarding__choice-desc"><?php esc_html_e( 'Digital or film photographs, where deep blacks and tonal separation carry the image.', 'drop-print' ); ?></span>
				</span>
			</label>
			<label class="dp-onboarding__choice">
				<input type="radio" name="dp_default_intent" value="art_repro" <?php checked( $intent, 'art_repro' ); ?> />
				<span class="dp-onboarding__choice-body">
					<span class="dp-onboarding__choice-title"><?php esc_html_e( 'Art reproduction', 'drop-print' ); ?></span>
					<span class="dp-onboarding__choice-desc"><?php esc_html_e( 'Giclée reproduction of physical work, where paper surface should answer the original.', 'drop-print' ); ?></span>
				</span>
			</label>
			<label class="dp-onboarding__choice">
				<input type="radio" name="dp_default_intent" value="ask" <?php checked( $intent, 'ask' ); ?> />
				<span class="dp-onboarding__choice-body">
					<span class="dp-onboarding__choice-title"><?php esc_html_e( 'Ask me each time', 'drop-print' ); ?></span>
					<span class="dp-onboarding__choice-desc"><?php esc_html_e( 'You work across both. We’ll ask per product instead of assuming.', 'drop-print' ); ?></span>
				</span>
			</label>
		</div>
	<?php drop_print_onboarding_actions( __( 'Continue', 'drop-print' ), drop_print_onboarding_next_step( $steps, 'intent' ) ); ?>
	<?php
}
function drop_print_onboarding_render_step_location( int $user_id, array $steps ) {
	$settings = drop_print_get_user_settings( $user_id );
	$selected = $settings['default_customer_location'] ?? '';
	if ( '' === $selected ) {
		$base     = function_exists( 'wc_get_base_location' ) ? wc_get_base_location() : array();
		$selected = isset( $base['country'] ) ? $base['country'] : '';
	}
	$countries = ( function_exists( 'WC' ) && WC() && isset( WC()->countries ) ) ? WC()->countries->get_countries() : array();
	?>
	<h1><?php esc_html_e( 'Where are most of your customers?', 'drop-print' ); ?></h1>
	<p><?php esc_html_e( 'We use this to show you realistic prices and delivery before anyone has entered an address. Every real order is still priced against its own destination.', 'drop-print' ); ?></p>
	<?php drop_print_onboarding_form_open( 'location' ); ?>
		<div class="dp-onboarding__field">
			<label class="dp-onboarding__label" for="dp_default_customer_location"><?php esc_html_e( 'Primary customer country', 'drop-print' ); ?></label>
			<?php if ( ! empty( $countries ) ) : ?>
				<select id="dp_default_customer_location" name="dp_default_customer_location">
					<?php foreach ( $countries as $code => $name ) : ?>
						<option value="<?php echo esc_attr( $code ); ?>" <?php selected( $selected, $code ); ?>><?php echo esc_html( $name ); ?></option>
					<?php endforeach; ?>
				</select>
			<?php else : ?>
				<input type="text" id="dp_default_customer_location" name="dp_default_customer_location" value="<?php echo esc_attr( $selected ); ?>" maxlength="2" placeholder="GB" />
			<?php endif; ?>
		</div>
	<?php drop_print_onboarding_actions( __( 'Continue', 'drop-print' ), drop_print_onboarding_next_step( $steps, 'location' ) ); ?>
	<?php
}
function drop_print_onboarding_render_step_shipping( int $user_id, array $steps ) {
	$report    = function_exists( 'drop_print_shipping_zone_report' ) ? drop_print_shipping_zone_report() : array( 'wc_active' => false, 'zone_count' => 0, 'enrolled' => false, 'has_any' => false );
	$connected = drop_print_onboarding_user_is_connected( $user_id );
	?>
	<h1><?php esc_html_e( 'Live shipping at checkout', 'drop-print' ); ?></h1>
	<p><?php esc_html_e( 'When a customer buys a print, Drop Print can quote the real cost of getting that exact parcel to their exact address — rather than a flat rate you had to guess at.', 'drop-print' ); ?></p>
	<?php if ( ! $report['wc_active'] ) : ?>
		<div class="dp-onboarding__note">
			<p><?php esc_html_e( 'WooCommerce isn’t active, so there’s nothing to set up yet. You can come back to this once it is.', 'drop-print' ); ?></p>
		</div>
		<?php drop_print_onboarding_form_open( 'shipping' ); ?>
		<?php drop_print_onboarding_actions( __( 'Continue', 'drop-print' ) ); ?>
		<?php
		return;
	endif;
	?>
	<?php if ( $report['enrolled'] ) : ?>
		<div class="dp-onboarding__note">
			<p class="dp-onboarding__note-title"><?php esc_html_e( 'Already set up', 'drop-print' ); ?></p>
			<p><?php esc_html_e( 'Drop Print’s live rate is already on your shipping zones. Nothing to do here.', 'drop-print' ); ?></p>
		</div>
		<?php drop_print_onboarding_form_open( 'shipping' ); ?>
		<input type="hidden" name="dp_shipping_consent" value="yes" />
		<?php drop_print_onboarding_actions( __( 'Continue', 'drop-print' ) ); ?>
		<?php
		return;
	endif;
	?>
	<div class="dp-onboarding__note">
		<p class="dp-onboarding__note-title"><?php esc_html_e( 'What we found on your shop', 'drop-print' ); ?></p>
		<?php if ( ! $report['has_any'] ) : ?>
			<p><strong><?php esc_html_e( 'You have no shipping set up at all.', 'drop-print' ); ?></strong> <?php esc_html_e( 'No shipping zones, no methods. As things stand a customer can’t complete an order for anything physical — not just prints. This is worth fixing whether or not you use Drop Print.', 'drop-print' ); ?></p>
		<?php else : ?>
			<p>
				<?php
				printf(
					esc_html( _n( 'You have %d shipping zone, but Drop Print’s live rate isn’t on it.', 'You have %d shipping zones, but Drop Print’s live rate isn’t on them.', (int) $report['zone_count'], 'drop-print' ) ),
					(int) $report['zone_count']
				);
				?>
				<?php esc_html_e( 'Prints will be charged whatever flat rate you already have, which may be well under what delivery actually costs.', 'drop-print' ); ?>
			</p>
		<?php endif; ?>
	</div>
	<p><?php esc_html_e( 'May we add Drop Print live shipping for you?', 'drop-print' ); ?></p>
	<?php drop_print_onboarding_form_open( 'shipping' ); ?>
		<div class="dp-onboarding__choices">
			<label class="dp-onboarding__choice">
				<input type="radio" name="dp_shipping_consent" value="yes" checked />
				<span class="dp-onboarding__choice-body">
					<span class="dp-onboarding__choice-title"><?php esc_html_e( 'Yes, add it', 'drop-print' ); ?></span>
					<span class="dp-onboarding__choice-desc">
						<?php if ( ! $report['has_any'] ) : ?>
							<?php esc_html_e( 'Adds a rate covering everywhere you don’t already have a zone for, so prints can be bought and delivered anywhere.', 'drop-print' ); ?>
						<?php else : ?>
							<?php esc_html_e( 'Adds Drop Print’s live rate to your existing zones. Your own methods stay exactly as they are.', 'drop-print' ); ?>
						<?php endif; ?>
					</span>
				</span>
			</label>
			<label class="dp-onboarding__choice">
				<input type="radio" name="dp_shipping_consent" value="later" />
				<span class="dp-onboarding__choice-body">
					<span class="dp-onboarding__choice-title"><?php esc_html_e( 'Not now — remind me tomorrow', 'drop-print' ); ?></span>
					<span class="dp-onboarding__choice-desc">
						<?php if ( ! $report['has_any'] ) : ?>
							<?php esc_html_e( 'We won’t touch anything. Bear in mind your shop still can’t take an order for a physical product until shipping exists — ours or your own.', 'drop-print' ); ?>
						<?php else : ?>
							<?php esc_html_e( 'We won’t touch anything. Prints will keep using your existing rate, which may be less than delivery actually costs you.', 'drop-print' ); ?>
						<?php endif; ?>
					</span>
				</span>
			</label>
		</div>
		<div class="dp-onboarding__note" style="margin-top:20px;">
			<p class="dp-onboarding__note-title"><?php esc_html_e( 'How it behaves', 'drop-print' ); ?></p>
			<p><?php esc_html_e( 'On a cart of prints, our rate is the one shown. If a cart also holds your own non-Drop-Print products, those keep their normal shipping and the two are added together — nothing of yours is overridden.', 'drop-print' ); ?></p>
			<p><?php esc_html_e( 'If we ever can’t get a live quote, we say so rather than invent a price. Nothing is charged against a made-up shipping cost.', 'drop-print' ); ?></p>
		</div>
		<?php if ( ! $connected ) : ?>
			<p class="dp-onboarding__hint"><?php esc_html_e( 'We’ll add it once your account is connected at the end of this setup — a shipping rate we can’t yet quote would only show errors at your checkout.', 'drop-print' ); ?></p>
		<?php endif; ?>
	<?php
	drop_print_onboarding_actions( __( 'Continue', 'drop-print' ), '', true, __( 'Skip — remind me later', 'drop-print' ) );
	?>
	<?php
}
function drop_print_onboarding_render_step_colour( int $user_id, array $steps ) {
	?>
	<h1><?php esc_html_e( 'Work in Alden Delta', 'drop-print' ); ?></h1>
	<p><?php esc_html_e( 'For giclée printing we recommend working in Alden Delta — our own colour space, made for this. It is deliberately wider than sRGB and wider than Adobe RGB in the places fine-art papers actually reach, so nothing in your work is thrown away before the paper is even considered.', 'drop-print' ); ?></p>
	<div class="dp-onboarding__note">
		<p class="dp-onboarding__note-title"><?php esc_html_e( 'In Photoshop', 'drop-print' ); ?></p>
		<p><?php esc_html_e( 'Edit → Convert to Profile, and choose Alden Delta. Use CONVERT, not Assign — converting re-maps the colour so your work still looks like itself, where assigning keeps the numbers and changes their meaning, which will visibly shift your image.', 'drop-print' ); ?></p>
	</div>
	<p>
		<a href="<?php echo esc_url( drop_print_onboarding_icc_url() ); ?>" class="dp-btn dp-btn--ghost" download>
			<?php esc_html_e( 'Download AldenDelta.icc', 'drop-print' ); ?>
		</a>
	</p>
	<p class="dp-onboarding__hint"><?php esc_html_e( 'Install it, then restart your image editor so it appears in the profile list. You don’t have to do this now — your prints will still work.', 'drop-print' ); ?></p>
	<?php drop_print_onboarding_form_open( 'colour' ); ?>
	<?php drop_print_onboarding_actions( __( 'Continue', 'drop-print' ), drop_print_onboarding_next_step( $steps, 'colour' ), false, __( 'Skip', 'drop-print' ) ); ?>
	<?php
}
function drop_print_onboarding_get_payment_method_state( int $user_id ): array {
	$state = array( 'ok' => false, 'has_card' => false, 'brand' => null, 'last4' => null, 'exp_month' => null, 'exp_year' => null );
	if ( $user_id > 0 && drop_print_onboarding_user_is_connected( $user_id ) && function_exists( 'drop_print_make_server_request_for_ajax' ) ) {
		$response = drop_print_make_server_request_for_ajax( 'account-state', array( 'client_wp_user_id' => $user_id ), 'POST', 15, $user_id );
		if ( ! is_wp_error( $response ) && is_array( $response ) && isset( $response['payment_method'] ) && is_array( $response['payment_method'] ) ) {
			$payment_method = $response['payment_method'];
			$state          = array(
				'ok'        => true,
				'has_card'  => ! empty( $payment_method['has_card'] ),
				'brand'     => isset( $payment_method['brand'] ) ? sanitize_text_field( (string) $payment_method['brand'] ) : null,
				'last4'     => isset( $payment_method['last4'] ) ? sanitize_text_field( (string) $payment_method['last4'] ) : null,
				'exp_month' => isset( $payment_method['exp_month'] ) ? absint( $payment_method['exp_month'] ) : null,
				'exp_year'  => isset( $payment_method['exp_year'] ) ? absint( $payment_method['exp_year'] ) : null,
			);
			$settings                            = drop_print_get_user_settings( $user_id );
			$settings['payment_method_snapshot'] = array(
				'has_card'  => $state['has_card'],
				'brand'     => $state['brand'],
				'last4'     => $state['last4'],
				'exp_month' => $state['exp_month'],
				'exp_year'  => $state['exp_year'],
			);
			update_user_meta( $user_id, 'drop_print_user_settings', $settings );
		}
	}
	return (array) apply_filters( 'drop_print_onboarding_payment_method_state', $state, $user_id );
}
function drop_print_onboarding_render_step_billing( int $user_id, array $steps ) {
	$card_state     = drop_print_onboarding_get_payment_method_state( $user_id );
	$next_step      = drop_print_onboarding_next_step( $steps, 'billing' );
	$add_card_url   = function_exists( 'drop_print_network_base_url' ) ? drop_print_network_base_url( 'account' ) . '/my-account/add-payment-method/' : '';
	$card_error     = ! empty( $_GET['dp_card_error'] );
	$auto_prod_note = __( 'You can change this at any time in Drop Print → Settings.', 'drop-print' );
	?>
	<h1><?php esc_html_e( 'Automatic production', 'drop-print' ); ?></h1>
	<?php if ( ! empty( $card_state['ok'] ) && ! empty( $card_state['has_card'] ) ) : ?>
		<p><?php esc_html_e( 'You already have a card saved with Drop Print. Use it and paid orders go straight into production — you don’t have to approve each one by hand.', 'drop-print' ); ?></p>
		<div class="dp-onboarding__saved-card">
			<span class="dp-onboarding__saved-card-brand"><?php echo esc_html( $card_state['brand'] ? ucfirst( $card_state['brand'] ) : __( 'Card', 'drop-print' ) ); ?></span>
			<span class="dp-onboarding__saved-card-num">•••• <?php echo esc_html( (string) $card_state['last4'] ); ?></span>
			<?php if ( $card_state['exp_month'] && $card_state['exp_year'] ) : ?>
				<span class="dp-onboarding__saved-card-exp">
					<?php
					printf(
						esc_html__( 'Expires %1$s/%2$s', 'drop-print' ),
						esc_html( str_pad( (string) $card_state['exp_month'], 2, '0', STR_PAD_LEFT ) ),
						esc_html( (string) $card_state['exp_year'] )
					);
					?>
				</span>
			<?php endif; ?>
		</div>
		<p class="dp-onboarding__hint">
			<?php esc_html_e( 'Drop Print charges this card for printing and postage when one of your customers pays you. Nothing is charged for an order your customer never paid for.', 'drop-print' ); ?>
			<?php echo esc_html( $auto_prod_note ); ?>
		</p>
		<?php drop_print_onboarding_form_open( 'billing' ); ?>
		<input type="hidden" name="dp_enable_auto_production" value="1" />
		<?php
		drop_print_onboarding_actions(
			__( 'Use this card — send paid orders to print automatically', 'drop-print' ),
			$next_step,
			false,
			__( 'Not now — I’ll approve each order', 'drop-print' )
		);
		?>
	<?php elseif ( ! empty( $card_state['ok'] ) ) : ?>
		<?php if ( $card_error ) : ?>
			<div class="dp-onboarding__note">
				<p class="dp-onboarding__note-title"><?php esc_html_e( 'No card found', 'drop-print' ); ?></p>
				<p><?php esc_html_e( 'We checked again just now and couldn’t find a card on your Drop Print account, so automatic production hasn’t been switched on.', 'drop-print' ); ?></p>
			</div>
		<?php endif; ?>
		<p><?php esc_html_e( 'Add a card to Drop Print and paid orders can go straight into production — no approving each one by hand. Your customers never see this card; it’s how you pay Drop Print for printing and postage.', 'drop-print' ); ?></p>
		<?php if ( '' !== $add_card_url ) : ?>
			<p>
				<a class="dp-btn" href="<?php echo esc_url( $add_card_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Add a card at Drop Print', 'drop-print' ); ?></a>
			</p>
			<p class="dp-onboarding__hint"><?php esc_html_e( 'This opens in a new tab. Come back here when you’re done and press the button below.', 'drop-print' ); ?></p>
		<?php endif; ?>
		<?php drop_print_onboarding_form_open( 'billing' ); ?>
		<input type="hidden" name="dp_recheck" value="1" />
		<?php
		drop_print_onboarding_actions(
			__( 'I’ve added my card — check again', 'drop-print' ),
			$next_step,
			false,
			__( 'Skip — I’ll add a card later', 'drop-print' )
		);
		?>
	<?php else : ?>
		<p><?php esc_html_e( 'With a card saved at Drop Print, paid orders can go straight into production — no approving each one by hand.', 'drop-print' ); ?></p>
		<div class="dp-onboarding__note">
			<p class="dp-onboarding__note-title"><?php esc_html_e( 'We couldn’t check just now', 'drop-print' ); ?></p>
			<p><?php esc_html_e( 'We weren’t able to reach Drop Print to see whether you have a card saved, so we’ve left this alone rather than guess. Nothing here is holding up the rest of your setup.', 'drop-print' ); ?></p>
			<p><?php esc_html_e( 'You can add a card and switch automatic production on at any time in Drop Print → Settings.', 'drop-print' ); ?></p>
		</div>
		<?php drop_print_onboarding_form_open( 'billing' ); ?>
		<?php drop_print_onboarding_actions( __( 'Continue', 'drop-print' ), $next_step, false, __( 'Skip', 'drop-print' ) ); ?>
	<?php endif; ?>
	<?php
}
function drop_print_onboarding_render_step_connect( int $user_id, array $steps ) {
	if ( drop_print_use_headless_connect() ) {
		drop_print_onboarding_render_step_connect_headless( $user_id, $steps );
		return;
	}
	$attempt = isset( $_GET['attempt'] ) ? absint( wp_unslash( $_GET['attempt'] ) ) : 1;
	$attempt = max( 1, min( $attempt, DROP_PRINT_ONBOARDING_MAX_CONNECT_ATTEMPTS ) );
	$email    = (string) get_user_meta( $user_id, DROP_PRINT_ONBOARDING_EMAIL_META, true );
	$settings = drop_print_get_user_settings( $user_id );
	$connect_url = drop_print_build_connect_url(
		$user_id,
		array(
			'client_url'         => drop_print_onboarding_page_url( 'billing' ),
			'client_user_email'  => $email,
			'client_pref_currency' => $settings['billing_currency'] ?? '',
		)
	);
	$auth_host = $connect_url ? wp_parse_url( $connect_url, PHP_URL_HOST ) : '';
	?>
	<h1><?php esc_html_e( 'Connect your Drop Print account', 'drop-print' ); ?></h1>
	<p><?php esc_html_e( 'Last step. You’ll create your account (or sign in, if you already have one) and come straight back here.', 'drop-print' ); ?></p>
	<?php if ( $attempt > 1 ) : ?>
		<div class="dp-onboarding__note">
			<p class="dp-onboarding__note-title"><?php esc_html_e( 'Let’s try that again', 'drop-print' ); ?></p>
			<p>
				<?php
				printf(
					esc_html__( 'That didn’t complete. Attempt %1$d of %2$d.', 'drop-print' ),
					(int) $attempt,
					(int) DROP_PRINT_ONBOARDING_MAX_CONNECT_ATTEMPTS
				);
				?>
			</p>
		</div>
	<?php endif; ?>
	<?php if ( empty( $connect_url ) ) : ?>
		<div class="dp-onboarding__note">
			<p><?php esc_html_e( 'The Drop Print connection URL isn’t configured on this site. Please contact support.', 'drop-print' ); ?></p>
		</div>
	<?php else : ?>
		<p>
			<a href="<?php echo esc_url( $connect_url ); ?>" class="dp-btn"><?php esc_html_e( 'Create account &amp; connect', 'drop-print' ); ?></a>
		</p>
		<?php if ( $auth_host ) : ?>
		<p class="dp-onboarding__hint">
			<?php
			printf(
				esc_html__( 'You’ll be taken to %s to sign in securely, then returned here automatically.', 'drop-print' ),
				esc_html( $auth_host )
			);
			?>
		</p>
		<?php endif; ?>
	<?php endif; ?>
	<div class="dp-onboarding__actions">
		<div class="dp-onboarding__actions-right">
			<a class="dp-link-skip" href="<?php echo esc_url( drop_print_onboarding_page_url( 'done' ) ); ?>"><?php esc_html_e( 'I\'ll do this later', 'drop-print' ); ?></a>
		</div>
	</div>
	<?php
}
function drop_print_onboarding_handle_connect_post( int $user_id, array $steps ) {
	$action = isset( $_POST['dp_connect_action'] ) ? sanitize_key( wp_unslash( $_POST['dp_connect_action'] ) ) : '';
	if ( 'register' === $action ) {
		$email    = isset( $_POST['dp_email'] ) ? sanitize_email( wp_unslash( $_POST['dp_email'] ) ) : '';
		$currency = isset( $_POST['dp_billing_currency'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['dp_billing_currency'] ) ) ) : '';
		$terms_version = isset( $_POST['dp_terms_version'] ) ? sanitize_text_field( wp_unslash( $_POST['dp_terms_version'] ) ) : '';
		if ( empty( $_POST['dp_terms_accepted'] ) ) {
			wp_safe_redirect( drop_print_onboarding_page_url( 'connect', array( 'dp_connect_error' => rawurlencode( __( 'Please accept the Terms and Conditions to continue.', 'drop-print' ) ) ) ) );
			exit;
		}
		if ( '' === $email || ! is_email( $email ) ) {
			wp_safe_redirect( drop_print_onboarding_page_url( 'connect', array( 'dp_connect_error' => rawurlencode( __( 'Please enter a valid email address.', 'drop-print' ) ) ) ) );
			exit;
		}
		$result = drop_print_headless_register( $email, $currency, $terms_version, $user_id );
		if ( is_wp_error( $result ) && 'dp_account_exists' === $result->get_error_code() ) {
			wp_safe_redirect( drop_print_onboarding_page_url( 'connect', array( 'dp_mode' => 'pair', 'dp_email' => rawurlencode( $email ) ) ) );
			exit;
		}
		if ( is_wp_error( $result ) ) {
			wp_safe_redirect( drop_print_onboarding_page_url( 'connect', array( 'dp_connect_error' => rawurlencode( $result->get_error_message() ) ) ) );
			exit;
		}
		update_user_meta( $user_id, DROP_PRINT_ONBOARDING_EMAIL_META, $email );
		drop_print_headless_pair_request( $email );
		wp_safe_redirect( drop_print_onboarding_page_url( 'connect', array( 'dp_mode' => 'pair', 'dp_email' => rawurlencode( $email ), 'dp_code_sent' => 1, 'dp_registered' => 1 ) ) );
		exit;
	}
	if ( 'pair_request' === $action ) {
		$email = isset( $_POST['dp_email'] ) ? sanitize_email( wp_unslash( $_POST['dp_email'] ) ) : '';
		if ( '' !== $email && is_email( $email ) ) {
			drop_print_headless_pair_request( $email ); 
		}
		wp_safe_redirect( drop_print_onboarding_page_url( 'connect', array( 'dp_mode' => 'pair', 'dp_email' => rawurlencode( $email ), 'dp_code_sent' => 1 ) ) );
		exit;
	}
	if ( 'pair_confirm' === $action ) {
		$email = isset( $_POST['dp_email'] ) ? sanitize_email( wp_unslash( $_POST['dp_email'] ) ) : '';
		$code  = isset( $_POST['dp_pair_code'] ) ? sanitize_text_field( wp_unslash( $_POST['dp_pair_code'] ) ) : '';
		$result = drop_print_headless_pair_confirm( $email, $code, $user_id );
		if ( is_wp_error( $result ) ) {
			wp_safe_redirect( drop_print_onboarding_page_url( 'connect', array( 'dp_mode' => 'pair', 'dp_email' => rawurlencode( $email ), 'dp_connect_error' => rawurlencode( $result->get_error_message() ) ) ) );
			exit;
		}
		update_user_meta( $user_id, DROP_PRINT_ONBOARDING_EMAIL_META, $email );
		$stored = drop_print_store_connection_payload( $user_id, $result );
		if ( is_wp_error( $stored ) ) {
			wp_safe_redirect( drop_print_onboarding_page_url( 'connect', array( 'dp_mode' => 'pair', 'dp_email' => rawurlencode( $email ), 'dp_connect_error' => rawurlencode( $stored->get_error_message() ) ) ) );
			exit;
		}
		$next = drop_print_onboarding_next_step( $steps, 'connect' );
		wp_safe_redirect( '' === $next ? drop_print_onboarding_page_url( 'done' ) : drop_print_onboarding_page_url( $next ) );
		exit;
	}
	wp_safe_redirect( drop_print_onboarding_page_url( 'connect' ) );
	exit;
}
function drop_print_onboarding_render_step_connect_headless( int $user_id, array $steps ) {
	$mode          = isset( $_GET['dp_mode'] ) ? sanitize_key( wp_unslash( $_GET['dp_mode'] ) ) : '';
	$prefill_email = isset( $_GET['dp_email'] )
		? sanitize_email( rawurldecode( wp_unslash( $_GET['dp_email'] ) ) )
		: (string) get_user_meta( $user_id, DROP_PRINT_ONBOARDING_EMAIL_META, true );
	$error         = isset( $_GET['dp_connect_error'] ) ? sanitize_text_field( rawurldecode( wp_unslash( $_GET['dp_connect_error'] ) ) ) : '';
	$code_sent     = ! empty( $_GET['dp_code_sent'] );
	$just_registered = ! empty( $_GET['dp_registered'] );
	if ( '' === $prefill_email ) {
		$wp_user       = wp_get_current_user();
		$prefill_email = $wp_user ? $wp_user->user_email : '';
	}
	?>
	<h1><?php esc_html_e( 'Connect your Drop Print account', 'drop-print' ); ?></h1>
	<?php if ( '' !== $error ) : ?>
		<div class="dp-onboarding__note dp-onboarding__note--error">
			<p><?php echo esc_html( $error ); ?></p>
		</div>
	<?php endif; ?>
	<?php if ( 'pair' === $mode ) : ?>
		<?php if ( $just_registered ) : ?>
			<p><?php esc_html_e( 'Your Drop Print account has been created. We\'ve emailed you a one-time code to finish connecting it here - your Drop Print password is never needed on this site.', 'drop-print' ); ?></p>
		<?php else : ?>
			<p><?php esc_html_e( 'This email already has a Drop Print account. We\'ll email you a one-time code to reconnect it here - your Drop Print password is never needed on this site.', 'drop-print' ); ?></p>
		<?php endif; ?>
		<?php if ( $code_sent ) : ?>
			<p class="dp-onboarding__hint">
				<?php
				if ( $just_registered ) {
					esc_html_e( 'A code has been sent to your email. Enter it below.', 'drop-print' );
				} else {
					esc_html_e( 'If an account exists for that email, a code has been sent. Enter it below.', 'drop-print' );
				}
				?>
			</p>
			<?php drop_print_onboarding_form_open( 'connect' ); ?>
				<input type="hidden" name="dp_connect_action" value="pair_confirm" />
				<input type="hidden" name="dp_email" value="<?php echo esc_attr( $prefill_email ); ?>" />
				<div class="dp-onboarding__field">
					<label class="dp-onboarding__label" for="dp_pair_code"><?php esc_html_e( 'Code from your email', 'drop-print' ); ?></label>
					<input type="text" id="dp_pair_code" name="dp_pair_code" autocomplete="one-time-code" maxlength="8" style="text-transform:uppercase;letter-spacing:2px;" required />
				</div>
				<?php drop_print_onboarding_actions( __( 'Reconnect', 'drop-print' ) ); ?>
		<?php else : ?>
			<?php drop_print_onboarding_form_open( 'connect' ); ?>
				<input type="hidden" name="dp_connect_action" value="pair_request" />
				<div class="dp-onboarding__field">
					<label class="dp-onboarding__label" for="dp_email_pair"><?php esc_html_e( 'Your Drop Print account email', 'drop-print' ); ?></label>
					<input type="email" id="dp_email_pair" name="dp_email" value="<?php echo esc_attr( $prefill_email ); ?>" autocomplete="email" required />
				</div>
				<?php drop_print_onboarding_actions( __( 'Email me a code', 'drop-print' ) ); ?>
		<?php endif; ?>
	<?php else :
		$terms = drop_print_headless_fetch_terms();
		?>
		<p><?php esc_html_e( 'Last step. Create your Drop Print account without leaving this page.', 'drop-print' ); ?></p>
		<?php drop_print_onboarding_form_open( 'connect' ); ?>
			<input type="hidden" name="dp_connect_action" value="register" />
			<div class="dp-onboarding__field">
				<label class="dp-onboarding__label" for="dp_connect_email"><?php esc_html_e( 'Email for your Drop Print account', 'drop-print' ); ?></label>
				<input type="email" id="dp_connect_email" name="dp_email" value="<?php echo esc_attr( $prefill_email ); ?>" autocomplete="email" required />
			</div>
			<div class="dp-onboarding__field">
				<label class="dp-onboarding__label" for="dp_connect_currency"><?php esc_html_e( 'Billing currency', 'drop-print' ); ?></label>
				<?php
				$settings = drop_print_get_user_settings( $user_id );
				$selected = $settings['billing_currency'] ?? '';
				?>
				<select id="dp_connect_currency" name="dp_billing_currency" required>
					<?php foreach ( ( function_exists( 'get_woocommerce_currencies' ) ? get_woocommerce_currencies() : array() ) as $code => $label ) : ?>
						<option value="<?php echo esc_attr( $code ); ?>" <?php selected( $selected, $code ); ?>><?php echo esc_html( $label . ' (' . $code . ')' ); ?></option>
					<?php endforeach; ?>
				</select>
				<p class="dp-onboarding__hint"><?php esc_html_e( 'This cannot be changed later.', 'drop-print' ); ?></p>
			</div>
			<?php if ( is_wp_error( $terms ) ) : ?>
				<div class="dp-onboarding__note dp-onboarding__note--error">
					<p><?php echo esc_html( $terms->get_error_message() ); ?></p>
				</div>
			<?php else : ?>
				<input type="hidden" name="dp_terms_version" value="<?php echo esc_attr( $terms['version'] ); ?>" />
				<div class="dp-onboarding__terms" style="max-height:220px;overflow-y:auto;border:1px solid #444;padding:12px;margin-bottom:12px;">
					<?php echo wp_kses_post( $terms['html'] ); ?>
				</div>
				<div class="dp-onboarding__field">
					<label>
						<input type="checkbox" name="dp_terms_accepted" value="1" required />
						<?php esc_html_e( 'I accept the Terms and Conditions', 'drop-print' ); ?>
					</label>
				</div>
			<?php endif; ?>
			<?php drop_print_onboarding_actions( __( 'Create account & connect', 'drop-print' ) ); ?>
		<p class="dp-onboarding__hint">
			<a href="<?php echo esc_url( drop_print_onboarding_page_url( 'connect', array( 'dp_mode' => 'pair' ) ) ); ?>"><?php esc_html_e( 'Already have a Drop Print account? Reconnect it instead.', 'drop-print' ); ?></a>
		</p>
	<?php endif; ?>
	<?php
}
function drop_print_onboarding_render_step_done( int $user_id, array $steps ) {
	$connected = drop_print_onboarding_user_is_connected( $user_id );
	$settings  = drop_print_get_user_settings( $user_id );
	if ( empty( $settings['onboarding_completed_version'] ) ) {
		$settings['onboarding_completed_version'] = defined( 'DROP_PRINT_VERSION' ) ? DROP_PRINT_VERSION : '2.0';
		update_user_meta( $user_id, 'drop_print_user_settings', $settings );
	}
	$attempt  = isset( $_GET['attempt'] ) ? absint( wp_unslash( $_GET['attempt'] ) ) : 1;
	$raw_unit = get_option( 'woocommerce_dimension_unit', 'in' );
	$unit_map = array( 'cm' => 'cm', 'm' => 'cm', 'mm' => 'cm', 'in' => 'in', 'yd' => 'in', 'ft' => 'in' );
	$units    = ( 'cm' === ( $unit_map[ $raw_unit ] ?? 'in' ) ) ? __( 'Centimetres', 'drop-print' ) : __( 'Inches', 'drop-print' );
	$intents  = array(
		'photo'     => __( 'Photography', 'drop-print' ),
		'art_repro' => __( 'Art reproduction', 'drop-print' ),
		'ask'       => __( 'Ask each time', 'drop-print' ),
	);
	$intent   = $intents[ $settings['default_intent'] ?? 'ask' ] ?? $intents['ask'];
	$location = $settings['default_customer_location'] ?? '';
	if ( '' !== $location && function_exists( 'WC' ) && WC() && isset( WC()->countries ) ) {
		$all      = WC()->countries->get_countries();
		$location = $all[ $location ] ?? $location;
	}
	?>
	<?php if ( $connected ) : ?>
		<div class="dp-onboarding__done-mark">✓</div>
		<h1><?php esc_html_e( 'You’re all set', 'drop-print' ); ?></h1>
		<p><?php esc_html_e( 'Your account is connected and live shipping is switched on. Upload artwork to any product and Drop Print will handle the printing and delivery.', 'drop-print' ); ?></p>
	<?php else : ?>
		<h1><?php esc_html_e( 'Nearly there', 'drop-print' ); ?></h1>
		<p><?php esc_html_e( 'Your preferences are saved, but your account isn’t connected yet — so we can’t print or ship anything for you until it is.', 'drop-print' ); ?></p>
	<?php endif; ?>
	<ul class="dp-onboarding__summary">
		<?php if ( ! empty( $settings['billing_currency'] ) ) : ?>
			<li><span><?php esc_html_e( 'Currency', 'drop-print' ); ?></span><span><?php echo esc_html( $settings['billing_currency'] ); ?></span></li>
		<?php endif; ?>
		<li><span><?php esc_html_e( 'Units', 'drop-print' ); ?></span><span><?php echo esc_html( $units ); ?></span></li>
		<li><span><?php esc_html_e( 'Intent', 'drop-print' ); ?></span><span><?php echo esc_html( $intent ); ?></span></li>
		<?php if ( '' !== $location ) : ?>
			<li><span><?php esc_html_e( 'Customers', 'drop-print' ); ?></span><span><?php echo esc_html( $location ); ?></span></li>
		<?php endif; ?>
	</ul>
	<div class="dp-onboarding__actions">
		<div class="dp-onboarding__actions-right">
			<?php if ( ! $connected && isset( $steps['connect'] ) ) : ?>
				<?php if ( $attempt < DROP_PRINT_ONBOARDING_MAX_CONNECT_ATTEMPTS ) : ?>
					<a class="dp-link-skip" href="<?php echo esc_url( admin_url( 'admin.php?page=drop-print-settings' ) ); ?>"><?php esc_html_e( 'Later', 'drop-print' ); ?></a>
					<a class="dp-btn" href="<?php echo esc_url( drop_print_onboarding_page_url( 'connect', array( 'attempt' => $attempt + 1 ) ) ); ?>"><?php esc_html_e( 'Try connecting again', 'drop-print' ); ?></a>
				<?php else : ?>
					<a class="dp-btn" href="<?php echo esc_url( admin_url( 'admin.php?page=drop-print-settings' ) ); ?>"><?php esc_html_e( 'Go to settings', 'drop-print' ); ?></a>
				<?php endif; ?>
			<?php else : ?>
				<a class="dp-btn" href="<?php echo esc_url( admin_url( 'edit.php?post_type=product' ) ); ?>"><?php esc_html_e( 'Go to my products', 'drop-print' ); ?></a>
			<?php endif; ?>
		</div>
	</div>
	<?php if ( ! $connected && $attempt >= DROP_PRINT_ONBOARDING_MAX_CONNECT_ATTEMPTS ) : ?>
		<p class="dp-onboarding__hint" style="margin-top:16px;">
			<?php esc_html_e( 'Still not connecting? You can retry any time from the Drop Print settings page, or contact us and we’ll sort it out with you.', 'drop-print' ); ?>
		</p>
	<?php endif; ?>
	<?php
}
