<?php
/**
 * PSP Step
 *
 * Shows the currently configured payment service provider (or "none") and
 * provides a quick link to the PSP settings tab.  Opening settings in a new
 * tab keeps the wizard open so the user can come back and continue.
 *
 * @package AmbassadorHub\Admin\Wizard\Steps
 */

namespace AmbassadorHub\Admin\Wizard\Steps;

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

use AmbassadorHub\Admin\Wizard\AbstractWizardStep;
use AmbassadorHub\Connectors\ConnectorRegistry;

/**
 * Wizard step for connecting a payment provider.
 */
final class PspStep extends AbstractWizardStep {

	/**
	 * {@inheritdoc}
	 */
	public function get_id(): string {
		return 'psp';
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_title(): string {
		return __( 'Connect a payment provider', 'ambassadorhub' );
	}

	/**
	 * {@inheritdoc}
	 */
	public function is_skippable(): bool {
		return true;
	}

	/**
	 * {@inheritdoc}
	 */
	public function is_alertable(): bool {
		return true;
	}

	/**
	 * {@inheritdoc}
	 */
	public function is_done(): bool {
		return '' !== get_option( 'ambh_active_connector_psp', '' );
	}

	/**
	 * {@inheritdoc}
	 *
	 * Persists the mode choice:
	 * - 'test' → activates the Free connector immediately.
	 * - 'real' → clears Free mode so the dashboard shows PSP as pending;
	 *            leaves any already-configured real PSP untouched.
	 */
	public function handle( array $post_data ): ?string {
		$choice = sanitize_key( $post_data['ambh_psp_choice'] ?? '' );

		if ( 'test' === $choice ) {
			update_option( 'ambh_active_connector_psp', 'free' );
		} elseif ( 'real' === $choice ) {
			// Switching away from test mode — clear Free so the admin must
			// configure a real PSP from the settings tab.
			if ( 'free' === get_option( 'ambh_active_connector_psp', '' ) ) {
				update_option( 'ambh_active_connector_psp', '' );
			}
		}

		return null; // Advance to next step.
	}

	/**
	 * {@inheritdoc}
	 */
	public function render(): void {
		$active_id        = (string) get_option( 'ambh_active_connector_psp', '' );
		$is_test_mode     = 'free' === $active_id;
		$is_real_psp      = '' !== $active_id && ! $is_test_mode;
		$selected         = $is_test_mode ? 'test' : 'real';
		$psp_settings_url = admin_url( 'admin.php?page=ambassadorhub-settings&tab=psp' );

		// Resolve display label for the currently active real PSP.
		$psp_label = '';
		if ( $is_real_psp ) {
			$connector = ConnectorRegistry::instance()->get( $active_id );
			$psp_label = $connector ? $connector::get_label() : esc_html( $active_id );
		}
		?>
		<p>
			<?php esc_html_e( 'A payment provider processes donations from your visitors. Configure one now, or use the free test mode to explore AmbassadorHub without any credentials.', 'ambassadorhub' ); ?>
		</p>

		<!-- Option 1: real PSP -->
		<label style="display:flex;align-items:flex-start;gap:12px;padding:14px 16px;border:1px solid #c3c4c7;border-radius:4px;margin-bottom:10px;cursor:pointer;background:#fff;">
			<input
				type="radio"
				name="ambh_psp_choice"
				value="real"
				<?php checked( $selected, 'real' ); ?>
				style="margin-top:3px;flex-shrink:0;"
			/>
			<div>
				<strong><?php esc_html_e( 'I already have a PSP and want to set it up', 'ambassadorhub' ); ?></strong>
				<?php if ( $is_real_psp ) : ?>
					<p class="description" style="margin:4px 0 0;">
						<span class="dashicons dashicons-yes-alt" style="color:#00a32a;font-size:14px;width:14px;height:14px;vertical-align:middle;" aria-hidden="true"></span>
						<?php
						echo esc_html(
							sprintf(
								/* translators: %s: PSP name */
								__( 'Connected: %s', 'ambassadorhub' ),
								$psp_label
							)
						);
						?>
					</p>
					<p style="margin:6px 0 0;">
						<a href="<?php echo esc_url( $psp_settings_url ); ?>" target="_blank" rel="noopener noreferrer" class="button button-small">
							<?php esc_html_e( 'PSP settings', 'ambassadorhub' ); ?>
							<span class="dashicons dashicons-external" aria-hidden="true" style="font-size:13px;width:13px;height:13px;margin-top:3px;"></span>
						</a>
					</p>
				<?php else : ?>
					<p class="description" style="margin:4px 0 0;">
						<?php esc_html_e( 'Connect Mollie, PayPal, or another provider.', 'ambassadorhub' ); ?>
					</p>
					<p style="margin:6px 0 0;">
						<a href="<?php echo esc_url( $psp_settings_url ); ?>" target="_blank" rel="noopener noreferrer" class="button button-small">
							<?php esc_html_e( 'Open PSP settings', 'ambassadorhub' ); ?>
							<span class="dashicons dashicons-external" aria-hidden="true" style="font-size:13px;width:13px;height:13px;margin-top:3px;"></span>
						</a>
					</p>
					<p class="description" style="margin:4px 0 0;">
						<?php esc_html_e( 'Opens in a new tab. Configure your credentials, then click Next.', 'ambassadorhub' ); ?>
					</p>
				<?php endif; ?>
			</div>
		</label>

		<!-- Option 2: free / test -->
		<label style="display:flex;align-items:flex-start;gap:12px;padding:14px 16px;border:1px solid #c3c4c7;border-radius:4px;cursor:pointer;background:#fff;">
			<input
				type="radio"
				name="ambh_psp_choice"
				value="test"
				<?php checked( $selected, 'test' ); ?>
				style="margin-top:3px;flex-shrink:0;"
			/>
			<div>
				<strong><?php esc_html_e( 'Let me test this first', 'ambassadorhub' ); ?></strong>
				<p class="description" style="margin:4px 0 0;">
					<?php esc_html_e( 'Donations complete instantly — no credentials, no external checkout. Switch to a real PSP any time from Settings.', 'ambassadorhub' ); ?>
				</p>
			</div>
		</label>
		<?php
	}
}
