<?php
/**
 * Marketing tab — abandoned cart recovery.
 *
 * @package TextTango
 *
 * @var array<string, mixed> $args View data supplied by SettingsPage::render().
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

use TextTango\Marketing\AbandonedCart;

$texttango_settings = (array) ( $args['settings'] ?? array() );
$texttango_tags     = (array) ( $args['cart_tags'] ?? array() );
$texttango_on       = ! empty( $texttango_settings['abandoned_cart_enabled'] );
?>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
	<input type="hidden" name="action" value="<?php echo esc_attr( (string) $args['save_action'] ); ?>" />
	<input type="hidden" name="texttango_tab" value="<?php echo esc_attr( (string) $args['tab'] ); ?>" />
	<?php wp_nonce_field( (string) $args['save_nonce'] ); ?>

	<div class="tt-card tt-mb-4">
		<h2 class="tt-m-0 tt-mb-1 tt-p-0 tt-text-lg tt-font-semibold tt-text-slate-900">
			<?php esc_html_e( 'Abandoned baskets', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-5 tt-text-sm tt-text-slate-500">
			<?php esc_html_e( 'A reminder to somebody who filled a basket and left without buying.', 'texttango' ); ?>
		</p>

		<!-- The limitation, stated where the decision is made rather than
			discovered later from a report that is smaller than expected. -->
		<div class="tt-notice tt-notice-warn tt-mb-5">
			<?php esc_html_e( 'Signed-in customers only, and only those who agreed to marketing messages and have a usable number on file. A guest who never checked out has given this site no number and no permission to use one, so they are left alone.', 'texttango' ); ?>
		</div>

		<label class="tt-mb-5 tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
			<input type="checkbox" name="abandoned_cart_enabled" value="1" class="tt-mt-0.5"
				<?php checked( $texttango_on ); ?> />
			<span>
				<?php esc_html_e( 'Send basket reminders', 'texttango' ); ?>
				<span class="tt-hint tt-block">
					<?php esc_html_e( 'Every change to a basket restarts the clock, so somebody still shopping is not interrupted.', 'texttango' ); ?>
				</span>
			</span>
		</label>

		<div class="tt-mb-5">
			<label class="tt-label" for="texttango-cart-delay"><?php esc_html_e( 'Wait before the first reminder', 'texttango' ); ?></label>
			<input
				class="tt-input"
				id="texttango-cart-delay"
				name="abandoned_cart_delay"
				type="number"
				min="1"
				max="10080"
				step="1"
				value="<?php echo esc_attr( (string) $texttango_settings['abandoned_cart_delay'] ); ?>" />
			<p class="tt-hint"><?php esc_html_e( 'Minutes of no activity on the basket. Sixty is a common starting point.', 'texttango' ); ?></p>
		</div>

		<div class="tt-mb-5">
			<label class="tt-label" for="texttango-cart-template"><?php esc_html_e( 'First reminder', 'texttango' ); ?></label>
			<textarea
				class="tt-input"
				id="texttango-cart-template"
				name="abandoned_cart_template"
				rows="3"
				placeholder="<?php echo esc_attr( AbandonedCart::default_template( AbandonedCart::STAGE_FIRST ) ); ?>"
			><?php echo esc_textarea( (string) $texttango_settings['abandoned_cart_template'] ); ?></textarea>
			<p class="tt-hint"><?php esc_html_e( 'Leave blank for the default.', 'texttango' ); ?></p>
		</div>

		<div class="tt-mb-5">
			<label class="tt-label" for="texttango-cart-followup"><?php esc_html_e( 'Follow-up', 'texttango' ); ?></label>
			<input
				class="tt-input"
				id="texttango-cart-followup"
				name="abandoned_cart_followup_hours"
				type="number"
				min="0"
				max="336"
				step="1"
				value="<?php echo esc_attr( (string) $texttango_settings['abandoned_cart_followup_hours'] ); ?>" />
			<p class="tt-hint"><?php esc_html_e( 'Hours after the first reminder to send a second one. Zero sends only one — which is the setting to keep unless a second is genuinely worth somebody\'s attention.', 'texttango' ); ?></p>
		</div>

		<div class="tt-mb-2">
			<label class="tt-label" for="texttango-cart-followup-template"><?php esc_html_e( 'Follow-up message', 'texttango' ); ?></label>
			<textarea
				class="tt-input"
				id="texttango-cart-followup-template"
				name="abandoned_cart_followup_template"
				rows="3"
				placeholder="<?php echo esc_attr( AbandonedCart::default_template( AbandonedCart::STAGE_FOLLOWUP ) ); ?>"
			><?php echo esc_textarea( (string) $texttango_settings['abandoned_cart_followup_template'] ); ?></textarea>
			<p class="tt-hint"><?php esc_html_e( 'Only used when the follow-up is set to something other than zero.', 'texttango' ); ?></p>
		</div>
	</div>

	<div class="tt-card tt-mb-4">
		<h2 class="tt-m-0 tt-mb-1 tt-p-0 tt-text-lg tt-font-semibold tt-text-slate-900">
			<?php esc_html_e( 'Merge tags', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-4 tt-text-sm tt-text-slate-500">
			<?php esc_html_e( 'These are the only tags a basket reminder can resolve. Order tags have no meaning here — there is no order.', 'texttango' ); ?>
		</p>

		<dl class="tt-m-0 tt-grid tt-grid-cols-1 tt-gap-2 sm:tt-grid-cols-2">
			<?php foreach ( $texttango_tags as $texttango_tag => $texttango_meaning ) : ?>
				<div class="tt-flex tt-items-baseline tt-gap-2">
					<dt><code class="tt-text-xs"><?php echo esc_html( (string) $texttango_tag ); ?></code></dt>
					<dd class="tt-m-0 tt-text-xs tt-text-slate-500"><?php echo esc_html( (string) $texttango_meaning ); ?></dd>
				</div>
			<?php endforeach; ?>
		</dl>
	</div>

	<p class="tt-m-0">
		<button type="submit" class="tt-btn tt-btn-primary"><?php esc_html_e( 'Save changes', 'texttango' ); ?></button>
	</p>
</form>
