<?php
/**
 * Setup, as a list of stages.
 *
 * One row per stage — status on the left, a short result on the right, controls
 * behind the disclosure. The same `<details>` shape the Notifications tab uses,
 * so this reads as part of the plugin rather than as a screen of its own.
 *
 * @package TextTango
 *
 * @var array<string, mixed> $args View data supplied by Wizard::render().
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

use TextTango\Admin\Wizard;

$texttango_steps    = (array) ( $args['steps'] ?? array() );
$texttango_facts    = (array) ( $args['facts'] ?? array() );
$texttango_labels   = (array) ( $args['labels'] ?? array() );
$texttango_open     = (string) ( $args['open'] ?? '' );
$texttango_progress = (array) ( $args['progress'] ?? array() );
$texttango_complete = ! empty( $args['complete'] );
?>
<div class="wrap">
	<div id="texttango-app" class="texttango-ui" data-texttango-wizard>
		<div class="tt-py-4">

			<header class="tt-mb-6">
				<?php
				load_template(
					TEXTTANGO_PATH . 'templates/admin/partials/brand.php',
					false,
					array( 'title' => __( 'Setup', 'texttango' ) )
				);
				?>

				<!-- The one-line state of the whole thing, before any detail. -->
				<p class="tt-mt-2 tt-flex tt-flex-wrap tt-items-center tt-gap-x-3 tt-gap-y-1 tt-text-sm tt-text-slate-600">
					<span class="tt-badge <?php echo esc_attr( $texttango_complete ? 'tt-badge-ok' : 'tt-badge-off' ); ?>">
						<span class="tt-dot"></span>
						<?php
						echo $texttango_complete
							? esc_html__( 'Set up', 'texttango' )
							: esc_html__( 'Unfinished', 'texttango' );
						?>
					</span>

					<span class="tt-text-slate-400">&middot;</span>
					<span>
						<?php
						printf(
							/* translators: 1: stages done, 2: stages that apply to this site. */
							esc_html__( '%1$d of %2$d done', 'texttango' ),
							(int) ( $texttango_progress['done'] ?? 0 ),
							(int) ( $texttango_progress['total'] ?? 0 )
						);
						?>
					</span>

					<?php foreach ( (array) ( $args['summary'] ?? array() ) as $texttango_fact ) : ?>
						<span class="tt-text-slate-400">&middot;</span>
						<span><?php echo esc_html( (string) $texttango_fact ); ?></span>
					<?php endforeach; ?>
				</p>
			</header>

			<?php foreach ( (array) ( $args['notices'] ?? array() ) as $texttango_notice ) : ?>
				<div class="tt-notice tt-notice-<?php echo esc_attr( 'ok' === $texttango_notice['type'] ? 'ok' : ( 'warn' === $texttango_notice['type'] ? 'warn' : 'error' ) ); ?> tt-mb-4">
					<?php echo esc_html( (string) $texttango_notice['message'] ); ?>
				</div>
			<?php endforeach; ?>

			<div class="tt-card tt-p-0">
				<?php foreach ( $texttango_steps as $texttango_step ) : ?>
					<?php
					$texttango_key   = (string) $texttango_step['key'];
					$texttango_state = Wizard::status( (array) $texttango_step );
					$texttango_meta  = Wizard::meta( $texttango_key, $texttango_facts );
					?>
					<details
						class="tt-stage tt-stage-<?php echo esc_attr( $texttango_state ); ?>"
						<?php echo $texttango_key === $texttango_open ? 'open' : ''; ?>>

						<summary class="tt-stage-row">
							<span class="tt-stage-mark" aria-hidden="true">
								<?php
								if ( 'done' === $texttango_state ) {
									echo '&#10003;';
								} elseif ( 'blocked' === $texttango_state ) {
									echo '&ndash;';
								}
								?>
							</span>

							<span class="tt-stage-name">
								<?php echo esc_html( (string) ( $texttango_labels[ $texttango_key ] ?? $texttango_step['title'] ) ); ?>
								<span class="tt-sr-only">
									<?php
									if ( 'done' === $texttango_state ) {
										esc_html_e( '(done)', 'texttango' );
									} elseif ( 'blocked' === $texttango_state ) {
										esc_html_e( '(not possible on this site)', 'texttango' );
									} else {
										esc_html_e( '(still to do)', 'texttango' );
									}
									?>
								</span>
							</span>

							<span class="tt-stage-meta"><?php echo esc_html( $texttango_meta ); ?></span>
							<span class="tt-stage-chevron" aria-hidden="true">&rsaquo;</span>
						</summary>

						<div class="tt-stage-body">
							<p class="tt-mb-5 tt-mt-0 tt-text-sm tt-leading-relaxed tt-text-slate-500">
								<?php echo esc_html( (string) $texttango_step['summary'] ); ?>
							</p>

							<?php
							$texttango_view = TEXTTANGO_PATH . 'templates/admin/wizard/step-' . sanitize_file_name( $texttango_key ) . '.php';

							if ( file_exists( $texttango_view ) ) {
								// A plain require, so the stage inherits $args and $texttango_step.
								require $texttango_view;
							}
							?>
						</div>
					</details>
				<?php endforeach; ?>
			</div>

			<p class="tt-mt-4 tt-flex tt-flex-wrap tt-gap-3">
				<a class="tt-btn tt-btn-secondary" href="<?php echo esc_url( (string) $args['settings'] ); ?>">
					<?php esc_html_e( 'Open settings', 'texttango' ); ?>
				</a>
				<a class="tt-btn tt-btn-secondary" href="<?php echo esc_url( (string) $args['logs_url'] ); ?>">
					<?php esc_html_e( 'View the message log', 'texttango' ); ?>
				</a>
			</p>
		</div>
	</div>
</div>
