<?php
/**
 * Setup checklist panel (RTEC-aligned markup). Free tier only.
 *
 * @package WPEventGenius
 */

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

use WPEventGenius\Admin\Services\EvgeOnboardingService;

$onboarding_state = EvgeOnboardingService::get_state();
if ( empty( $onboarding_state[ EvgeOnboardingService::KEY_CHECKLIST_INITIATED ] ) ) {
	return;
}
if ( ! empty( $onboarding_state[ EvgeOnboardingService::KEY_CHECKLIST_DISMISSED ] ) ) {
	return;
}

$checklist = EvgeOnboardingService::get_checklist();
$done      = 0;
foreach ( $checklist as $item ) {
	if ( ! empty( $item['done'] ) ) {
		++$done;
	}
}
$total    = count( $checklist );
$all_done = ( $done >= $total );

// phpcs:disable WordPress.Security.NonceVerification.Recommended
$base = add_query_arg( array( 'page' => isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : 'evge-settings' ), admin_url( 'admin.php' ) );
if ( ! empty( $_GET['tab'] ) ) {
	$base = add_query_arg( 'tab', sanitize_key( wp_unslash( $_GET['tab'] ) ), $base );
}
if ( ! empty( $_GET['subtab'] ) ) {
	$base = add_query_arg( 'subtab', sanitize_key( wp_unslash( $_GET['subtab'] ) ), $base );
}
if ( ! empty( $_GET['form_id'] ) ) {
	$base = add_query_arg( 'form_id', absint( $_GET['form_id'] ), $base );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended

$dismiss_url = wp_nonce_url( add_query_arg( 'evge_dismiss_checklist', '1', $base ), 'evge_dismiss_checklist' );
?>
<div class="evge-settings-checklist <?php echo $all_done ? 'evge-settings-checklist-collapsed' : ''; ?>" id="evge-settings-checklist" data-evge-checklist data-all-done="<?php echo $all_done ? '1' : '0'; ?>">
	<div class="evge-settings-checklist-header">
		<button type="button" class="evge-settings-checklist-toggle" aria-expanded="<?php echo $all_done ? 'false' : 'true'; ?>" aria-controls="evge-settings-checklist-body" id="evge-settings-checklist-toggle">
			<span class="evge-settings-checklist-title">
				<?php if ( $all_done ) : ?>
					<?php esc_html_e( 'Setup Complete ✓', 'event-genius' ); ?>
				<?php else : ?>
					🎯 <?php esc_html_e( 'Get the most out of Event Genius', 'event-genius' ); ?>
				<?php endif; ?>
				<span class="evge-settings-checklist-progress-inline" aria-hidden="true"><?php echo (int) $done; ?> / <?php echo (int) $total; ?></span>
			</span>
			<span class="evge-settings-checklist-chevron" aria-hidden="true"></span>
		</button>
	</div>
	<div id="evge-settings-checklist-body" class="evge-settings-checklist-body"<?php echo $all_done ? ' aria-hidden="true"' : ''; ?>>
		<p class="evge-settings-checklist-intro"><?php esc_html_e( 'Here are a few optional next steps:', 'event-genius' ); ?></p>
		<ul class="evge-settings-checklist-list">
			<?php foreach ( $checklist as $item ) : ?>
				<li class="evge-settings-checklist-item <?php echo ! empty( $item['done'] ) ? 'evge-checklist-done' : ''; ?>">
					<?php if ( ! empty( $item['done'] ) ) : ?>
						<span class="evge-checklist-icon evge-checklist-done-icon" aria-hidden="true">✓</span>
					<?php else : ?>
						<span class="evge-checklist-icon evge-checklist-pending-icon" aria-hidden="true">○</span>
					<?php endif; ?>
					<span class="evge-checklist-content">
						<?php if ( ! empty( $item['done'] ) ) : ?>
							<span><?php echo esc_html( $item['label'] ); ?></span>
						<?php elseif ( ! empty( $item['url'] ) ) : ?>
							<?php
							$cta = isset( $item['cta_label'] ) ? $item['cta_label'] : $item['label'];
							?>
							<span class="evge-checklist-label-line">
								<a href="<?php echo esc_url( $item['url'] ); ?>" <?php echo ! empty( $item['external'] ) ? ' target="_blank" rel="noopener noreferrer"' : ''; ?>><?php echo esc_html( $item['label'] ); ?></a>
								<?php if ( ! empty( $item['description'] ) ) : ?>
									<span class="evge-checklist-info-wrap">
										<span class="evge-checklist-info-icon" aria-label="<?php esc_attr_e( 'More information', 'event-genius' ); ?>">ⓘ</span>
										<span class="evge-checklist-description evge-checklist-tooltip"><?php echo esc_html( $item['description'] ); ?></span>
									</span>
								<?php endif; ?>
							</span>
							<span class="evge-checklist-cta-wrap">
								<a href="<?php echo esc_url( $item['url'] ); ?>" class="evge-checklist-cta" <?php echo ! empty( $item['external'] ) ? ' target="_blank" rel="noopener noreferrer"' : ''; ?>><?php echo esc_html( $cta ); ?> &#8250;</a>
							</span>
						<?php else : ?>
							<span class="evge-checklist-label-line">
								<span><?php echo esc_html( $item['label'] ); ?></span>
								<?php if ( ! empty( $item['description'] ) ) : ?>
									<span class="evge-checklist-info-wrap">
										<span class="evge-checklist-info-icon" aria-label="<?php esc_attr_e( 'More information', 'event-genius' ); ?>">ⓘ</span>
										<span class="evge-checklist-description evge-checklist-tooltip"><?php echo esc_html( $item['description'] ); ?></span>
									</span>
								<?php endif; ?>
							</span>
						<?php endif; ?>
					</span>
				</li>
			<?php endforeach; ?>
		</ul>
		<div class="evge-settings-checklist-footer">
			<a href="<?php echo esc_url( $dismiss_url ); ?>" class="evge-settings-checklist-dismiss"><?php esc_html_e( 'Dismiss permanently', 'event-genius' ); ?></a>
		</div>
	</div>
</div>
