<?php
/**
 * Lead Form Generator — Builder UI
 *
 * Renders the admin-facing form builder (Card A: behavior, Card B: steps/questions).
 * All state is serialized into a single hidden JSON field and submitted to
 * options.php under the Niwa_AI_Constants::OPT_LEAD_FORM_GENERATOR option.
 *
 * @package Niwa_AI
 * @since   0.12.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Niwa_AI_Lead_Form_Builder_UI {

	/**
	 * Render the builder page.
	 *
	 * @return void
	 */
	public static function render() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$opts      = Niwa_AI_Lead_Form_Schema::get();
		$option    = Niwa_AI_Constants::OPT_LEAD_FORM_GENERATOR;
		$media_id  = isset( $opts['completion_media_id'] ) ? absint( $opts['completion_media_id'] ) : 0;
		$media_url = $media_id ? wp_get_attachment_url( $media_id ) : '';
		$mime_type = $media_id ? (string) get_post_mime_type( $media_id ) : '';
		$is_image  = $mime_type && 0 === strpos( $mime_type, 'image/' );
		$is_video  = $mime_type && 0 === strpos( $mime_type, 'video/' );

		echo '<form method="post" action="' . esc_url( admin_url( 'options.php' ) ) . '" id="niwa-lf-form">';
		settings_fields( 'niwa_ai_lead_form_generator_group' );

		// ──────────────────────────────────────────────────────────
		// Card A — Behavior
		// ──────────────────────────────────────────────────────────
		echo '<div class="niwa-card">';
		echo '<details class="niwa-collapsible" open>';
		echo '<summary>';
		echo '<div class="niwa-card__header">';
		echo '<div class="niwa-card__icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 2v4"/><path d="M12 18v4"/><path d="M4.93 4.93l2.83 2.83"/><path d="M16.24 16.24l2.83 2.83"/><path d="M2 12h4"/><path d="M18 12h4"/><path d="M4.93 19.07l2.83-2.83"/><path d="M16.24 7.76l2.83-2.83"/></svg></div>';
		echo '<h2 class="niwa-card__title">' . esc_html__( 'Behavior', 'niwa-ai' ) . '</h2>';
		echo '</div>';
		echo '</summary>';
		echo '<div class="niwa-card__body">';

		// Enabled toggle.
		echo '<div class="niwa-field">';
		echo '<label class="niwa-toggle">';
		echo '<input type="checkbox" id="niwa-lf-enabled" ' . checked( ! empty( $opts['enabled'] ), true, false ) . ' />';
		echo esc_html__( 'Enable Lead Form Generator', 'niwa-ai' );
		echo '</label>';
		echo '<span class="niwa-field__hint">' . esc_html__( 'When enabled, the form runs in the frontend chat based on the trigger you pick below.', 'niwa-ai' ) . '</span>';
		echo '</div>';

		// Mode.
		echo '<div class="niwa-field">';
		echo '<label class="niwa-field__label">' . esc_html__( 'Mode', 'niwa-ai' ) . '</label>';
		echo '<div class="niwa-lf-radios">';
		echo '<label><input type="radio" name="niwa-lf-mode" value="chat" ' . checked( $opts['mode'], 'chat', false ) . ' /> ' . esc_html__( 'Chat messages', 'niwa-ai' ) . '</label>';
		echo '<label><input type="radio" name="niwa-lf-mode" value="card" ' . checked( $opts['mode'], 'card', false ) . ' /> ' . esc_html__( 'Card form (morphed input)', 'niwa-ai' ) . '</label>';
		echo '</div>';
		echo '<span class="niwa-field__hint">' . esc_html__( 'Chat mode asks questions inline as bot messages. Card mode replaces the input with a step-by-step form.', 'niwa-ai' ) . '</span>';
		echo '</div>';

		// Trigger.
		echo '<div class="niwa-field">';
		echo '<label class="niwa-field__label">' . esc_html__( 'Trigger', 'niwa-ai' ) . '</label>';
		echo '<select id="niwa-lf-trigger">';
		$triggers = array(
			'immediate'            => __( 'Immediate (as soon as chat opens)', 'niwa-ai' ),
			'after_delay'          => __( 'After delay', 'niwa-ai' ),
			'after_first_user_msg' => __( 'After first user message', 'niwa-ai' ),
			'ai_only'              => __( 'AI agent decides (tool-triggered only)', 'niwa-ai' ),
		);
		foreach ( $triggers as $val => $lbl ) {
			echo '<option value="' . esc_attr( $val ) . '"' . selected( $opts['trigger'], $val, false ) . '>' . esc_html( $lbl ) . '</option>';
		}
		echo '</select>';
		echo '<span class="niwa-field__hint">' . esc_html__( 'Pick "AI agent decides" to let the frontend Niwa agent choose when to launch the form based on the description below. The other triggers auto-arm regardless.', 'niwa-ai' ) . '</span>';
		echo '</div>';

		// Trigger delay ms.
		echo '<div class="niwa-field">';
		echo '<label class="niwa-field__label">' . esc_html__( 'Trigger delay (ms)', 'niwa-ai' ) . '</label>';
		echo '<input type="number" id="niwa-lf-trigger-delay" value="' . esc_attr( (int) $opts['trigger_delay_ms'] ) . '" min="0" max="60000" step="100" />';
		echo '<span class="niwa-field__hint">' . esc_html__( 'Used when Trigger is "After delay". 0–60000 ms.', 'niwa-ai' ) . '</span>';
		echo '</div>';

		// Trigger description (AI agent guidance).
		echo '<div class="niwa-field">';
		echo '<label class="niwa-field__label">' . esc_html__( 'Trigger description (for the AI agent)', 'niwa-ai' ) . '</label>';
		echo '<textarea id="niwa-lf-trigger-description" rows="4" class="niwa-input--wide" style="max-width:100%;" placeholder="' . esc_attr__( 'Example: Trigger this form when the visitor asks about joining the Platinum Club newsletter and confirms they want to join.', 'niwa-ai' ) . '">' . esc_textarea( isset( $opts['trigger_description'] ) ? (string) $opts['trigger_description'] : '' ) . '</textarea>';
		echo '<span class="niwa-field__hint">' . esc_html__( 'Describe — in plain language — when the frontend Niwa agent should launch this form. The text is fed into the `activate_lead_form` tool description so the model can match user intent. Required when Trigger = "AI agent decides".', 'niwa-ai' ) . '</span>';
		echo '</div>';

		// Partial submit.
		echo '<div class="niwa-field">';
		echo '<label class="niwa-toggle">';
		echo '<input type="checkbox" id="niwa-lf-partial-submit" ' . checked( ! empty( $opts['partial_submit'] ), true, false ) . ' />';
		echo esc_html__( 'Partial submit on email/phone', 'niwa-ai' );
		echo '</label>';
		echo '<span class="niwa-field__hint">' . esc_html__( 'As soon as a valid email or phone answer is captured, send the lead (marked as partial) even if the visitor abandons the form before finishing.', 'niwa-ai' ) . '</span>';
		echo '</div>';

		// Completion text.
		echo '<div class="niwa-field">';
		echo '<label class="niwa-field__label">' . esc_html__( 'Completion text', 'niwa-ai' ) . '</label>';
		echo '<textarea id="niwa-lf-completion-text" rows="3" class="niwa-input--wide" style="max-width:100%;">' . esc_textarea( (string) $opts['completion_text'] ) . '</textarea>';
		echo '<span class="niwa-field__hint">' . esc_html__( 'Shown to the visitor after they finish the form.', 'niwa-ai' ) . '</span>';
		echo '</div>';

		// Completion media picker.
		echo '<div class="niwa-field">';
		echo '<label class="niwa-field__label">' . esc_html__( 'Completion media (image / GIF / video)', 'niwa-ai' ) . '</label>';
		echo '<span class="niwa-field__hint">' . esc_html__( 'Optional celebratory GIF or image shown next to the completion text.', 'niwa-ai' ) . '</span>';
		echo '<input type="hidden" id="niwa_lf_completion_media_id" value="' . esc_attr( (string) $media_id ) . '" />';
		echo '<div style="display:flex;gap:8px;margin-bottom:8px;">';
		echo '<button type="button" class="button" id="niwa_lf_completion_media_btn">' . esc_html__( 'Choose Media', 'niwa-ai' ) . '</button>';
		$hide = $media_id ? '' : ' style="display:none;"';
		echo '<button type="button" class="button" id="niwa_lf_completion_media_remove"' . $hide . '>' . esc_html__( 'Remove', 'niwa-ai' ) . '</button>';
		echo '</div>';
		echo '<div id="niwa_lf_completion_media_preview" style="margin-top:4px;">';
		if ( $media_url ) {
			if ( $is_image ) {
				echo '<img src="' . esc_url( $media_url ) . '" alt="" style="max-width:200px;max-height:150px;display:block;border-radius:8px;" />';
			} elseif ( $is_video ) {
				echo '<video controls style="max-width:200px;max-height:150px;display:block;border-radius:8px;"><source src="' . esc_url( $media_url ) . '" type="' . esc_attr( $mime_type ) . '" /></video>';
			}
			echo '<p style="margin:6px 0 0;font-size:13px;"><strong>' . esc_html( basename( $media_url ) ) . '</strong></p>';
		}
		echo '</div>';
		echo '</div>';

		echo '</div>'; // .niwa-card__body
		echo '</details>';
		echo '</div>'; // .niwa-card

		// ──────────────────────────────────────────────────────────
		// Card B — Form Builder
		// ──────────────────────────────────────────────────────────
		echo '<div class="niwa-card">';
		echo '<details class="niwa-collapsible" open>';
		echo '<summary>';
		echo '<div class="niwa-card__header">';
		echo '<div class="niwa-card__icon"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="9" y1="13" x2="15" y2="13"/><line x1="9" y1="17" x2="15" y2="17"/></svg></div>';
		echo '<h2 class="niwa-card__title">' . esc_html__( 'Form Builder', 'niwa-ai' ) . '</h2>';
		echo '</div>';
		echo '</summary>';
		echo '<div class="niwa-card__body">';

		echo '<p class="niwa-field__hint" style="margin:0 0 12px;">' . esc_html__( 'Build your form as a sequence of steps. Each step contains one or more questions. For each question you can choose how Niwa decides what to ask next — no branching, explicit rules, AI routing, or rules with AI fallback.', 'niwa-ai' ) . '</p>';

		echo '<div id="niwa-lf-builder" data-empty-text="' . esc_attr__( 'No steps yet. Click + Add Step to start.', 'niwa-ai' ) . '">';
		echo '<div class="niwa-lf-toolbar">';
		echo '<button type="button" class="button niwa-btn niwa-btn--primary" id="niwa-lf-add-step">+ ' . esc_html__( 'Add Step', 'niwa-ai' ) . '</button>';
		echo '</div>';
		echo '<div id="niwa-lf-empty" class="niwa-lf-empty" style="display:none;">' . esc_html__( 'No steps yet. Click + Add Step to start.', 'niwa-ai' ) . '</div>';
		echo '<div id="niwa-lf-steps" class="niwa-lf-steps"></div>';
		echo '</div>';

		// Hidden JSON payload.
		echo '<input type="hidden" name="' . esc_attr( $option ) . '" id="niwa-lf-json-out" value="" />';

		echo '</div>'; // .niwa-card__body
		echo '</details>';
		echo '</div>'; // .niwa-card

		// ── Templates ──
		self::render_step_template();
		self::render_question_template();
		self::render_option_template();
		self::render_rule_template();

		// Submit.
		echo '<div style="margin-top:16px;">';
		submit_button( __( 'Save Form', 'niwa-ai' ), 'primary niwa-btn niwa-btn--primary', 'submit', false );
		echo '</div>';

		echo '</form>';
	}

	/**
	 * Step template.
	 *
	 * @return void
	 */
	private static function render_step_template() {
		?>
		<template id="niwa-lf-step-tpl">
			<div class="niwa-lf-step" data-step-id="">
				<details class="niwa-lf-step__details" open>
					<summary class="niwa-lf-step__summary">
						<span class="niwa-lf-drag" title="<?php echo esc_attr__( 'Drag to reorder', 'niwa-ai' ); ?>">&#x2630;</span>
						<span class="niwa-lf-step__title" data-title><?php echo esc_html__( 'Untitled step', 'niwa-ai' ); ?></span>
						<span class="niwa-lf-step__meta" data-qcount>0 <?php echo esc_html__( 'questions', 'niwa-ai' ); ?></span>
						<button type="button" class="button-link niwa-lf-delete" data-delete-step title="<?php echo esc_attr__( 'Delete step', 'niwa-ai' ); ?>">&#x2715;</button>
					</summary>
					<div class="niwa-lf-step__body">
						<div class="niwa-field">
							<label class="niwa-field__label"><?php echo esc_html__( 'Step label', 'niwa-ai' ); ?></label>
							<input type="text" data-step-label class="niwa-input--wide" placeholder="<?php echo esc_attr__( 'e.g. Contact info', 'niwa-ai' ); ?>" />
						</div>
						<div class="niwa-lf-questions" data-questions></div>
						<div class="niwa-lf-step__footer">
							<button type="button" class="button niwa-btn niwa-btn--secondary" data-add-question>+ <?php echo esc_html__( 'Add Question', 'niwa-ai' ); ?></button>
						</div>
					</div>
				</details>
			</div>
		</template>
		<?php
	}

	/**
	 * Question template.
	 *
	 * @return void
	 */
	private static function render_question_template() {
		$types = array(
			'text'          => __( 'Short text', 'niwa-ai' ),
			'textarea'      => __( 'Long text', 'niwa-ai' ),
			'email'         => __( 'Email', 'niwa-ai' ),
			'phone'         => __( 'Phone', 'niwa-ai' ),
			'number'        => __( 'Number', 'niwa-ai' ),
			'choice_single' => __( 'Single choice', 'niwa-ai' ),
			'choice_multi'  => __( 'Multiple choice', 'niwa-ai' ),
			'appointment'   => __( 'Appointment scheduler', 'niwa-ai' ),
		);
		?>
		<template id="niwa-lf-question-tpl">
			<div class="niwa-lf-question" data-question-id="">
				<details class="niwa-lf-question__details">
					<summary class="niwa-lf-question__summary">
						<span class="niwa-lf-drag" title="<?php echo esc_attr__( 'Drag to reorder', 'niwa-ai' ); ?>">&#x2630;</span>
						<span class="niwa-lf-question__title" data-title><?php echo esc_html__( 'Untitled question', 'niwa-ai' ); ?></span>
						<code class="niwa-lf-question__id" data-id-display></code>
						<button type="button" class="button-link niwa-lf-delete" data-delete-question title="<?php echo esc_attr__( 'Delete question', 'niwa-ai' ); ?>">&#x2715;</button>
					</summary>
					<div class="niwa-lf-question__body">
						<div class="niwa-lf-grid-2">
							<div class="niwa-field">
								<label class="niwa-field__label"><?php echo esc_html__( 'Question label', 'niwa-ai' ); ?></label>
								<input type="text" data-q-label class="niwa-input--wide" />
							</div>
							<div class="niwa-field">
								<label class="niwa-field__label"><?php echo esc_html__( 'Type', 'niwa-ai' ); ?></label>
								<select data-q-type>
									<?php foreach ( $types as $val => $lbl ) : ?>
										<option value="<?php echo esc_attr( $val ); ?>"><?php echo esc_html( $lbl ); ?></option>
									<?php endforeach; ?>
								</select>
							</div>
						</div>

						<div class="niwa-lf-grid-2">
							<div class="niwa-field">
								<label class="niwa-toggle">
									<input type="checkbox" data-q-required />
									<?php echo esc_html__( 'Required', 'niwa-ai' ); ?>
								</label>
							</div>
							<div class="niwa-field">
								<label class="niwa-field__label"><?php echo esc_html__( 'Placeholder', 'niwa-ai' ); ?></label>
								<input type="text" data-q-placeholder class="niwa-input--wide" />
							</div>
						</div>

						<!-- Options editor (choice types only) -->
						<div class="niwa-lf-options-wrap" data-options-wrap style="display:none;">
							<label class="niwa-field__label"><?php echo esc_html__( 'Options', 'niwa-ai' ); ?></label>
							<div class="niwa-lf-options" data-options></div>
							<button type="button" class="button niwa-btn niwa-btn--secondary" data-add-option>+ <?php echo esc_html__( 'Add Option', 'niwa-ai' ); ?></button>
						</div>

						<!-- Scheduler editor (appointment type only) -->
						<div class="niwa-lf-appointment-wrap" data-appointment-wrap hidden>
							<div class="niwa-lf-appointment-wrap__header">
								<strong><?php echo esc_html__( 'Appointment rules', 'niwa-ai' ); ?></strong>
								<span><?php echo esc_html__( 'Choose which Scheduler services and team members visitors may book in this step.', 'niwa-ai' ); ?></span>
							</div>
							<div class="niwa-lf-grid-2">
								<fieldset class="niwa-field">
									<legend class="niwa-field__label"><?php echo esc_html__( 'Allowed services', 'niwa-ai' ); ?></legend>
									<span class="niwa-field__hint"><?php echo esc_html__( 'No selection means all active services.', 'niwa-ai' ); ?></span>
									<div class="niwa-lf-appointment-options" data-appointment-services></div>
								</fieldset>
								<fieldset class="niwa-field">
									<legend class="niwa-field__label"><?php echo esc_html__( 'Allowed team members', 'niwa-ai' ); ?></legend>
									<span class="niwa-field__hint"><?php echo esc_html__( 'Optional. No selection lets Scheduler assign any qualified available member.', 'niwa-ai' ); ?></span>
									<div class="niwa-lf-appointment-options" data-appointment-team></div>
								</fieldset>
							</div>
							<p class="niwa-lf-appointment-empty" data-appointment-empty hidden><?php echo esc_html__( 'Add services and team members in the Scheduler tab first.', 'niwa-ai' ); ?></p>
						</div>

						<!-- Validation -->
						<div class="niwa-lf-validation">
							<label class="niwa-field__label"><?php echo esc_html__( 'Validation', 'niwa-ai' ); ?></label>
							<div class="niwa-lf-grid-3">
								<div class="niwa-field">
									<label class="niwa-field__sublabel"><?php echo esc_html__( 'Min (length / value)', 'niwa-ai' ); ?></label>
									<input type="number" data-v-min />
								</div>
								<div class="niwa-field">
									<label class="niwa-field__sublabel"><?php echo esc_html__( 'Max (length / value)', 'niwa-ai' ); ?></label>
									<input type="number" data-v-max />
								</div>
								<div class="niwa-field">
									<label class="niwa-field__sublabel"><?php echo esc_html__( 'Regex', 'niwa-ai' ); ?></label>
									<input type="text" data-v-regex placeholder="^[A-Z].*" />
								</div>
							</div>
						</div>

						<!-- Branching (conditional logic + AI) -->
						<div class="niwa-lf-branch">
							<div class="niwa-lf-branch__header">
								<span class="niwa-lf-branch__title"><?php echo esc_html__( 'What happens after this question?', 'niwa-ai' ); ?></span>
								<span class="niwa-lf-branch__subtitle"><?php echo esc_html__( 'Choose how Niwa decides which question to show next.', 'niwa-ai' ); ?></span>
							</div>

							<div class="niwa-lf-branch__modes" role="radiogroup" aria-label="<?php echo esc_attr__( 'Branching mode', 'niwa-ai' ); ?>">
								<label class="niwa-lf-branch-card" data-mode-card="none">
									<input type="radio" data-branch-mode value="none" />
									<span class="niwa-lf-branch-card__body">
										<span class="niwa-lf-branch-card__title"><?php echo esc_html__( 'Next in order', 'niwa-ai' ); ?></span>
										<span class="niwa-lf-branch-card__desc"><?php echo esc_html__( 'Always go to the following question. No branching logic.', 'niwa-ai' ); ?></span>
									</span>
								</label>

								<label class="niwa-lf-branch-card" data-mode-card="rules">
									<input type="radio" data-branch-mode value="rules" />
									<span class="niwa-lf-branch-card__body">
										<span class="niwa-lf-branch-card__title"><?php echo esc_html__( 'Rules (if / then)', 'niwa-ai' ); ?></span>
										<span class="niwa-lf-branch-card__desc"><?php echo esc_html__( 'You define exact "if answer is X, go to Y" conditions.', 'niwa-ai' ); ?></span>
									</span>
								</label>

								<label class="niwa-lf-branch-card" data-mode-card="ai">
									<input type="radio" data-branch-mode value="ai" />
									<span class="niwa-lf-branch-card__body">
										<span class="niwa-lf-branch-card__title"><?php echo esc_html__( 'AI decides', 'niwa-ai' ); ?></span>
										<span class="niwa-lf-branch-card__desc"><?php echo esc_html__( 'The AI reads the answer and picks the next question from a list you choose.', 'niwa-ai' ); ?></span>
									</span>
								</label>

								<label class="niwa-lf-branch-card" data-mode-card="rules_ai">
									<input type="radio" data-branch-mode value="rules_ai" />
									<span class="niwa-lf-branch-card__body">
										<span class="niwa-lf-branch-card__title"><?php echo esc_html__( 'Rules first, then AI', 'niwa-ai' ); ?></span>
										<span class="niwa-lf-branch-card__desc"><?php echo esc_html__( 'Rules run first. If none match, the AI picks from your list.', 'niwa-ai' ); ?></span>
									</span>
								</label>
							</div>

							<!-- Rules body -->
							<div class="niwa-lf-branch__section" data-branch-section="rules" hidden>
								<div class="niwa-lf-branch__section-head">
									<h4 class="niwa-lf-branch__section-title"><?php echo esc_html__( 'Rules', 'niwa-ai' ); ?></h4>
									<span class="niwa-lf-branch__section-desc"><?php echo esc_html__( 'Rules are checked top to bottom. The first match wins.', 'niwa-ai' ); ?></span>
								</div>
								<div class="niwa-lf-rules" data-rules></div>
								<button type="button" class="button niwa-btn niwa-btn--secondary" data-add-rule>+ <?php echo esc_html__( 'Add rule', 'niwa-ai' ); ?></button>
							</div>

							<!-- AI body -->
							<div class="niwa-lf-branch__section" data-branch-section="ai" hidden>
								<div class="niwa-lf-branch__section-head">
									<h4 class="niwa-lf-branch__section-title"><?php echo esc_html__( 'AI routing', 'niwa-ai' ); ?></h4>
									<span class="niwa-lf-branch__section-desc"><?php echo esc_html__( 'The AI reads the visitor\'s answer and picks the next question — but only from the ones you tick below.', 'niwa-ai' ); ?></span>
								</div>

								<div class="niwa-lf-field">
									<label class="niwa-lf-field__label" for=""><?php echo esc_html__( 'Guidance for the AI', 'niwa-ai' ); ?></label>
									<span class="niwa-lf-field__hint"><?php echo esc_html__( 'Plain language in any language. Refer to questions by id or by what they ask — the AI is shown both.', 'niwa-ai' ); ?></span>
									<textarea data-llm-instruction rows="3" class="niwa-input--wide" placeholder="<?php echo esc_attr__( 'Example: If the answer is Yes, go to the email question. If No, go to the feedback question.', 'niwa-ai' ); ?>"></textarea>
								</div>

								<div class="niwa-lf-field">
									<div class="niwa-lf-candidates__head">
										<label class="niwa-lf-field__label"><?php echo esc_html__( 'Which questions may the AI route to?', 'niwa-ai' ); ?></label>
										<div class="niwa-lf-candidates__actions">
											<button type="button" class="button-link niwa-lf-linkbtn" data-candidates-select-all><?php echo esc_html__( 'Select all', 'niwa-ai' ); ?></button>
											<span aria-hidden="true">·</span>
											<button type="button" class="button-link niwa-lf-linkbtn" data-candidates-clear><?php echo esc_html__( 'Clear', 'niwa-ai' ); ?></button>
										</div>
									</div>
									<span class="niwa-lf-field__hint"><?php echo esc_html__( 'Tick every question the AI is allowed to jump to. If none are ticked the AI is skipped entirely.', 'niwa-ai' ); ?></span>
									<div class="niwa-lf-candidates" data-llm-candidates role="group" aria-label="<?php echo esc_attr__( 'Candidate questions for AI routing', 'niwa-ai' ); ?>"></div>
									<div class="niwa-lf-candidates__empty" data-candidates-empty hidden><?php echo esc_html__( 'Add more questions to this form to use AI routing.', 'niwa-ai' ); ?></div>
								</div>
							</div>
						</div>
					</div>
				</details>
			</div>
		</template>
		<?php
	}

	/**
	 * Option row template (for choice_* questions).
	 *
	 * @return void
	 */
	private static function render_option_template() {
		?>
		<template id="niwa-lf-option-tpl">
			<div class="niwa-lf-option-row">
				<input type="text" data-opt-value placeholder="<?php echo esc_attr__( 'value', 'niwa-ai' ); ?>" />
				<input type="text" data-opt-label placeholder="<?php echo esc_attr__( 'label', 'niwa-ai' ); ?>" />
				<button type="button" class="button-link niwa-lf-delete" data-delete-option title="<?php echo esc_attr__( 'Delete option', 'niwa-ai' ); ?>">&#x2715;</button>
			</div>
		</template>
		<?php
	}

	/**
	 * Rule row template.
	 *
	 * @return void
	 */
	private static function render_rule_template() {
		$match_types = array(
			'equals'   => __( 'is exactly', 'niwa-ai' ),
			'contains' => __( 'contains the text', 'niwa-ai' ),
			'any_of'   => __( 'is one of', 'niwa-ai' ),
			'regex'    => __( 'matches pattern (advanced)', 'niwa-ai' ),
		);
		?>
		<template id="niwa-lf-rule-tpl">
			<div class="niwa-lf-rule">
				<div class="niwa-lf-rule__row">
					<span class="niwa-lf-rule__lead"><?php echo esc_html__( 'If the answer', 'niwa-ai' ); ?></span>
					<select data-rule-match-type class="niwa-lf-rule__op">
						<?php foreach ( $match_types as $val => $lbl ) : ?>
							<option value="<?php echo esc_attr( $val ); ?>"><?php echo esc_html( $lbl ); ?></option>
						<?php endforeach; ?>
					</select>
					<input type="text" data-rule-match-value class="niwa-lf-rule__value" placeholder="<?php echo esc_attr__( 'value', 'niwa-ai' ); ?>" />
					<span class="niwa-lf-rule__arrow" aria-hidden="true">→</span>
					<span class="niwa-lf-rule__lead niwa-lf-rule__lead--arrow"><?php echo esc_html__( 'go to', 'niwa-ai' ); ?></span>
					<select data-rule-next class="niwa-lf-rule__next"></select>
					<button type="button" class="button-link niwa-lf-delete" data-delete-rule title="<?php echo esc_attr__( 'Delete rule', 'niwa-ai' ); ?>">&#x2715;</button>
				</div>
				<span class="niwa-lf-rule__hint" data-rule-help></span>
			</div>
		</template>
		<?php
	}
}
