<?php
/**
 * CartCue — Unified single-page play editor (v1.5).
 *
 * @var array|null        $play
 * @var array|null        $recipe
 * @var \CartCue\Plugin   $plugin
 *
 * @package CartCue
 */
defined( 'ABSPATH' ) || exit;

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

$src         = $play ?? $recipe ?? [];
$is_new      = empty( $play );
$play_id     = (int) ( $play['id'] ?? 0 );
$wf_name     = (string) ( $src['name'] ?? '' );
$wf_trigger  = (string) ( $src['trigger_slug'] ?? $src['trigger'] ?? '' );
$wf_t_setts  = $src['trigger_settings'] ?? [];
$wf_rules    = $src['rules'] ?? [];
$wf_actions  = $src['actions'] ?? [];
$wf_timing   = $src['timing'] ?? [ 'type' => 'immediate' ];
$wf_status   = (string) ( $src['status'] ?? 'paused' );
$max_runs    = (int) ( $src['max_runs_per_customer'] ?? 1 );
$cooldown    = (int) ( $src['cooldown_days'] ?? 0 );

$timing_type = $wf_timing['type'] ?? 'immediate';
$delay_val   = (int) ( $wf_timing['delay_value'] ?? 1 );
$delay_unit  = (string) ( $wf_timing['delay_unit'] ?? 'hours' );

$back_url          = add_query_arg( [ 'page' => 'cartcue' ], admin_url( 'admin.php' ) );
$current_trigger   = $wf_trigger ? $plugin->get_trigger( $wf_trigger ) : null;
$is_ghost_trigger  = $current_trigger instanceof \CartCue\Triggers\GhostTrigger;
$is_pro            = $plugin->is_pro();
$max_active_plays  = $plugin->max_active_plays();
$at_active_limit   = $max_active_plays > 0 && ! $plugin->can_activate_more_plays( $play_id );

if ( empty( $wf_actions ) ) {
	$wf_actions = [ [ 'type' => '', 'settings' => [] ] ];
}

$trigger_group_defs = [
	'cart_checkout' => [
		'label' => __( 'Cart & checkout', 'cartcue-sales-automation' ),
		'slugs' => [ 'cart_abandoned', 'checkout_started' ],
	],
	'orders'        => [
		'label' => __( 'Orders', 'cartcue-sales-automation' ),
		'slugs' => [ 'order_paid', 'order_status_changed', 'order_cancelled', 'order_refunded', 'payment_failed', 'order_note_added' ],
	],
	'customers'     => [
		'label' => __( 'Customers', 'cartcue-sales-automation' ),
		'slugs' => [ 'customer_created', 'customer_win_back', 'customer_never_purchased', 'customer_birthday', 'manual' ],
	],
	'products'      => [
		'label' => __( 'Products & reviews', 'cartcue-sales-automation' ),
		'slugs' => [ 'purchased_product', 'purchased_category', 'purchased_tag', 'new_review', 'review_pending', 'product_stock_low' ],
	],
	'subscriptions' => [
		'label' => __( 'Subscriptions', 'cartcue-sales-automation' ),
		'slugs' => [ 'subscription_event', 'subscription_cancelled', 'subscription_expired', 'subscription_paused', 'subscription_reactivated', 'nth_renewal_milestone', 'saved_card_expiring' ],
	],
	'integrations'  => [
		'label' => __( 'Webhooks & integrations', 'cartcue-sales-automation' ),
		'slugs' => [ 'inbound_webhook' ],
	],
];

$all_triggers   = $plugin->get_triggers();
$assigned_slugs = [];

$play_revenue = 0.0;
if ( $play_id ) {
	$rev_rows = \CartCue\ConversionTracker::get_revenue_by_plays( [ $play_id ] );
	if ( ! empty( $rev_rows[0]['revenue'] ) ) {
		$play_revenue = (float) $rev_rows[0]['revenue'];
	}
}
?>
<div class="ap-wrap ap-wrap--unified">

	<div id="ap-test-toast" class="ap-test-toast" role="status" aria-live="polite" hidden></div>

	<header class="ap-uni-header">
		<a href="<?php echo esc_url( $back_url ); ?>" class="ap-back-link">← <?php esc_html_e( 'Plays', 'cartcue-sales-automation' ); ?></a>
		<input
			type="text"
			id="ap-uni-name-inline"
			class="ap-uni-name-inline"
			value="<?php echo esc_attr( $wf_name ); ?>"
			placeholder="<?php esc_attr_e( 'Untitled automation', 'cartcue-sales-automation' ); ?>"
			autocomplete="off"
			spellcheck="false"
		>
		<span class="ap-uni-status-pill" data-status-display>
			<?php echo 'active' === $wf_status
				? esc_html__( 'Active', 'cartcue-sales-automation' )
				: esc_html__( 'Draft', 'cartcue-sales-automation' ); ?>
		</span>
		<?php if ( $play_revenue > 0 ) : ?>
		<span class="ap-uni-revenue-badge" title="<?php esc_attr_e( 'Revenue recovered by this play', 'cartcue-sales-automation' ); ?>">
			💰 <?php echo wp_kses_post( wc_price( $play_revenue ) ); ?>
		</span>
		<?php endif; ?>
	</header>

	<form
		method="post"
		action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>"
		id="ap-play-form"
		class="ap-uni-form ap-automation-editor ap-automation-editor--event"
		data-is-new="<?php echo $is_new ? '1' : '0'; ?>"
		data-editor-layout="unified"
	>
		<?php wp_nonce_field( 'cartcue_save_play' ); ?>
		<input type="hidden" name="action" value="cartcue_save_play">
		<input type="hidden" name="play_id" value="<?php echo esc_attr( $play_id ); ?>">
		<input type="hidden" name="automation_type" value="event">
		<input type="hidden" name="name" id="ap-play-name" value="<?php echo esc_attr( $wf_name ); ?>">
		<input type="hidden" name="status" id="ap-status-input" value="<?php echo esc_attr( $wf_status ); ?>">
		<input type="hidden" name="timing_type" id="ap-timing-type" value="<?php echo esc_attr( $timing_type ); ?>">
		<input type="hidden" name="delay_value" id="ap-delay-value" value="<?php echo esc_attr( $delay_val ); ?>">
		<input type="hidden" name="delay_unit" id="ap-delay-unit" value="<?php echo esc_attr( $delay_unit ); ?>">
		<input type="hidden" name="max_runs_per_customer" id="ap-max-runs" value="<?php echo esc_attr( $max_runs ); ?>">
		<input type="hidden" name="cooldown_days" id="ap-cooldown-days" value="<?php echo esc_attr( $cooldown ); ?>">

		<div class="ap-uni-sentence">

			<section class="ap-uni-block ap-uni-block--when" id="ap-uni-when">
				<div class="ap-uni-block__keyword">
					<span class="ap-uni-keyword"><?php esc_html_e( 'When', 'cartcue-sales-automation' ); ?></span>
					<span class="ap-uni-keyword-sub"><?php esc_html_e( 'something happens in your store', 'cartcue-sales-automation' ); ?></span>
				</div>

				<div class="ap-uni-trigger-picker" id="ap-trigger-grid">
					<?php
					foreach ( $trigger_group_defs as $group_key => $group_meta ) :
						$in_group = [];
						foreach ( $group_meta['slugs'] as $slug ) {
							if ( ! isset( $all_triggers[ $slug ] ) ) {
								continue;
							}
							$t = $all_triggers[ $slug ];
							if ( ! $t->show_in_picker() ) {
								continue;
							}
							$in_group[ $slug ]       = $t;
							$assigned_slugs[ $slug ] = true;
						}
						if ( empty( $in_group ) ) {
							continue;
						}
						?>
					<div class="ap-uni-trigger-group" data-group="<?php echo esc_attr( $group_key ); ?>">
						<h3 class="ap-uni-trigger-group__label"><?php echo esc_html( $group_meta['label'] ); ?></h3>
						<div class="ap-uni-trigger-group__grid">
							<?php
							foreach ( $in_group as $slug => $t ) :
								$is_ghost = $t instanceof \CartCue\Triggers\GhostTrigger;
								$is_sel   = $wf_trigger === $slug;
								$tag      = ( $is_ghost && ! $is_pro ) ? 'button' : 'label';
								?>
							<<?php echo esc_attr( $tag ); ?>
								class="ap-trigger-card<?php echo $is_sel ? ' is-selected' : ''; ?><?php echo $is_ghost ? ' is-pro' : ''; ?><?php echo 'customer_birthday' === $slug ? ' ap-trigger-card--birthday' : ''; ?>"
								data-trigger-slug="<?php echo esc_attr( $slug ); ?>"
								<?php if ( $is_ghost && ! $is_pro ) : ?>
								type="button"
								aria-label="<?php echo esc_attr( sprintf(
									/* translators: %s: trigger label */
									__( '%s — available in CartCue Pro', 'cartcue-sales-automation' ),
									$t->get_label()
								) ); ?>"
								<?php endif; ?>>
								<?php if ( ! $is_ghost || $is_pro ) : ?>
								<input type="radio" name="trigger_slug" value="<?php echo esc_attr( $slug ); ?>" <?php checked( $is_sel ); ?> class="ap-trigger-radio">
								<?php endif; ?>
								<?php if ( $is_ghost ) : ?>
								<span class="ap-pro-badge"><?php esc_html_e( 'Pro', 'cartcue-sales-automation' ); ?></span>
								<?php endif; ?>
								<?php if ( 'customer_birthday' === $slug && $is_ghost ) : ?>
								<span class="ap-trigger-stat"><?php esc_html_e( '40%+ open rate', 'cartcue-sales-automation' ); ?></span>
								<?php endif; ?>
								<span class="ap-trigger-label"><?php echo esc_html( $t->get_label() ); ?></span>
								<span class="ap-trigger-desc"><?php echo esc_html( $t->get_description() ); ?></span>
							</<?php echo esc_attr( $tag ); ?>>
							<?php endforeach; ?>
						</div>
					</div>
					<?php endforeach; ?>

					<?php
					$other_triggers = [];
					foreach ( $all_triggers as $slug => $t ) {
						if ( isset( $assigned_slugs[ $slug ] ) || ! $t->show_in_picker() ) {
							continue;
						}
						$other_triggers[ $slug ] = $t;
					}
					if ( ! empty( $other_triggers ) ) :
						?>
					<div class="ap-uni-trigger-group" data-group="other">
						<h3 class="ap-uni-trigger-group__label"><?php esc_html_e( 'More triggers', 'cartcue-sales-automation' ); ?></h3>
						<div class="ap-uni-trigger-group__grid">
							<?php foreach ( $other_triggers as $slug => $t ) :
								$is_ghost = $t instanceof \CartCue\Triggers\GhostTrigger;
								$is_sel   = $wf_trigger === $slug;
								$tag      = ( $is_ghost && ! $is_pro ) ? 'button' : 'label';
								?>
							<<?php echo esc_attr( $tag ); ?>
								class="ap-trigger-card<?php echo $is_sel ? ' is-selected' : ''; ?><?php echo $is_ghost ? ' is-pro' : ''; ?><?php echo 'customer_birthday' === $slug ? ' ap-trigger-card--birthday' : ''; ?>"
								data-trigger-slug="<?php echo esc_attr( $slug ); ?>"
								<?php if ( $is_ghost && ! $is_pro ) : ?>
								type="button"
								aria-label="<?php echo esc_attr( sprintf(
									/* translators: %s: trigger label */
									__( '%s — available in CartCue Pro', 'cartcue-sales-automation' ),
									$t->get_label()
								) ); ?>"
								<?php endif; ?>>
								<?php if ( ! $is_ghost || $is_pro ) : ?>
								<input type="radio" name="trigger_slug" value="<?php echo esc_attr( $slug ); ?>" <?php checked( $is_sel ); ?> class="ap-trigger-radio">
								<?php endif; ?>
								<?php if ( $is_ghost ) : ?>
								<span class="ap-pro-badge"><?php esc_html_e( 'Pro', 'cartcue-sales-automation' ); ?></span>
								<?php endif; ?>
								<?php if ( 'customer_birthday' === $slug && $is_ghost ) : ?>
								<span class="ap-trigger-stat"><?php esc_html_e( '40%+ open rate', 'cartcue-sales-automation' ); ?></span>
								<?php endif; ?>
								<span class="ap-trigger-label"><?php echo esc_html( $t->get_label() ); ?></span>
								<span class="ap-trigger-desc"><?php echo esc_html( $t->get_description() ); ?></span>
							</<?php echo esc_attr( $tag ); ?>>
							<?php endforeach; ?>
						</div>
					</div>
					<?php endif; ?>
				</div>

				<div id="ap-trigger-settings" class="ap-trigger-settings-panel">
					<?php foreach ( $all_triggers as $slug => $t ) :
						$fields = $t->get_settings_fields();
						if ( empty( $fields ) ) {
							continue;
						}
						?>
					<div class="ap-tsettings-group <?php echo $wf_trigger === $slug ? 'is-visible' : ''; ?>" data-trigger="<?php echo esc_attr( $slug ); ?>">
						<?php foreach ( $fields as $f ) :
							$fval = $wf_t_setts[ $f['key'] ] ?? ( $f['default'] ?? '' ); ?>
						<div class="ap-field-row">
							<label class="ap-field-label"><?php echo esc_html( $f['label'] ); ?></label>
							<?php if ( 'select' === ( $f['type'] ?? '' ) ) : ?>
							<select name="trigger_settings[<?php echo esc_attr( $f['key'] ); ?>]" class="ap-select">
								<?php foreach ( ( $f['options'] ?? [] ) as $ov => $ol ) : ?>
								<option value="<?php echo esc_attr( $ov ); ?>" <?php selected( $fval, $ov ); ?>><?php echo esc_html( $ol ); ?></option>
								<?php endforeach; ?>
							</select>
							<?php else : ?>
							<input type="<?php echo esc_attr( $f['type'] ?? 'text' ); ?>" class="ap-input" name="trigger_settings[<?php echo esc_attr( $f['key'] ); ?>]" value="<?php echo esc_attr( $fval ); ?>">
							<?php endif; ?>
						</div>
						<?php endforeach; ?>
					</div>
					<?php endforeach; ?>
				</div>

				<div id="ap-ghost-gate" class="ap-ghost-gate" <?php echo $is_ghost_trigger ? '' : 'style="display:none"'; ?>>
					<?php if ( $is_ghost_trigger && $current_trigger ) :
						echo wp_kses_post( $current_trigger->get_upgrade_html() );
					endif; ?>
				</div>
			</section>

			<section class="ap-uni-block ap-uni-block--and" id="ap-uni-and">
				<div class="ap-uni-block__keyword">
					<span class="ap-uni-keyword ap-uni-keyword--conjunction"><?php esc_html_e( 'and', 'cartcue-sales-automation' ); ?></span>
					<span class="ap-uni-keyword-sub"><?php esc_html_e( 'the customer matches…', 'cartcue-sales-automation' ); ?></span>
				</div>

				<div id="ap-rules-list" class="ap-uni-rules-list">
					<?php if ( empty( $wf_rules ) ) : ?>
					<p class="ap-rules-empty"><?php esc_html_e( 'Everyone who matches the trigger. Add filters to narrow down by role, order history, or product.', 'cartcue-sales-automation' ); ?></p>
					<?php endif; ?>
					<?php foreach ( $wf_rules as $i => $rule ) :
						$rule_obj = $plugin->get_rule( $rule['slug'] ?? '' ); ?>
					<div class="ap-rule-row ap-rule-row--sentence" data-index="<?php echo esc_attr( $i ); ?>">
						<span class="ap-rule-prefix"><?php esc_html_e( 'Customer', 'cartcue-sales-automation' ); ?></span>
						<select name="rules[<?php echo esc_attr( $i ); ?>][slug]" class="ap-select ap-rule-slug">
							<option value="">— <?php esc_html_e( 'Choose a filter', 'cartcue-sales-automation' ); ?> —</option>
							<?php foreach ( $plugin->get_rules() as $rs => $ro ) : ?>
							<option value="<?php echo esc_attr( $rs ); ?>" <?php selected( $rule['slug'] ?? '', $rs ); ?>><?php echo esc_html( $ro->get_label() ); ?></option>
							<?php endforeach; ?>
						</select>
						<?php if ( $rule_obj ) : ?>
						<select name="rules[<?php echo esc_attr( $i ); ?>][operator]" class="ap-select ap-rule-op">
							<?php foreach ( $rule_obj->get_operators() as $ok => $ol ) : ?>
							<option value="<?php echo esc_attr( $ok ); ?>" <?php selected( $rule['operator'] ?? '', $ok ); ?>><?php echo esc_html( $ol ); ?></option>
							<?php endforeach; ?>
						</select>
						<?php if ( 'hidden' !== $rule_obj->get_value_type() ) : ?>
						<input type="text" class="ap-input ap-rule-val" name="rules[<?php echo esc_attr( $i ); ?>][value]" value="<?php echo esc_attr( $rule['value'] ?? '' ); ?>">
						<?php endif; ?>
						<?php endif; ?>
						<button type="button" class="ap-icon-btn ap-remove-rule" aria-label="<?php esc_attr_e( 'Remove filter', 'cartcue-sales-automation' ); ?>">✕</button>
					</div>
					<?php endforeach; ?>
				</div>
				<button type="button" id="ap-add-rule" class="ap-btn ap-btn-ghost ap-btn-sm">+ <?php esc_html_e( 'Add audience filter', 'cartcue-sales-automation' ); ?></button>
				<div id="cartcue-audience-count" class="ap-audience-count"></div>
			</section>

			<section class="ap-uni-block ap-uni-block--wait" id="ap-uni-wait">
				<div class="ap-uni-block__keyword">
					<span class="ap-uni-keyword"><?php esc_html_e( 'Wait', 'cartcue-sales-automation' ); ?></span>
					<span class="ap-uni-keyword-sub"><?php esc_html_e( 'how long after?', 'cartcue-sales-automation' ); ?></span>
				</div>

				<div class="ap-uni-timing-pills">
					<?php
					$primary = [
						[ 'type' => 'immediate', 'v' => 0, 'u' => '', 'label' => __( 'Right away', 'cartcue-sales-automation' ) ],
						[ 'type' => 'delay', 'v' => 1, 'u' => 'hours', 'label' => __( '1 hour', 'cartcue-sales-automation' ), 'recommended' => true ],
						[ 'type' => 'delay', 'v' => 1, 'u' => 'days', 'label' => __( '1 day', 'cartcue-sales-automation' ) ],
					];
					foreach ( $primary as $opt ) :
						$is_sel = ( 'immediate' === $opt['type'] && 'immediate' === $timing_type )
							|| ( 'delay' === $opt['type'] && 'delay' === $timing_type && (int) $opt['v'] === $delay_val && $opt['u'] === $delay_unit );
						?>
					<button type="button"
							class="ap-timing-pill<?php echo $is_sel ? ' is-selected' : ''; ?><?php echo ! empty( $opt['recommended'] ) ? ' is-recommended' : ''; ?>"
							data-type="<?php echo esc_attr( $opt['type'] ); ?>"
							data-value="<?php echo esc_attr( (string) $opt['v'] ); ?>"
							data-unit="<?php echo esc_attr( $opt['u'] ); ?>">
						<?php echo esc_html( $opt['label'] ); ?>
						<?php if ( ! empty( $opt['recommended'] ) ) : ?>
						<span class="ap-timing-recommended"><?php esc_html_e( 'recommended', 'cartcue-sales-automation' ); ?></span>
						<?php endif; ?>
					</button>
					<?php endforeach; ?>
					<button type="button" class="ap-timing-pill ap-timing-pill--custom-toggle">
						<?php esc_html_e( 'Custom…', 'cartcue-sales-automation' ); ?>
					</button>
				</div>

				<?php
				$custom_timing_open = 'delay' === $timing_type
					&& ! ( 1 === $delay_val && in_array( $delay_unit, [ 'hours', 'days' ], true ) );
				?>
				<div id="ap-timing-custom-panel" class="ap-timing-custom-panel<?php echo $custom_timing_open ? ' is-open' : ''; ?>">
					<input type="number" id="ap-custom-val" class="ap-input ap-input--tiny" min="1" value="<?php echo esc_attr( $delay_val ); ?>">
					<select id="ap-custom-unit" class="ap-select ap-select--sm">
						<option value="minutes" <?php selected( $delay_unit, 'minutes' ); ?>><?php esc_html_e( 'minutes', 'cartcue-sales-automation' ); ?></option>
						<option value="hours" <?php selected( $delay_unit, 'hours' ); ?>><?php esc_html_e( 'hours', 'cartcue-sales-automation' ); ?></option>
						<option value="days" <?php selected( $delay_unit, 'days' ); ?>><?php esc_html_e( 'days', 'cartcue-sales-automation' ); ?></option>
					</select>
					<button type="button" id="ap-custom-timing-btn" class="ap-btn ap-btn-sm"><?php esc_html_e( 'Set', 'cartcue-sales-automation' ); ?></button>
				</div>
			</section>

			<section class="ap-uni-block ap-uni-block--then" id="ap-uni-then">
				<div class="ap-uni-block__keyword">
					<span class="ap-uni-keyword"><?php esc_html_e( 'Then', 'cartcue-sales-automation' ); ?></span>
					<span class="ap-uni-keyword-sub"><?php esc_html_e( 'do this:', 'cartcue-sales-automation' ); ?></span>
				</div>

				<div id="ap-actions-list" class="ap-uni-actions">
					<?php foreach ( $wf_actions as $i => $action_row ) :
						$action_type = $action_row['type'] ?? '';
						$action_obj  = $plugin->get_action( $action_type );
						$has_type    = '' !== $action_type && $action_obj;
						?>
					<div class="ap-action-card<?php echo $has_type ? ' ap-action-card--has-type' : ''; ?><?php echo ( $has_type && 'send_email' === $action_type ) ? ' ap-action-card--email' : ''; ?>" data-index="<?php echo esc_attr( $i ); ?>" data-play-id="<?php echo esc_attr( (string) $play_id ); ?>">
						<input type="hidden" name="actions[<?php echo esc_attr( $i ); ?>][type]" class="ap-action-type" value="<?php echo esc_attr( $action_type ); ?>">

						<div class="ap-action-inserter-slot" data-card-index="<?php echo esc_attr( (string) $i ); ?>"<?php echo $has_type ? ' hidden' : ''; ?>>
							<button type="button" class="ap-btn ap-btn-ghost ap-action-inserter-trigger">
								+ <?php esc_html_e( 'Choose what happens', 'cartcue-sales-automation' ); ?>
							</button>
						</div>

						<div class="ap-action-card__header" <?php echo $has_type ? '' : 'style="display:none"'; ?>>
							<div class="ap-action-card__header-main">
								<span class="ap-action-type-label"><?php echo $action_obj ? esc_html( $action_obj->get_label() ) : ''; ?></span>
								<button type="button" class="ap-btn ap-btn-ghost ap-btn-sm ap-change-action-type"><?php esc_html_e( 'Change', 'cartcue-sales-automation' ); ?></button>
							</div>
							<button type="button" class="ap-icon-btn ap-remove-action" aria-label="<?php esc_attr_e( 'Remove action', 'cartcue-sales-automation' ); ?>">✕</button>
						</div>

						<?php if ( $action_obj ) : ?>
						<div class="ap-action-fields">
							<?php foreach ( $action_obj->get_settings_fields() as $f ) :
								$fv = $action_row['settings'][ $f['key'] ] ?? ( $f['default'] ?? '' );
								if ( 'hidden' === ( $f['type'] ?? '' ) ) : ?>
							<input type="hidden" name="actions[<?php echo esc_attr( $i ); ?>][settings][<?php echo esc_attr( $f['key'] ); ?>]" value="<?php echo esc_attr( $fv ); ?>">
							<?php continue; endif; ?>
							<div class="ap-field-row">
								<label class="ap-field-label"><?php echo esc_html( $f['label'] ); ?></label>
								<?php if ( 'textarea' === ( $f['type'] ?? '' ) ) : ?>
								<textarea name="actions[<?php echo esc_attr( $i ); ?>][settings][<?php echo esc_attr( $f['key'] ); ?>]"
										  class="ap-textarea<?php echo 'body' === $f['key'] ? ' ap-gutenberg-source' : ''; ?>"
										  rows="<?php echo esc_attr( $f['rows'] ?? 5 ); ?>"
										  data-trigger-slug="<?php echo esc_attr( $wf_trigger ); ?>"
										  placeholder="<?php echo esc_attr( $f['placeholder'] ?? '' ); ?>"><?php echo esc_textarea( $fv ); ?></textarea>
								<?php elseif ( 'select' === ( $f['type'] ?? '' ) ) : ?>
								<select name="actions[<?php echo esc_attr( $i ); ?>][settings][<?php echo esc_attr( $f['key'] ); ?>]" class="ap-select">
									<?php foreach ( ( $f['options'] ?? [] ) as $ov => $ol ) : ?>
									<option value="<?php echo esc_attr( $ov ); ?>" <?php selected( $fv, $ov ); ?>><?php echo esc_html( $ol ); ?></option>
									<?php endforeach; ?>
								</select>
								<?php else : ?>
								<input type="<?php echo esc_attr( $f['type'] ?? 'text' ); ?>"
									   name="actions[<?php echo esc_attr( $i ); ?>][settings][<?php echo esc_attr( $f['key'] ); ?>]"
									   class="ap-input"
									   value="<?php echo esc_attr( $fv ); ?>"
									   placeholder="<?php echo esc_attr( $f['placeholder'] ?? '' ); ?>">
								<?php endif; ?>
								<?php if ( ! empty( $f['description'] ) ) : ?>
								<p class="ap-field-hint"><?php echo esc_html( $f['description'] ); ?></p>
								<?php endif; ?>
							</div>
							<?php endforeach; ?>
							<?php if ( ! $is_pro && 'create_coupon' === $action_type && method_exists( $action_obj, 'get_pro_feature_labels' ) ) : ?>
							<div class="ap-action-pro-upsell">
								<p class="ap-action-pro-upsell__title"><?php esc_html_e( 'Need unique codes per customer?', 'cartcue-sales-automation' ); ?></p>
								<ul class="ap-action-pro-upsell__list">
									<?php foreach ( $action_obj->get_pro_feature_labels() as $pro_label ) : ?>
									<li><?php echo esc_html( $pro_label ); ?></li>
									<?php endforeach; ?>
								</ul>
								<a href="<?php echo esc_url( \CartCue\Plugin::upgrade_url( 'action_create_coupon' ) ); ?>" target="_blank" rel="noopener noreferrer" class="ap-btn ap-btn-primary ap-btn-sm">
									<?php esc_html_e( 'Upgrade to CartCue Pro', 'cartcue-sales-automation' ); ?> →
								</a>
							</div>
							<?php endif; ?>
						</div>
						<?php endif; ?>
					</div>
					<?php endforeach; ?>
				</div>

				<button type="button" id="ap-add-action" class="ap-btn ap-btn-ghost ap-btn-sm">
					+ <?php esc_html_e( 'Send another nudge after that', 'cartcue-sales-automation' ); ?>
				</button>
			</section>

		</div>

		<details class="ap-uni-advanced" id="ap-uni-advanced">
			<summary>
				<span class="ap-uni-advanced__icon" aria-hidden="true">⚙</span>
				<?php esc_html_e( 'Advanced settings', 'cartcue-sales-automation' ); ?>
				<span class="ap-uni-advanced__hint"><?php esc_html_e( '(frequency, campaign — most people skip this)', 'cartcue-sales-automation' ); ?></span>
			</summary>
			<div class="ap-uni-advanced__body">
				<div class="ap-uni-advanced__row">
					<div class="ap-uni-advanced__row-label"><?php esc_html_e( 'How often per customer?', 'cartcue-sales-automation' ); ?></div>
					<div class="ap-freq-pills">
						<button type="button" class="ap-freq-pill <?php echo ( 0 === $max_runs && 0 === $cooldown ) ? 'is-selected' : ''; ?>" data-max-runs="0" data-cooldown="0">
							<?php esc_html_e( 'Every time', 'cartcue-sales-automation' ); ?>
						</button>
						<button type="button" class="ap-freq-pill <?php echo ( 1 === $max_runs && 0 === $cooldown ) ? 'is-selected' : ''; ?>" data-max-runs="1" data-cooldown="0">
							<?php esc_html_e( 'Once per customer', 'cartcue-sales-automation' ); ?>
							<span class="ap-freq-pill__hint"><?php esc_html_e( 'default', 'cartcue-sales-automation' ); ?></span>
						</button>
						<button type="button" class="ap-freq-pill <?php echo $cooldown > 0 ? 'is-selected' : ''; ?>" data-custom="cooldown" data-max-runs="0" data-cooldown="custom">
							<?php esc_html_e( 'At most every', 'cartcue-sales-automation' ); ?>
							<input type="number" id="ap-cooldown-val" class="ap-input ap-input--tiny" value="<?php echo esc_attr( $cooldown ?: 30 ); ?>" min="1" onclick="event.stopPropagation()">
							<?php esc_html_e( 'days', 'cartcue-sales-automation' ); ?>
						</button>
					</div>
				</div>

				<?php if ( ! $play || 'paused' === $wf_status ) : ?>
				<div class="ap-uni-advanced__row" id="ap-past-matches-row">
					<label class="ap-checkbox-label">
						<input type="checkbox" name="send_to_past_matches" value="1" id="ap-past-matches-checkbox" <?php checked( (int) ( $play['send_to_past_matches'] ?? 0 ), 1 ); ?>>
						<?php esc_html_e( 'Also apply to existing customers who already match', 'cartcue-sales-automation' ); ?>
					</label>
					<p class="description" id="ap-past-count-hint">
						<?php esc_html_e( 'Caution: this sends to everyone who matches today. We will show the count before firing.', 'cartcue-sales-automation' ); ?>
					</p>
				</div>
				<?php endif; ?>
			</div>
		</details>

		<footer class="ap-uni-save-bar">
			<div class="ap-uni-save-bar__inner">
				<button type="submit" class="ap-btn ap-btn-ghost" id="ap-uni-save-draft" data-set-status="paused">
					<?php esc_html_e( 'Save draft', 'cartcue-sales-automation' ); ?>
				</button>
				<button type="submit" class="ap-btn ap-btn-primary ap-btn-lg" id="ap-uni-activate" data-set-status="active"<?php echo $is_ghost_trigger ? ' disabled' : ''; ?>>
					<?php
					if ( $play_id && 'active' === $wf_status ) {
						esc_html_e( 'Save changes ↻', 'cartcue-sales-automation' );
					} elseif ( $play_id ) {
						esc_html_e( 'Activate this play →', 'cartcue-sales-automation' );
					} else {
						esc_html_e( 'Activate →', 'cartcue-sales-automation' );
					}
					?>
				</button>
			</div>
			<?php if ( $at_active_limit && 'active' !== $wf_status ) : ?>
			<p class="ap-uni-save-bar__limit">
				<?php
				printf(
					/* translators: %d: max active plays on free plan */
					esc_html__( 'Free plan: %d active automations max. Pause another or upgrade.', 'cartcue-sales-automation' ),
					(int) $max_active_plays
				);
				?>
			</p>
			<?php endif; ?>
		</footer>

	</form>
</div>
<?php
/* phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound */
