<?php
/**
 * Create Pages Step
 *
 * Installs the required WordPress pages (Account, Sign-up, Payment
 * confirmation, etc.) via the existing PageInstaller service.  Also shows
 * the current status of each page so the user can see what was created.
 *
 * @package AmbassadorHub\Admin\Wizard\Steps
 */

namespace AmbassadorHub\Admin\Wizard\Steps;

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

use AmbassadorHub\Admin\Wizard\AbstractWizardStep;
use AmbassadorHub\Admin\Setup\PageInstaller;

/**
 * Wizard step that creates required WordPress pages using PageInstaller.
 */
final class CreatePagesStep extends AbstractWizardStep {

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

	/**
	 * {@inheritdoc}
	 */
	public function get_title(): string {
		return __( 'Create required pages', 'ambassadorhub' );
	}

	/**
	 * {@inheritdoc}
	 *
	 * Pages are recommended but the user may prefer to set them up manually.
	 */
	public function is_skippable(): bool {
		return true;
	}

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

	/**
	 * {@inheritdoc}
	 */
	public function is_done(): bool {
		foreach ( PageInstaller::get_statuses() as $status ) {
			if ( ! $status['page_exists'] ) {
				return false;
			}
		}

		return true;
	}

	/**
	 * {@inheritdoc}
	 *
	 * Runs PageInstaller::install() when pages are missing so that clicking
	 * "Next" is also the trigger for page creation.  Already-existing pages are
	 * silently skipped by PageInstaller (idempotent).
	 */
	public function handle( array $post_data ): ?string {
		PageInstaller::install();

		return null; // Auto-advance.
	}

	/**
	 * {@inheritdoc}
	 */
	public function render(): void {
		$statuses = PageInstaller::get_statuses();
		$all_done = $this->is_done();
		?>
		<p>
			<?php esc_html_e( 'AmbassadorHub needs a few WordPress pages to work. Clicking "Next" will create any missing pages automatically.', 'ambassadorhub' ); ?>
		</p>

		<?php if ( $all_done ) : ?>
			<div class="ambh-wizard-status ambh-wizard-status--done">
				<span class="dashicons dashicons-yes-alt" aria-hidden="true"></span>
				<?php esc_html_e( 'All pages are already in place.', 'ambassadorhub' ); ?>
			</div>
		<?php endif; ?>

		<ul class="ambh-wizard-pages-list">
			<?php foreach ( $statuses as $status ) : ?>
				<?php $exists = $status['page_exists']; ?>
				<li>
					<span class="dashicons ambh-wizard-page-icon
						<?php echo $exists ? 'dashicons-yes-alt ambh-wizard-page-icon--done' : 'dashicons-marker ambh-wizard-page-icon--pending'; ?>"
						aria-hidden="true"></span>
					<span><?php echo esc_html( $status['definition']['label'] ); ?></span>
					<?php if ( $exists ) : ?>
						<span style="margin-left:auto;font-size:11px;color:#646970;">
							<?php esc_html_e( 'exists', 'ambassadorhub' ); ?>
						</span>
					<?php else : ?>
						<span style="margin-left:auto;font-size:11px;color:#d63638;">
							<?php esc_html_e( 'will be created', 'ambassadorhub' ); ?>
						</span>
					<?php endif; ?>
				</li>
			<?php endforeach; ?>
		</ul>
		<?php
	}
}
