<?php
/**
 * CartCue — Automation templates picker.
 *
 * @var array $recipes  All built-in recipes (from Recipes::all()).
 * @var \CartCue\Plugin $plugin
 * @package CartCue
 */
defined( 'ABSPATH' ) || exit;

/* phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound */

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$template_mode = ( isset( $_GET['mode'] ) && 'blast' === sanitize_key( wp_unslash( $_GET['mode'] ) ) ) ? 'blast' : 'event';
$new_blank_url = add_query_arg(
	[
		'page'            => 'cartcue',
		'action'          => 'new',
		'automation_type' => 'blast' === $template_mode ? 'campaign' : 'event',
	],
	admin_url( 'admin.php' )
);
$back_url              = add_query_arg( [ 'page' => 'cartcue' ], admin_url( 'admin.php' ) );
$cart_recovery_url     = add_query_arg(
	[ 'page' => 'cartcue', 'action' => 'new', 'recipe' => 'cart_recovery' ],
	admin_url( 'admin.php' )
);
$event_templates_url   = add_query_arg( [ 'page' => 'cartcue-recipes' ], admin_url( 'admin.php' ) );
$blast_templates_url   = add_query_arg( [ 'page' => 'cartcue-recipes', 'mode' => 'blast' ], admin_url( 'admin.php' ) );
$popular_recipe_keys   = [ 'cart_recovery', 'first_order_welcome', 'post_purchase_review_ask' ];
$popular_recipes       = [];
foreach ( $popular_recipe_keys as $popular_key ) {
	if ( isset( $recipes[ $popular_key ] ) ) {
		$popular_recipes[ $popular_key ] = $recipes[ $popular_key ];
	}
}
$trigger_labels = [];
foreach ( $plugin->get_triggers() as $slug => $t ) {
	$trigger_labels[ $slug ] = $t->get_label();
}

$categories = [
	'recover'       => [
		'label' => __( 'Recover lost revenue', 'cartcue-sales-automation' ),
		'desc'  => __( 'Bring back customers who left before completing their purchase.', 'cartcue-sales-automation' ),
	],
	'post_purchase' => [
		'label' => __( 'Build customer loyalty', 'cartcue-sales-automation' ),
		'desc'  => __( 'Keep buyers coming back with smart follow-up after every order.', 'cartcue-sales-automation' ),
	],
	'win_back'      => [
		'label' => __( 'Re-engage lapsed customers', 'cartcue-sales-automation' ),
		'desc'  => __( 'Reconnect with customers who haven\'t ordered in a while.', 'cartcue-sales-automation' ),
	],
	'loyalty'       => [
		'label' => __( 'Grow reviews and referrals', 'cartcue-sales-automation' ),
		'desc'  => __( 'Collect social proof from happy customers automatically.', 'cartcue-sales-automation' ),
	],
	'subscriptions' => [
		'label' => __( 'Protect subscription revenue', 'cartcue-sales-automation' ),
		'desc'  => __( 'Reduce churn with lifecycle messages at every renewal milestone.', 'cartcue-sales-automation' ),
	],
	'integrations'  => [
		'label' => __( 'Sync with your CRM', 'cartcue-sales-automation' ),
		'desc'  => __( 'Keep FluentCRM, Mailchimp, MailPoet and Klaviyo in sync on every order.', 'cartcue-sales-automation' ),
	],
];

$grouped = [];
foreach ( $recipes as $key => $recipe ) {
	$cat = $recipe['category'] ?? 'post_purchase';
	if ( ! isset( $categories[ $cat ] ) ) {
		$cat = 'post_purchase';
	}
	$grouped[ $cat ][ $key ] = $recipe;
}
?>
<div class="ap-wrap">

	<div class="ap-page-header">
		<div>
			<a href="<?php echo esc_url( $back_url ); ?>" class="ap-back-link">← <?php esc_html_e( 'Plays', 'cartcue-sales-automation' ); ?></a>
			<h1 class="ap-page-title"><?php esc_html_e( 'Templates', 'cartcue-sales-automation' ); ?></h1>
			<p class="ap-page-sub"><?php esc_html_e( 'Pick a goal — we\'ll pre-fill the play. Customize anything before you go live.', 'cartcue-sales-automation' ); ?></p>
		</div>
	</div>

	<div class="ap-template-mode" role="tablist" aria-label="<?php esc_attr_e( 'Template type', 'cartcue-sales-automation' ); ?>">
		<a href="<?php echo esc_url( $event_templates_url ); ?>" class="ap-template-mode__btn<?php echo 'event' === $template_mode ? ' is-active' : ''; ?>" role="tab" aria-selected="<?php echo 'event' === $template_mode ? 'true' : 'false'; ?>">
			<?php esc_html_e( 'Event-triggered', 'cartcue-sales-automation' ); ?>
		</a>
		<a href="<?php echo esc_url( $blast_templates_url ); ?>" class="ap-template-mode__btn<?php echo 'blast' === $template_mode ? ' is-active' : ''; ?>" role="tab" aria-selected="<?php echo 'blast' === $template_mode ? 'true' : 'false'; ?>">
			<?php esc_html_e( 'One-time blast', 'cartcue-sales-automation' ); ?>
		</a>
	</div>

	<?php if ( 'event' === $template_mode && isset( $recipes['cart_recovery'] ) ) : ?>
	<div class="ap-template-quickwin">
		<p class="ap-template-quickwin__eyebrow"><?php esc_html_e( 'Most stores start here', 'cartcue-sales-automation' ); ?></p>
		<div class="ap-quickwin-card">
			<div class="ap-quickwin-card__icon">🛒</div>
			<div class="ap-quickwin-card__body">
				<strong><?php esc_html_e( 'Recover abandoned carts', 'cartcue-sales-automation' ); ?></strong>
				<p><?php esc_html_e( 'When someone leaves without buying → wait 1 hour → send a recovery email. Pre-configured and ready to activate.', 'cartcue-sales-automation' ); ?></p>
			</div>
			<a href="<?php echo esc_url( $cart_recovery_url ); ?>" class="ap-btn ap-btn-primary">
				<?php esc_html_e( 'Set up in 2 minutes →', 'cartcue-sales-automation' ); ?>
			</a>
		</div>
	</div>
	<?php endif; ?>

	<?php if ( ! empty( $popular_recipes ) && 'event' === $template_mode ) : ?>
	<div class="ap-template-group ap-template-group--popular">
		<h2 class="ap-template-group__title"><?php esc_html_e( 'Most popular', 'cartcue-sales-automation' ); ?></h2>
		<div class="ap-recipe-grid">
			<?php
			foreach ( $popular_recipes as $key => $recipe ) {
				include __DIR__ . '/partials/recipe-card.php';
			}
			?>
		</div>
	</div>
	<?php endif; ?>

	<?php foreach ( $categories as $cat_slug => $cat_meta ) :
		if ( empty( $grouped[ $cat_slug ] ) ) {
			continue;
		}
		?>
	<details class="ap-template-group ap-template-group--collapsible">
		<summary class="ap-template-group__summary">
			<span class="ap-template-group__title"><?php echo esc_html( $cat_meta['label'] ); ?></span>
			<span class="ap-template-group__desc"><?php echo esc_html( $cat_meta['desc'] ); ?></span>
		</summary>
		<div class="ap-recipe-grid">
			<?php
			foreach ( $grouped[ $cat_slug ] as $key => $recipe ) {
				include __DIR__ . '/partials/recipe-card.php';
			}
			?>
		</div>
	</details>
	<?php endforeach; ?>

	<p class="ap-template-scratch-link">
		<a href="<?php echo esc_url( $new_blank_url ); ?>"><?php esc_html_e( 'Or start from scratch →', 'cartcue-sales-automation' ); ?></a>
	</p>

	<div class="ap-recipe-modal" id="ap-recipe-modal" hidden role="dialog" aria-modal="true" aria-labelledby="ap-recipe-modal-title">
		<div class="ap-recipe-modal__backdrop"></div>
		<div class="ap-recipe-modal__panel">
			<button type="button" class="ap-recipe-modal__close" aria-label="<?php esc_attr_e( 'Close', 'cartcue-sales-automation' ); ?>">✕</button>
			<div class="ap-recipe-modal__icon">
				<i class="ti ti-bolt" aria-hidden="true"></i>
			</div>
			<h2 class="ap-recipe-modal__title" id="ap-recipe-modal-title"></h2>
			<p class="ap-recipe-modal__desc"></p>
			<div class="ap-recipe-modal__story">
				<span class="ap-story-label"><?php esc_html_e( 'When', 'cartcue-sales-automation' ); ?></span>
				<strong class="ap-recipe-modal__trigger"></strong>
				<span class="ap-story-arrow">→</span>
				<span class="ap-recipe-modal__wait"></span>
				<span class="ap-story-arrow">→</span>
				<span class="ap-recipe-modal__action-label"><?php esc_html_e( 'send email', 'cartcue-sales-automation' ); ?></span>
			</div>
			<div class="ap-recipe-modal__email-preview" hidden>
				<p class="ap-recipe-modal__email-preview-label"><?php esc_html_e( 'Email preview', 'cartcue-sales-automation' ); ?></p>
				<div class="ap-recipe-modal__email-thumb">
					<div class="ap-recipe-modal__email-subject"></div>
					<div class="ap-recipe-modal__email-snippet"></div>
				</div>
			</div>
			<div class="ap-recipe-modal__actions">
				<a href="#" class="ap-btn ap-btn-primary ap-recipe-modal__activate" id="ap-recipe-modal-activate">
					<?php esc_html_e( 'Activate this template →', 'cartcue-sales-automation' ); ?>
				</a>
				<button type="button" class="ap-btn ap-btn-ghost ap-recipe-modal__customize" id="ap-recipe-modal-customize">
					<?php esc_html_e( 'Customise before activating', 'cartcue-sales-automation' ); ?>
				</button>
			</div>
		</div>
	</div>
</div>
<?php
/* phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound */
?>
