<?php
/**
 * Notifications tab — the order event matrix.
 *
 * @package TextTango
 *
 * @var array<string, mixed> $args View data supplied by SettingsPage::render().
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

use TextTango\WooCommerce\Events;
use TextTango\WooCommerce\Templates;

$texttango_settings = (array) ( $args['settings'] ?? array() );
$texttango_events   = (array) ( $args['events'] ?? array() );
?>
<?php if ( empty( $args['woo_active'] ) ) : ?>
	<div class="tt-notice tt-notice-warn tt-mb-4">
		<strong><?php esc_html_e( 'WooCommerce is not active.', 'texttango' ); ?></strong>
		<?php esc_html_e( 'These settings can be saved, but no order notifications will send until WooCommerce is running.', 'texttango' ); ?>
	</div>
<?php endif; ?>

<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( 'Order notifications', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-5 tt-text-sm tt-text-slate-500">
			<?php esc_html_e( 'One SMS per order event, sent to the order\'s billing phone. Everything is off until you switch it on. Open a row to edit its message.', 'texttango' ); ?>
		</p>

		<?php
		/*
		 * The tag list used to live here, once, for everything. It moved into each
		 * event because the sets are no longer the same: a low-stock alert cannot
		 * resolve {order_number}, and an editor that offers it is offering a tag
		 * that renders as a hole in the sentence.
		 */
		?>
		<p class="tt-mb-5 tt-rounded-md tt-bg-slate-50 tt-p-4 tt-text-xs tt-text-slate-500">
			<?php esc_html_e( 'Open an event to write its message. The tags it can use are listed with it — click one to insert it.', 'texttango' ); ?>
		</p>

		<div class="tt-overflow-hidden tt-rounded-md tt-border tt-border-solid tt-border-slate-200">
			<?php foreach ( $texttango_events as $texttango_id => $texttango_event ) : ?>
				<?php
				$texttango_slug       = (string) $texttango_id;
				$texttango_on         = Events::is_enabled( $texttango_slug );
				$texttango_customised = Events::is_customised( $texttango_slug );
				$texttango_default    = Events::default_template( $texttango_slug );
				$texttango_body       = Events::template( $texttango_slug );
				$texttango_set        = Events::tag_set( $texttango_slug );
				$texttango_preview    = Templates::render( $texttango_body, Templates::sample_for( $texttango_set ) );
				$texttango_estimate   = Templates::estimate( $texttango_preview );
				$texttango_is_admin   = Events::AUDIENCE_ADMIN === $texttango_event['audience'];
				?>
				<details class="tt-border-0 tt-border-b tt-border-solid tt-border-slate-200 last:tt-border-b-0" data-texttango-event>
					<summary class="tt-flex tt-cursor-pointer tt-flex-wrap tt-items-center tt-gap-3 tt-px-4 tt-py-3 hover:tt-bg-slate-50">
						<label class="tt-flex tt-items-center" data-texttango-toggle>
							<input
								type="checkbox"
								name="event_enabled[]"
								value="<?php echo esc_attr( $texttango_slug ); ?>"
								<?php checked( $texttango_on ); ?> />
							<span class="tt-sr-only"><?php echo esc_html( (string) $texttango_event['label'] ); ?></span>
						</label>

						<span class="tt-grow tt-text-sm tt-font-medium tt-text-slate-800">
							<?php echo esc_html( (string) $texttango_event['label'] ); ?>
						</span>

						<span class="tt-badge <?php echo $texttango_is_admin ? 'tt-badge-warn' : 'tt-badge-off'; ?>">
							<?php
							echo $texttango_is_admin
								? esc_html__( 'Staff', 'texttango' )
								: esc_html__( 'Customer', 'texttango' );
							?>
						</span>

						<span class="tt-w-16 tt-text-right tt-text-xs tt-text-slate-500" data-texttango-segments>
							<?php
							printf(
								/* translators: %d: number of SMS segments. */
								esc_html( _n( '%d SMS', '%d SMS', (int) $texttango_estimate['segments'], 'texttango' ) ),
								(int) $texttango_estimate['segments']
							);
							?>
						</span>

						<span class="tt-badge <?php echo $texttango_customised ? 'tt-badge-ok' : 'tt-badge-off'; ?>" data-texttango-state>
							<?php
							echo $texttango_customised
								? esc_html__( 'Customised', 'texttango' )
								: esc_html__( 'Default', 'texttango' );
							?>
						</span>
					</summary>

					<div class="tt-border-0 tt-border-t tt-border-solid tt-border-slate-100 tt-bg-slate-50 tt-px-4 tt-py-4">
						<p class="tt-m-0 tt-mb-3 tt-text-sm tt-text-slate-500">
							<?php echo esc_html( (string) $texttango_event['description'] ); ?>
						</p>

						<div class="tt-mb-1 tt-flex tt-items-center tt-justify-between">
							<label class="tt-label tt-mb-0" for="texttango-tpl-<?php echo esc_attr( $texttango_slug ); ?>">
								<?php esc_html_e( 'Message', 'texttango' ); ?>
							</label>
							<button
								type="button"
								class="tt-cursor-pointer tt-border-0 tt-bg-transparent tt-p-0 tt-text-xs tt-font-medium tt-text-brand-600 tt-underline"
								data-texttango-action="reset-template"
								<?php disabled( ! $texttango_customised ); ?>>
								<?php esc_html_e( 'Reset to default', 'texttango' ); ?>
							</button>
						</div>

						<textarea
							class="tt-input tt-bg-white"
							id="texttango-tpl-<?php echo esc_attr( $texttango_slug ); ?>"
							name="event_template[<?php echo esc_attr( $texttango_slug ); ?>]"
							rows="3"
							data-texttango-template
							data-default="<?php echo esc_attr( $texttango_default ); ?>"><?php echo esc_textarea( $texttango_body ); ?></textarea>

						<p class="tt-hint">
							<?php esc_html_e( 'Preview:', 'texttango' ); ?>
							<span class="tt-text-slate-700" data-texttango-preview><?php echo esc_html( $texttango_preview ); ?></span>
						</p>
						<p class="tt-hint" data-texttango-estimate>
							<?php
							printf(
								/* translators: 1: character count, 2: encoding name, 3: number of SMS segments. */
								esc_html__( '%1$d characters, %2$s encoding, %3$d SMS', 'texttango' ),
								(int) $texttango_estimate['length'],
								esc_html( (string) $texttango_estimate['encoding'] ),
								(int) $texttango_estimate['segments']
							);
							?>
						</p>

						<div class="tt-mt-3 tt-flex tt-flex-wrap tt-gap-x-3 tt-gap-y-2">
							<?php foreach ( Templates::tags_for( $texttango_set ) as $texttango_tag => $texttango_desc ) : ?>
								<?php
								$texttango_tag_title = sprintf(
									/* translators: 1: merge tag, e.g. {first_name}, 2: what it resolves to. */
									__( 'Insert %1$s — %2$s', 'texttango' ),
									(string) $texttango_tag,
									(string) $texttango_desc
								);
								?>
								<button type="button" class="tt-tag" data-texttango-insert-tag="<?php echo esc_attr( (string) $texttango_tag ); ?>" title="<?php echo esc_attr( $texttango_tag_title ); ?>"><?php echo esc_html( (string) $texttango_tag ); ?></button>
							<?php endforeach; ?>
						</div>
					</div>
				</details>
			<?php endforeach; ?>
		</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( 'Staff numbers', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-4 tt-text-sm tt-text-slate-500">
			<?php esc_html_e( 'Where staff alerts go. One number per line, or separated by commas. Each number gets its own message, so three numbers cost three SMS per alert.', 'texttango' ); ?>
		</p>
		<textarea class="tt-input" name="admin_recipients" rows="3" placeholder="+233…"><?php echo esc_textarea( (string) $texttango_settings['admin_recipients'] ); ?></textarea>
		<p class="tt-hint"><?php esc_html_e( 'Saved in E.164.', 'texttango' ); ?></p>

		<label class="tt-mt-4 tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
			<input type="checkbox" name="order_notes_enabled" value="1" class="tt-mt-0.5"
				<?php checked( ! empty( $texttango_settings['order_notes_enabled'] ) ); ?> />
			<span>
				<?php esc_html_e( 'Record every SMS as a private order note', 'texttango' ); ?>
				<span class="tt-hint tt-block">
					<?php esc_html_e( 'Notes say what was queued, skipped, or failed, and which number it went to.', 'texttango' ); ?>
				</span>
			</span>
		</label>
	</div>

	<div class="tt-flex tt-flex-wrap tt-items-center tt-gap-3">
		<button type="submit" class="tt-btn tt-btn-primary">
			<?php esc_html_e( 'Save settings', 'texttango' ); ?>
		</button>
	</div>
</form>
