<?php
/**
 * Create Fundraiser Form — outer wrapper template.
 *
 * @var array $context  Resolved context from ContextResolver::resolve().
 *
 * @package AmbassadorHub\Fundraisers\Blocks\CreateFundraiserForm
 */

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

$ambh_edit_data   = $context['edit_data'] ?? array();
$ambh_edit_mode   = ! empty( $ambh_edit_data );
$ambh_forced_type = $context['forced_type'] ?? '';

// Pre-fill user display name for personal forms (individual join / personal-join step).
$ambh_current_user      = wp_get_current_user();
$ambh_user_display_name = $ambh_current_user->display_name ?? '';

?>
<div <?php echo $context['wrapper_attrs']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
	data-root-id="<?php echo esc_attr( $context['root_id'] ); ?>"
	data-parent-id="<?php echo esc_attr( $context['parent_id'] ); ?>"
	data-nesting-enabled="<?php echo esc_attr( $context['nesting_enabled'] ? '1' : '0' ); ?>"
	data-forced-type="<?php echo esc_attr( $ambh_forced_type ); ?>"
	data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>"
	data-user-display-name="<?php echo esc_attr( $ambh_user_display_name ); ?>"
	data-i18n-title-required="<?php echo esc_attr( __( 'Please enter a title.', 'ambassadorhub' ) ); ?>"
	data-i18n-content-required="<?php echo esc_attr( __( 'Please enter a description.', 'ambassadorhub' ) ); ?>"
	data-i18n-image-required="<?php echo esc_attr( __( 'Please select an image.', 'ambassadorhub' ) ); ?>"
	data-i18n-saving="<?php echo esc_attr( __( 'Saving…', 'ambassadorhub' ) ); ?>"
	data-i18n-join-team="<?php echo esc_attr( __( 'Join the team', 'ambassadorhub' ) ); ?>"
	data-i18n-join-fundraiser="<?php echo esc_attr( __( 'Join this fundraiser', 'ambassadorhub' ) ); ?>"
	data-i18n-skip-team="<?php echo esc_attr( __( 'Skip — go to team page', 'ambassadorhub' ) ); ?>"
	data-i18n-skip-fundraiser="<?php echo esc_attr( __( 'Skip — go to fundraiser page', 'ambassadorhub' ) ); ?>"
	<?php if ( $ambh_edit_mode ) : ?>
	data-edit-mode="1"
	data-edit-fundraiser-id="<?php echo esc_attr( $ambh_edit_data['id'] ); ?>"
	data-edit-title="<?php echo esc_attr( $ambh_edit_data['title'] ); ?>"
	data-edit-content="<?php echo esc_attr( $ambh_edit_data['content'] ); ?>"
	data-edit-target="<?php echo esc_attr( $ambh_edit_data['target_amount'] ); ?>"
	data-edit-thumbnail-url="<?php echo esc_attr( $ambh_edit_data['thumbnail_url'] ); ?>"
	data-edit-permalink="<?php echo esc_attr( $ambh_edit_data['permalink'] ); ?>"
	<?php endif; ?>>

	<?php if ( ! $ambh_edit_mode ) : ?>

		<?php /* Context info banner — shows which fundraiser / team the user is joining. */ ?>
		<div class="ambh-cff-context-info<?php echo $context['skip_root_step'] ? ' is-visible' : ''; ?>"
			aria-live="polite"
			data-context-info>
			<span class="ambh-cff-context-info__label"><?php esc_html_e( 'Joining:', 'ambassadorhub' ); ?></span>
			<span class="ambh-cff-context-info__breadcrumb">
				<span data-context-root-text><?php echo esc_html( $context['root_title'] ); ?></span>
				<span class="ambh-cff-context-info__parent" data-context-parent
				<?php
				if ( ! $context['parent_id'] ) :
					?>
					style="display:none"<?php endif; ?>>
					<span class="ambh-cff-context-info__sep" aria-hidden="true"> › </span>
					<span data-context-parent-text><?php echo esc_html( $context['parent_title'] ); ?></span>
				</span>
			</span>
		</div>

		<?php if ( ! $context['skip_root_step'] ) : ?>
			<div class="ambh-cff-step ambh-cff-step--select-root is-active" data-step="select-root">
				<?php require __DIR__ . '/step-select-root.php'; ?>
			</div>
		<?php endif; ?>

		<?php if ( '' !== $context['type_conflict'] ) : ?>
			<div class="ambh-cff-conflict-notice" role="alert">
				<?php if ( 'root_no_teams' === $context['type_conflict'] ) : ?>
					<p><?php esc_html_e( 'Team fundraisers are not available for this fundraiser.', 'ambassadorhub' ); ?></p>
				<?php elseif ( 'parent_no_nested' === $context['type_conflict'] ) : ?>
					<p><?php esc_html_e( 'This team does not allow sub-teams.', 'ambassadorhub' ); ?></p>
				<?php endif; ?>
				<p><?php esc_html_e( 'You can still join as an individual.', 'ambassadorhub' ); ?></p>
			</div>
		<?php endif; ?>

		<?php if ( $context['has_type_step'] ) : ?>
			<div class="ambh-cff-step ambh-cff-step--select-type<?php echo $context['skip_root_step'] ? ' is-active' : ''; ?>" data-step="select-type">
				<?php require __DIR__ . '/step-select-type.php'; ?>
			</div>
		<?php endif; ?>

		<?php
		// Data step is immediately active when both root and type are already known.
		$ambh_data_step_active = $context['skip_root_step'] && ! $context['has_type_step'];
		?>
		<div class="ambh-cff-step ambh-cff-step--fundraiser-data<?php echo $ambh_data_step_active ? ' is-active' : ''; ?>" data-step="fundraiser-data">
			<?php require __DIR__ . '/step-fundraiser-data.php'; ?>
		</div>

		<?php /* Post-fundraiser step: shown after a sub-fundraiser is created. Always rendered; JS activates it. */ ?>
		<div class="ambh-cff-step ambh-cff-step--post-fundraiser" data-step="post-fundraiser">
			<?php require __DIR__ . '/step-post-fundraiser.php'; ?>
		</div>

		<?php /* Personal-join step: shown after team creation (or join-fundraiser). Always rendered; JS activates it. */ ?>
		<div class="ambh-cff-step ambh-cff-step--personal-join" data-step="personal-join">
			<?php require __DIR__ . '/step-personal-join.php'; ?>
		</div>

	<?php else : ?>

		<?php /* Edit mode: open data step directly. */ ?>
		<div class="ambh-cff-step ambh-cff-step--fundraiser-data is-active" data-step="fundraiser-data">
			<?php require __DIR__ . '/step-fundraiser-data.php'; ?>
		</div>

	<?php endif; ?>

</div>
