<?php
/**
 * Partial: AGB (Terms) questionnaire catalog.
 *
 * Renders the serialised AGB questionnaire catalog (categories → questions) from
 * the get-agb-catalog proxy, INSIDE the Legal Wizard's Contract step. Questions
 * carry their conditional-visibility rule in a data-condition attribute; the
 * shared client engine (agb-questionnaire.js) shows/hides them based on current
 * answers + context and persists answers as a JSON blob via the save-agb-data
 * proxy (stored in legalEntity.agbData). This is the SAME markup/engine the old
 * standalone "Terms Questionnaire" page used — relocated so the single Legal
 * Wizard (Rechtstextgenerator) owns the whole flow, mirroring the Cloud app.
 *
 * @var array $agb get-agb-catalog response: { catalog:[{id,title,icon,description,condition,questions:[...]}], answers:{}, context:{businessType,targetAudience,jurisdiction} }
 * @package Biscotti_CMP
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- function-scoped locals inside an included partial, not globals.

$agb_catalog = ( isset( $agb['catalog'] ) && is_array( $agb['catalog'] ) ) ? $agb['catalog'] : array();
$agb_answers = ( isset( $agb['answers'] ) && is_array( $agb['answers'] ) ) ? $agb['answers'] : array();
$agb_context = ( isset( $agb['context'] ) && is_array( $agb['context'] ) ) ? $agb['context'] : array();

/**
 * Resolve the current value for a question (saved answer, else default).
 *
 * @param array $q       Question definition.
 * @param array $answers Saved answers.
 * @return mixed
 */
if ( ! function_exists( 'biscotti_agb_val' ) ) {
	function biscotti_agb_val( $q, $answers ) {
		$id = isset( $q['id'] ) ? $q['id'] : '';
		if ( '' !== $id && array_key_exists( $id, $answers ) ) {
			return $answers[ $id ];
		}
		return isset( $q['default'] ) ? $q['default'] : null;
	}
}
?>

<p class="description"><?php esc_html_e( 'Answer these questions to tailor your Terms & Conditions (AGB). Your answers are saved and used when generating the Terms document.', 'biscotti-cmp' ); ?></p>

<?php if ( empty( $agb_catalog ) ) : ?>
	<p class="biscotti-empty-state"><?php esc_html_e( 'The questionnaire could not be loaded.', 'biscotti-cmp' ); ?></p>
<?php else : ?>
	<div id="biscotti-agb-form">
		<?php foreach ( $agb_catalog as $cat ) :
			$cat_id   = isset( $cat['id'] ) ? $cat['id'] : '';
			$cat_cond = isset( $cat['condition'] ) ? $cat['condition'] : 'always';
			?>
			<div class="biscotti-card biscotti-agb-cat" data-condition="<?php echo esc_attr( $cat_cond ); ?>">
				<h3><?php echo esc_html( ( isset( $cat['icon'] ) ? $cat['icon'] . ' ' : '' ) . ( isset( $cat['title'] ) ? $cat['title'] : $cat_id ) ); ?></h3>
				<?php if ( ! empty( $cat['description'] ) ) : ?>
					<p class="description"><?php echo esc_html( $cat['description'] ); ?></p>
				<?php endif; ?>

				<?php
				$questions = ( isset( $cat['questions'] ) && is_array( $cat['questions'] ) ) ? $cat['questions'] : array();
				foreach ( $questions as $q ) :
					$qid   = isset( $q['id'] ) ? $q['id'] : '';
					$qtype = isset( $q['type'] ) ? $q['type'] : 'text';
					$qcond = isset( $q['condition'] ) ? $q['condition'] : 'always';
					$qval  = biscotti_agb_val( $q, $agb_answers );
					if ( '' === $qid ) {
						continue;
					}
					?>
					<div class="biscotti-agb-q" data-question-id="<?php echo esc_attr( $qid ); ?>" data-type="<?php echo esc_attr( $qtype ); ?>" data-condition="<?php echo esc_attr( $qcond ); ?>">
						<label class="biscotti-agb-label"><?php echo esc_html( isset( $q['label'] ) ? $q['label'] : $qid ); ?></label>
						<?php if ( ! empty( $q['helpText'] ) ) : ?>
							<p class="description"><?php echo esc_html( $q['helpText'] ); ?></p>
						<?php endif; ?>
						<?php
						switch ( $qtype ) {
							case 'toggle':
								?>
								<label class="lw-cb"><input type="checkbox" class="biscotti-agb-input" data-qid="<?php echo esc_attr( $qid ); ?>" <?php checked( ! empty( $qval ) ); ?> /> <?php esc_html_e( 'Yes', 'biscotti-cmp' ); ?></label>
								<?php
								break;
							case 'number':
								?>
								<input type="number" class="biscotti-svc-input biscotti-agb-input" data-qid="<?php echo esc_attr( $qid ); ?>" value="<?php echo esc_attr( null !== $qval ? $qval : '' ); ?>"
									<?php echo isset( $q['min'] ) ? 'min="' . esc_attr( $q['min'] ) . '"' : ''; ?>
									<?php echo isset( $q['max'] ) ? 'max="' . esc_attr( $q['max'] ) . '"' : ''; ?> style="max-width:160px;" />
								<?php
								break;
							case 'multiselect':
								$selected = is_array( $qval ) ? $qval : array();
								$opts     = ( isset( $q['options'] ) && is_array( $q['options'] ) ) ? $q['options'] : array();
								echo '<div class="biscotti-lw-toggle-grid">';
								foreach ( $opts as $opt ) {
									$ov = isset( $opt['id'] ) ? $opt['id'] : ( isset( $opt['value'] ) ? $opt['value'] : '' );
									$ol = isset( $opt['label'] ) ? $opt['label'] : $ov;
									printf(
										'<label class="lw-toggle"><input type="checkbox" class="biscotti-agb-multi" data-qid="%s" value="%s" %s /> <span>%s</span></label>',
										esc_attr( $qid ),
										esc_attr( $ov ),
										in_array( $ov, $selected, true ) ? 'checked' : '',
										esc_html( $ol )
									);
								}
								echo '</div>';
								break;
							case 'select':
							default:
								$opts = ( isset( $q['options'] ) && is_array( $q['options'] ) ) ? $q['options'] : array();
								if ( ! empty( $opts ) ) {
									echo '<select class="biscotti-svc-input biscotti-agb-input" data-qid="' . esc_attr( $qid ) . '">';
									foreach ( $opts as $opt ) {
										$ov = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['id'] ) ? $opt['id'] : '' );
										$ol = isset( $opt['label'] ) ? $opt['label'] : $ov;
										printf(
											'<option value="%s" %s>%s</option>',
											esc_attr( $ov ),
											( (string) $qval === (string) $ov ) ? 'selected' : '',
											esc_html( $ol )
										);
									}
									echo '</select>';
								} else {
									echo '<input type="text" class="biscotti-svc-input biscotti-agb-input" data-qid="' . esc_attr( $qid ) . '" value="' . esc_attr( null !== $qval ? $qval : '' ) . '" />';
								}
								break;
						}
						?>
					</div>
				<?php endforeach; ?>
			</div>
		<?php endforeach; ?>
	</div>

	<script type="application/json" id="biscotti-agb-context"><?php echo wp_json_encode( $agb_context ); ?></script>
<?php endif; ?>
