<?php
/**
 * Setup readiness panel.
 *
 * Variables in scope (passed by Admin_Settings::render_list_view()):
 *
 * @var \DonationRouter\Setup_Check[] $checks
 * @var bool                          $is_ready
 * @var int                           $fail_count
 * @var int                           $warn_count
 *
 * @since   1.2.0
 * @package DonationRouter
 */

declare( strict_types=1 );

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

use DonationRouter\Setup_Check;

$ador_summary = ! $is_ready
	? sprintf(
		/* translators: %d: number of failing checks. */
		_n( '%d item needs attention', '%d items need attention', $fail_count, 'alamin-donation-router' ),
		$fail_count
	)
	: ( $warn_count > 0
		? sprintf(
			/* translators: %d: number of suggestions. */
			_n( 'Ready — %d suggestion', 'Ready — %d suggestions', $warn_count, 'alamin-donation-router' ),
			$warn_count
		)
		: __( 'Setup complete', 'alamin-donation-router' ) );
?>
<details class="ador-setup-status<?php echo $is_ready ? '' : ' ador-setup-status--has-fail'; ?>" <?php echo $is_ready ? '' : 'open'; ?>>
	<summary class="ador-setup-status__summary">
		<?php
		echo ador_icon( $is_ready ? 'check' : 'alert-triangle' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		?>
		<span><?php echo esc_html( $ador_summary ); ?></span>
	</summary>

	<ul class="ador-setup-status__list">
		<?php foreach ( $checks as $ador_check ) : ?>
			<?php if ( Setup_Check::PASS === $ador_check->state ) : ?>
				<?php continue; ?>
			<?php endif; ?>
			<li class="ador-setup-status__item is-<?php echo esc_attr( $ador_check->state ); ?>">
				<?php
				echo ador_icon( $ador_check->is_fail() ? 'alert-triangle' : 'info' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
				?>
				<span class="ador-setup-status__label"><?php echo esc_html( $ador_check->label ); ?></span>
				<span class="ador-setup-status__detail"><?php echo esc_html( $ador_check->detail ); ?></span>
				<?php if ( '' !== $ador_check->fix_url ) : ?>
					<a class="ador-setup-status__fix" href="<?php echo esc_url( $ador_check->fix_url ); ?>">
						<?php echo esc_html( $ador_check->fix_label ); ?>
					</a>
				<?php endif; ?>
			</li>
		<?php endforeach; ?>

		<?php if ( $is_ready && 0 === $warn_count ) : ?>
			<li class="ador-setup-status__item is-pass">
				<?php echo ador_icon( 'check' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				<span class="ador-setup-status__detail">
					<?php esc_html_e( 'Accounts, routing rules, webhooks and the donor selector are all in place.', 'alamin-donation-router' ); ?>
				</span>
			</li>
		<?php endif; ?>
	</ul>
</details>
