<?php
/**
 * Single recipe template card (included from recipes.php).
 *
 * Expects: $key, $recipe, $trigger_labels, $template_mode
 *
 * @package CartCue
 */
defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template scope.

$trigger_lbl = $trigger_labels[ $recipe['trigger'] ?? '' ] ?? ( $recipe['trigger'] ?? '' );
$edit_url    = add_query_arg(
	[
		'page'            => 'cartcue',
		'action'          => 'new',
		'recipe'          => $key,
		'automation_type' => 'blast' === $template_mode ? 'campaign' : 'event',
	],
	admin_url( 'admin.php' )
);
$autoactivate_url = wp_nonce_url(
	add_query_arg(
		[
			'page'           => 'cartcue',
			'action'         => 'new',
			'recipe'         => $key,
			'autoactivate'   => '1',
			'automation_type' => 'blast' === $template_mode ? 'campaign' : 'event',
		],
		admin_url( 'admin.php' )
	),
	'cartcue_autoactivate'
);
$timing = is_array( $recipe['timing'] ?? null ) ? $recipe['timing'] : [];
$story_wait = __( 'immediately', 'cartcue-sales-automation' );
if ( 'delay' === ( $timing['type'] ?? '' ) ) {
	$story_wait = sprintf(
		/* translators: 1: delay value, 2: delay unit */
		__( 'wait %1$d %2$s', 'cartcue-sales-automation' ),
		(int) ( $timing['delay_value'] ?? 1 ),
		esc_html( (string) ( $timing['delay_unit'] ?? 'hours' ) )
	);
}
$recipe_action      = __( 'run action', 'cartcue-sales-automation' );
$recipe_action_type = sanitize_key( (string) ( $recipe['actions'][0]['type'] ?? '' ) );
if ( 'send_email' === $recipe_action_type ) {
	$recipe_action = __( 'send email', 'cartcue-sales-automation' );
} elseif ( 'send_sms' === $recipe_action_type ) {
	$recipe_action = __( 'send SMS', 'cartcue-sales-automation' );
}
$recipe_email_subject = '';
$recipe_email_snippet = '';
foreach ( $recipe['actions'] ?? [] as $recipe_action_row ) {
	if ( 'send_email' !== ( $recipe_action_row['type'] ?? '' ) ) {
		continue;
	}
	$recipe_email_subject = (string) ( $recipe_action_row['settings']['subject'] ?? '' );
	$recipe_email_body    = (string) ( $recipe_action_row['settings']['body'] ?? '' );
	if ( \CartCue\EmailBlockRenderer::is_builder_content( $recipe_email_body ) ) {
		$recipe_email_body = \CartCue\EmailBlockRenderer::to_html( $recipe_email_body );
	}
	$recipe_email_snippet = wp_trim_words( wp_strip_all_tags( $recipe_email_body ), 30, '…' );
	break;
}
?>
<button type="button"
		class="ap-recipe-card<?php echo ! empty( $recipe['popular'] ) ? ' ap-recipe-card--popular' : ''; ?>"
		data-recipe-key="<?php echo esc_attr( $key ); ?>"
		data-recipe-name="<?php echo esc_attr( $recipe['name'] ); ?>"
		data-recipe-desc="<?php echo esc_attr( $recipe['description'] ); ?>"
		data-recipe-trigger="<?php echo esc_attr( $trigger_lbl ); ?>"
		data-recipe-wait="<?php echo esc_attr( $story_wait ); ?>"
		data-recipe-action="<?php echo esc_attr( $recipe_action ); ?>"
		data-recipe-email-subject="<?php echo esc_attr( $recipe_email_subject ); ?>"
		data-recipe-email-snippet="<?php echo esc_attr( $recipe_email_snippet ); ?>"
		data-recipe-url="<?php echo esc_url( $edit_url ); ?>"
		data-recipe-activate="<?php echo esc_url( $autoactivate_url ); ?>">
	<?php if ( ! empty( $recipe['popular'] ) ) : ?>
	<div class="ap-recipe-popular"><?php esc_html_e( 'Most popular', 'cartcue-sales-automation' ); ?></div>
	<?php endif; ?>
	<div class="ap-recipe-icon-wrap">
		<i class="ti <?php echo esc_attr( $recipe['icon'] ); ?>" aria-hidden="true"></i>
	</div>
	<div class="ap-recipe-title"><?php echo esc_html( $recipe['name'] ); ?></div>
	<p class="ap-recipe-desc"><?php echo esc_html( $recipe['description'] ); ?></p>
	<?php if ( $recipe_email_subject ) : ?>
	<div class="ap-recipe-card__hover-preview">
		<strong><?php echo esc_html( $recipe_email_subject ); ?></strong>
		<p><?php echo esc_html( $recipe_email_snippet ); ?></p>
	</div>
	<?php endif; ?>
	<div class="ap-recipe-trigger-tag"><?php echo esc_html( $trigger_lbl ); ?></div>
	<div class="ap-recipe-cta"><?php esc_html_e( 'Use this template →', 'cartcue-sales-automation' ); ?></div>
</button>
