<?php
/**
 * Onboarding wizard view.
 *
 * @var array<string, mixed> $mail_doctor_settings
 * @var array<string, mixed> $mail_doctor_status
 * @var array<string, array<string, mixed>> $mail_doctor_providers
 * @var array<string, array<string, string>> $mail_doctor_dns
 *
 * @package MailDoctor
 */

use MailDoctor\Onboarding;

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

$mail_doctor_step_labels = array(
	1 => __( 'Choose Provider', 'mail-doctor' ),
	2 => __( 'SMTP Credentials', 'mail-doctor' ),
	3 => __( 'Domain Health', 'mail-doctor' ),
	4 => __( 'Review & Finish', 'mail-doctor' ),
);

$mail_doctor_current_step = (int) $mail_doctor_status['current_step'];
$mail_doctor_main_id      = 'mail-doctor-onboarding';
?>
<div class="wrap mail-doctor-onboarding">
	<a class="mail-doctor__skip-link" href="#<?php echo esc_attr( $mail_doctor_main_id ); ?>"><?php esc_html_e( 'Skip to Mail Doctor setup content', 'mail-doctor' ); ?></a>
	<h1><?php esc_html_e( 'Mail Doctor Setup', 'mail-doctor' ); ?></h1>

	<?php settings_errors( 'order_mail_doctor' ); ?>

	<ol class="mail-doctor-onboarding__steps" role="list" aria-label="<?php esc_attr_e( 'Onboarding progress', 'mail-doctor' ); ?>">
		<?php foreach ( $mail_doctor_step_labels as $mail_doctor_number => $mail_doctor_label ) : ?>
			<li class="<?php echo esc_attr( $mail_doctor_number === $mail_doctor_current_step ? 'is-current' : ( $mail_doctor_number < $mail_doctor_current_step ? 'is-complete' : '' ) ); ?>">
				<span class="mail-doctor-onboarding__step-number"><?php echo (int) $mail_doctor_number; ?></span>
				<span class="mail-doctor-onboarding__step-label"><?php echo esc_html( $mail_doctor_label ); ?></span>
			</li>
		<?php endforeach; ?>
	</ol>

	<div class="mail-doctor-onboarding__content" id="<?php echo esc_attr( $mail_doctor_main_id ); ?>" tabindex="-1">
		<?php if ( 1 === $mail_doctor_current_step ) : ?>
			<form method="post">
				<?php wp_nonce_field( 'mail_doctor_onboarding_step' ); ?>
				<input type="hidden" name="mail_doctor_onboarding_step" value="1" />
				<h2><?php esc_html_e( 'Select Your Email Provider', 'mail-doctor' ); ?></h2>
				<p><?php esc_html_e( 'Choose the service you use to send email. We will prefill recommended SMTP settings based on your selection.', 'mail-doctor' ); ?></p>
				<div class="mail-doctor-onboarding__providers" role="radiogroup" aria-label="<?php esc_attr_e( 'Available email providers', 'mail-doctor' ); ?>">
					<?php foreach ( $mail_doctor_providers as $mail_doctor_key => $mail_doctor_provider ) : ?>
						<label class="mail-doctor-onboarding__provider">
							<input type="radio" name="onboarding_provider" value="<?php echo esc_attr( $mail_doctor_key ); ?>" <?php checked( $mail_doctor_status['provider'], $mail_doctor_key ); ?> required aria-describedby="mail-doctor-provider-<?php echo esc_attr( $mail_doctor_key ); ?>-help" />
							<span class="mail-doctor-onboarding__provider-name"><?php echo esc_html( $mail_doctor_provider['label'] ); ?></span>
							<span id="mail-doctor-provider-<?php echo esc_attr( $mail_doctor_key ); ?>-help" class="mail-doctor-onboarding__provider-help"><?php echo esc_html( $mail_doctor_provider['help'] ); ?></span>
						</label>
					<?php endforeach; ?>
				</div>
				<p class="mail-doctor-onboarding__actions">
					<button type="submit" class="button button-primary"><?php esc_html_e( 'Continue', 'mail-doctor' ); ?></button>
					<a class="button-link" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'mail_doctor_onboarding_skip', 1, Onboarding::step_url( 1 ) ), 'mail_doctor_onboarding_skip' ) ); ?>">
						<?php esc_html_e( 'Skip setup', 'mail-doctor' ); ?>
					</a>
				</p>
			</form>
		<?php elseif ( 2 === $mail_doctor_current_step ) : ?>
			<form method="post">
				<?php wp_nonce_field( 'mail_doctor_onboarding_step' ); ?>
				<input type="hidden" name="mail_doctor_onboarding_step" value="2" />
				<h2><?php esc_html_e( 'Enter SMTP Credentials', 'mail-doctor' ); ?></h2>
				<p><?php esc_html_e( 'Use the values supplied by your email provider. Passwords are stored securely in the WordPress database.', 'mail-doctor' ); ?></p>
				<input type="hidden" id="omd-onboarding-provider" value="<?php echo esc_attr( $mail_doctor_status['provider'] ?? '' ); ?>" />
				<table class="form-table">
					<tr>
						<th scope="row"><label for="omd-onboarding-transport"><?php esc_html_e( 'Delivery method', 'mail-doctor' ); ?></label></th>
						<td>
							<select id="omd-onboarding-transport" name="transport">
								<?php foreach ( $mail_doctor_transport_options as $mail_doctor_transport_key => $mail_doctor_transport_label ) : ?>
									<option value="<?php echo esc_attr( $mail_doctor_transport_key ); ?>" <?php selected( $mail_doctor_settings['transport'] ?? 'smtp', $mail_doctor_transport_key ); ?>><?php echo esc_html( $mail_doctor_transport_label ); ?></option>
								<?php endforeach; ?>
							</select>
							<p class="description"><?php esc_html_e( 'Switch between SMTP or provider APIs. These values can be refined later.', 'mail-doctor' ); ?></p>
						</td>
					</tr>
				<tr class="mail-doctor-transport-field mail-doctor-transport-smtp">
					<th scope="row"><label for="omd-onboarding-smtp-host"><?php esc_html_e( 'SMTP Host', 'mail-doctor' ); ?></label></th>
					<td><input type="text" id="omd-onboarding-smtp-host" name="smtp_host" value="<?php echo esc_attr( $mail_doctor_settings['smtp_host'] ?? '' ); ?>" class="regular-text mail-doctor-transport-field mail-doctor-transport-smtp" /></td>
				</tr>
				<tr class="mail-doctor-transport-field mail-doctor-transport-smtp">
					<th scope="row"><label for="omd-onboarding-smtp-port"><?php esc_html_e( 'SMTP Port', 'mail-doctor' ); ?></label></th>
					<td><input type="number" id="omd-onboarding-smtp-port" name="smtp_port" value="<?php echo esc_attr( (string) ( $mail_doctor_settings['smtp_port'] ?? 587 ) ); ?>" min="1" class="mail-doctor-transport-field mail-doctor-transport-smtp" /></td>
				</tr>
				<tr class="mail-doctor-transport-field mail-doctor-transport-smtp">
					<th scope="row"><?php esc_html_e( 'Encryption', 'mail-doctor' ); ?></th>
					<td>
						<select name="smtp_encryption" class="mail-doctor-transport-field mail-doctor-transport-smtp">
								<?php foreach ( array( 'none', 'tls', 'ssl' ) as $mail_doctor_encryption ) : ?>
									<option value="<?php echo esc_attr( $mail_doctor_encryption ); ?>" <?php selected( $mail_doctor_settings['smtp_encryption'] ?? 'tls', $mail_doctor_encryption ); ?>>
										<?php echo esc_html( strtoupper( $mail_doctor_encryption ) ); ?>
									</option>
								<?php endforeach; ?>
							</select>
						</td>
					</tr>
				<tr class="mail-doctor-transport-field mail-doctor-transport-smtp">
					<th scope="row"><label for="omd-onboarding-username"><?php esc_html_e( 'SMTP Username', 'mail-doctor' ); ?></label></th>
					<td><input type="text" id="omd-onboarding-username" name="smtp_username" value="<?php echo esc_attr( $mail_doctor_settings['smtp_username'] ?? '' ); ?>" class="regular-text mail-doctor-transport-field mail-doctor-transport-smtp" autocomplete="off" /></td>
				</tr>
				<tr class="mail-doctor-transport-field mail-doctor-transport-smtp">
					<th scope="row"><label for="omd-onboarding-password"><?php esc_html_e( 'SMTP Password / API Key', 'mail-doctor' ); ?></label></th>
					<td><input type="password" id="omd-onboarding-password" name="smtp_password" value="" class="regular-text mail-doctor-transport-field mail-doctor-transport-smtp" autocomplete="new-password" /></td>
				</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-mailgun">
						<th scope="row"><label for="omd-onboarding-mailgun-key"><?php esc_html_e( 'Mailgun API key', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-mailgun-key" name="mailgun_api_key" value="<?php echo esc_attr( $mail_doctor_settings['mailgun_api_key'] ?? '' ); ?>" class="regular-text" autocomplete="off" />
							<p class="description"><?php esc_html_e( 'Use your Mailgun private API key.', 'mail-doctor' ); ?></p>
						</td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-mailgun">
						<th scope="row"><label for="omd-onboarding-mailgun-domain"><?php esc_html_e( 'Mailgun domain', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-mailgun-domain" name="mailgun_api_domain" value="<?php echo esc_attr( $mail_doctor_settings['mailgun_api_domain'] ?? '' ); ?>" class="regular-text" placeholder="mg.example.com" />
							<p class="description"><?php esc_html_e( 'Enter the sending domain configured in Mailgun.', 'mail-doctor' ); ?></p>
						</td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-mailgun">
						<th scope="row"><label for="omd-onboarding-mailgun-endpoint"><?php esc_html_e( 'Mailgun API endpoint', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-mailgun-endpoint" name="mailgun_api_endpoint" value="<?php echo esc_attr( $mail_doctor_settings['mailgun_api_endpoint'] ?? 'api.mailgun.net' ); ?>" class="regular-text" placeholder="api.mailgun.net" /></td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-sendgrid">
						<th scope="row"><label for="omd-onboarding-sendgrid-key"><?php esc_html_e( 'SendGrid API key', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-sendgrid-key" name="sendgrid_api_key" value="<?php echo esc_attr( $mail_doctor_settings['sendgrid_api_key'] ?? '' ); ?>" class="regular-text" autocomplete="off" /></td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-postmark">
						<th scope="row"><label for="omd-onboarding-postmark-token"><?php esc_html_e( 'Postmark server token', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-postmark-token" name="postmark_api_token" value="<?php echo esc_attr( $mail_doctor_settings['postmark_api_token'] ?? '' ); ?>" class="regular-text" autocomplete="off" /></td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-sparkpost">
						<th scope="row"><label for="omd-onboarding-sparkpost-key"><?php esc_html_e( 'SparkPost API key', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-sparkpost-key" name="sparkpost_api_key" value="<?php echo esc_attr( $mail_doctor_settings['sparkpost_api_key'] ?? '' ); ?>" class="regular-text" autocomplete="off" /></td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-sparkpost">
						<th scope="row"><label for="omd-onboarding-sparkpost-endpoint"><?php esc_html_e( 'SparkPost API base', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-sparkpost-endpoint" name="sparkpost_api_endpoint" value="<?php echo esc_attr( $mail_doctor_settings['sparkpost_api_endpoint'] ?? 'https://api.sparkpost.com' ); ?>" class="regular-text" placeholder="https://api.sparkpost.com" /></td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-ses">
						<th scope="row"><label for="omd-onboarding-ses-key"><?php esc_html_e( 'SES access key ID', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-ses-key" name="ses_api_key" value="<?php echo esc_attr( $mail_doctor_settings['ses_api_key'] ?? '' ); ?>" class="regular-text" autocomplete="off" /></td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-ses">
						<th scope="row"><label for="omd-onboarding-ses-secret"><?php esc_html_e( 'SES secret access key', 'mail-doctor' ); ?></label></th>
						<td><input type="password" id="omd-onboarding-ses-secret" name="ses_api_secret" value="" class="regular-text" autocomplete="new-password"<?php echo esc_attr( empty( $mail_doctor_settings['ses_api_secret'] ) ? ' required' : '' ); ?> /></td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-ses">
						<th scope="row"><label for="omd-onboarding-ses-token"><?php esc_html_e( 'SES session token', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-ses-token" name="ses_api_session_token" value="<?php echo esc_attr( $mail_doctor_settings['ses_api_session_token'] ?? '' ); ?>" class="regular-text" autocomplete="off" /></td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-ses">
						<th scope="row"><label for="omd-onboarding-ses-region"><?php esc_html_e( 'SES region', 'mail-doctor' ); ?></label></th>
						<td>
							<select id="omd-onboarding-ses-region" name="ses_api_region">
								<?php foreach ( $mail_doctor_ses_regions as $mail_doctor_region_code => $mail_doctor_region_label ) : ?>
									<option value="<?php echo esc_attr( $mail_doctor_region_code ); ?>" <?php selected( $mail_doctor_settings['ses_api_region'] ?? 'us-east-1', $mail_doctor_region_code ); ?>><?php echo esc_html( $mail_doctor_region_label ); ?></option>
								<?php endforeach; ?>
							</select>
						</td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-generic">
						<th scope="row"><label for="omd-onboarding-generic-endpoint"><?php esc_html_e( 'Endpoint URL', 'mail-doctor' ); ?></label></th>
						<td><input type="url" id="omd-onboarding-generic-endpoint" name="generic_api_endpoint" value="<?php echo esc_attr( $mail_doctor_settings['generic_api_endpoint'] ?? '' ); ?>" class="regular-text" placeholder="https://example.com/maildoctor-webhook" /></td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-generic">
						<th scope="row"><label for="omd-onboarding-generic-method"><?php esc_html_e( 'HTTP method', 'mail-doctor' ); ?></label></th>
						<td>
							<select id="omd-onboarding-generic-method" name="generic_api_method">
								<option value="POST" <?php selected( strtoupper( $mail_doctor_settings['generic_api_method'] ?? 'POST' ), 'POST' ); ?>><?php esc_html_e( 'POST', 'mail-doctor' ); ?></option>
								<option value="PUT" <?php selected( strtoupper( $mail_doctor_settings['generic_api_method'] ?? 'POST' ), 'PUT' ); ?>><?php esc_html_e( 'PUT', 'mail-doctor' ); ?></option>
							</select>
						</td>
					</tr>
					<tr class="mail-doctor-transport-field mail-doctor-transport-generic">
						<th scope="row"><label for="omd-onboarding-generic-token"><?php esc_html_e( 'Bearer token', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-generic-token" name="generic_api_auth_token" value="<?php echo esc_attr( $mail_doctor_settings['generic_api_auth_token'] ?? '' ); ?>" class="regular-text" autocomplete="off" /></td>
					</tr>
					<tr>
						<th scope="row"><label for="omd-onboarding-from-email"><?php esc_html_e( 'From Email', 'mail-doctor' ); ?></label></th>
						<td><input type="email" id="omd-onboarding-from-email" name="from_email" value="<?php echo esc_attr( $mail_doctor_settings['from_email'] ?? '' ); ?>" class="regular-text" required /></td>
					</tr>
					<tr>
						<th scope="row"><label for="omd-onboarding-from-name"><?php esc_html_e( 'From Name', 'mail-doctor' ); ?></label></th>
						<td><input type="text" id="omd-onboarding-from-name" name="from_name" value="<?php echo esc_attr( $mail_doctor_settings['from_name'] ?? get_bloginfo( 'name' ) ); ?>" class="regular-text" /></td>
					</tr>
				</table>

				<p class="mail-doctor-onboarding__actions mail-doctor-onboarding__actions--test">
				<button type="button" class="button button-secondary mail-doctor-test-connection"><?php esc_html_e( 'Test connection', 'mail-doctor' ); ?></button>
				</p>

				<div class="mail-doctor-test-results" aria-live="polite"></div>

				<p class="mail-doctor-onboarding__actions">
					<a class="button button-secondary" href="<?php echo esc_url( Onboarding::step_url( 1 ) ); ?>"><?php esc_html_e( 'Back', 'mail-doctor' ); ?></a>
					<button type="submit" class="button button-primary"><?php esc_html_e( 'Continue', 'mail-doctor' ); ?></button>
				</p>
			</form>
		<?php elseif ( 3 === $mail_doctor_current_step ) : ?>
			<form method="post">
				<?php wp_nonce_field( 'mail_doctor_onboarding_step' ); ?>
				<input type="hidden" name="mail_doctor_onboarding_step" value="3" />
				<h2><?php esc_html_e( 'Check Domain Health', 'mail-doctor' ); ?></h2>
				<p><?php esc_html_e( 'We looked for SPF, DKIM, and DMARC records on your sending domain. Configure missing records for better deliverability.', 'mail-doctor' ); ?></p>

				<div class="mail-doctor-onboarding__dns">
					<?php foreach ( $mail_doctor_dns as $mail_doctor_key => $mail_doctor_record ) : ?>
						<article class="mail-doctor-onboarding__dns-item mail-doctor-onboarding__dns-item--<?php echo esc_attr( $mail_doctor_record['status'] ?? 'unknown' ); ?>">
							<h3><?php echo esc_html( strtoupper( $mail_doctor_key ) ); ?></h3>
							<p><?php echo esc_html( $mail_doctor_record['message'] ?? '' ); ?></p>
							<?php if ( ! empty( $mail_doctor_record['value'] ) ) : ?>
								<pre><?php echo esc_html( $mail_doctor_record['value'] ); ?></pre>
							<?php endif; ?>
						</article>
					<?php endforeach; ?>
				</div>

				<p class="description"><?php esc_html_e( 'Need help? Open the Mail Doctor Guides tab after setup for DNS walkthroughs.', 'mail-doctor' ); ?></p>

				<p class="mail-doctor-onboarding__actions">
					<a class="button button-secondary" href="<?php echo esc_url( Onboarding::step_url( 2 ) ); ?>"><?php esc_html_e( 'Back', 'mail-doctor' ); ?></a>
					<button type="submit" class="button button-primary"><?php esc_html_e( 'Looks good', 'mail-doctor' ); ?></button>
				</p>
			</form>
		<?php elseif ( 4 === $mail_doctor_current_step ) : ?>
			<form method="post">
				<?php wp_nonce_field( 'mail_doctor_onboarding_step' ); ?>
				<input type="hidden" name="mail_doctor_onboarding_step" value="4" />
				<h2><?php esc_html_e( 'Review & Finish', 'mail-doctor' ); ?></h2>
				<p><?php esc_html_e( 'Here is a summary of your configuration. Click Finish to go to the Mail Doctor dashboard.', 'mail-doctor' ); ?></p>

				<table class="widefat fixed striped mail-doctor-onboarding__summary">
					<tbody>
						<tr>
							<th><?php esc_html_e( 'SMTP Host', 'mail-doctor' ); ?></th>
							<td><?php echo esc_html( $mail_doctor_settings['smtp_host'] ?? '' ); ?></td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Port', 'mail-doctor' ); ?></th>
							<td><?php echo esc_html( (string) ( $mail_doctor_settings['smtp_port'] ?? '' ) ); ?></td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Encryption', 'mail-doctor' ); ?></th>
							<td><?php echo esc_html( strtoupper( $mail_doctor_settings['smtp_encryption'] ?? 'tls' ) ); ?></td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Username', 'mail-doctor' ); ?></th>
							<td><?php echo esc_html( $mail_doctor_settings['smtp_username'] ?? '' ); ?></td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'From Email', 'mail-doctor' ); ?></th>
							<td><?php echo esc_html( $mail_doctor_settings['from_email'] ?? '' ); ?></td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'From Name', 'mail-doctor' ); ?></th>
							<td><?php echo esc_html( $mail_doctor_settings['from_name'] ?? '' ); ?></td>
						</tr>
						<tr>
							<th><?php esc_html_e( 'Override Scope', 'mail-doctor' ); ?></th>
							<td><?php echo 'all' === ( $mail_doctor_settings['override_scope'] ?? 'all' ) ? esc_html__( 'All emails', 'mail-doctor' ) : esc_html__( 'WooCommerce only', 'mail-doctor' ); ?></td>
						</tr>
					</tbody>
				</table>

				<p class="mail-doctor-onboarding__actions">
					<a class="button button-secondary" href="<?php echo esc_url( Onboarding::step_url( 3 ) ); ?>"><?php esc_html_e( 'Back', 'mail-doctor' ); ?></a>
					<button type="submit" class="button button-primary"><?php esc_html_e( 'Finish', 'mail-doctor' ); ?></button>
				</p>
			</form>
		<?php endif; ?>
	</div>
</div>
