<?php
/**
 * Create Fundraiser Form — data entry step.
 *
 * Labels and help text adapt to the three creation modes:
 *  - individual: personal fundraising page (name, motivation, optional goal)
 *  - team:       team fundraiser (team name, team description, team goal)
 *  - fundraiser: sub-cause (fundraiser name, description, fundraiser goal)
 *
 * @var array{
 *     root_id: int,
 *     parent_id: int,
 *     nesting_enabled: bool,
 *     skip_root_step: bool,
 *     forced_type: string,
 *     has_type_step: bool,
 *     edit_data: array,
 *     fundraiser_end_date_enabled: bool,
 *     root_end_date_gmt: string,
 * } $context
 *
 * @package AmbassadorHub\Fundraisers\Blocks\CreateFundraiserForm
 */

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

use AmbassadorHub\Admin\DateTimeHelper;
use AmbassadorHub\Fundraisers\FundraisersHelpers;
use AmbassadorHub\Fundraisers\Admin\Settings\SettingsManager as FundraiserSettingsManager;

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

// Labels per type — used for PHP-rendered labels when type is known server-side.
// When type is empty (chosen via type-selection step), JS swaps these via data attributes.
switch ( $ambh_forced_type ) {
	case 'individual':
		$ambh_heading         = $ambh_edit_mode ? __( 'Edit your personal fundraiser', 'ambassadorhub' ) : __( 'Your personal fundraiser', 'ambassadorhub' );
		$ambh_title_label     = __( 'Your name', 'ambassadorhub' );
		$ambh_title_hint      = __( 'This is how you will appear to donors.', 'ambassadorhub' );
		$ambh_content_label   = __( 'Your personal motivation', 'ambassadorhub' );
		$ambh_content_hint    = __( 'Share your personal story and why you are fundraising.', 'ambassadorhub' );
		$ambh_image_label     = __( 'Your photo', 'ambassadorhub' );
		$ambh_target_label    = __( 'Your personal goal', 'ambassadorhub' );
		$ambh_target_optional = true;
		$ambh_submit_label    = $ambh_edit_mode ? __( 'Save changes', 'ambassadorhub' ) : __( 'Start fundraising', 'ambassadorhub' );
		break;

	case 'team':
		$ambh_heading         = $ambh_edit_mode ? __( 'Edit your team', 'ambassadorhub' ) : __( 'Create a team', 'ambassadorhub' );
		$ambh_title_label     = __( 'Team name', 'ambassadorhub' );
		$ambh_title_hint      = '';
		$ambh_content_label   = __( 'What is your team about?', 'ambassadorhub' );
		$ambh_content_hint    = __( 'Tell others what motivates your team.', 'ambassadorhub' );
		$ambh_image_label     = __( 'Team photo', 'ambassadorhub' );
		$ambh_target_label    = __( 'Team goal', 'ambassadorhub' );
		$ambh_target_optional = false;
		$ambh_submit_label    = $ambh_edit_mode ? __( 'Save changes', 'ambassadorhub' ) : __( 'Create a team', 'ambassadorhub' );
		break;

	case 'fundraiser':
	default:
		$ambh_heading         = $ambh_edit_mode ? __( 'Edit your fundraiser', 'ambassadorhub' ) : __( 'Start new Fundraiser', 'ambassadorhub' );
		$ambh_title_label     = __( 'Fundraiser name', 'ambassadorhub' );
		$ambh_title_hint      = '';
		$ambh_content_label   = __( 'Describe this fundraiser', 'ambassadorhub' );
		$ambh_content_hint    = __( 'What is the cause and who will it help?', 'ambassadorhub' );
		$ambh_image_label     = __( 'Fundraiser image', 'ambassadorhub' );
		$ambh_target_label    = __( 'Fundraiser goal', 'ambassadorhub' );
		$ambh_target_optional = false;
		$ambh_submit_label    = $ambh_edit_mode ? __( 'Save changes', 'ambassadorhub' ) : __( 'Start new Fundraiser', 'ambassadorhub' );
		break;
}

// "Allow sub-teams" checkbox — only relevant when creating a team.
$ambh_render_allow_nested  = ! empty( $context['nesting_enabled'] ) && ( ! empty( $context['has_type_step'] ) || 'team' === $ambh_forced_type );
$ambh_allow_nested_visible = 'team' === $ambh_forced_type;

// "Allow teams" checkbox — only relevant when creating a new root fundraiser (no existing root context).
$ambh_render_teams_enabled  = ( 0 === (int) $context['root_id'] ) && ! empty( $context['can_create_root'] );
$ambh_teams_enabled_visible = 'fundraiser' === $ambh_forced_type;

// Show back button when there is a preceding step.
$ambh_show_back = ! $ambh_edit_mode && ( ! $context['skip_root_step'] || $context['has_type_step'] );

// Minimum target amount from global option.
$ambh_min_target = max( 0, floatval( get_option( 'ambh_fundraiser_min_target', 0 ) ) );

// Encode label sets as JSON for JS to swap when type is chosen in the type-selection step.
// Each set mirrors the PHP labels above.
$ambh_labels_js = array(
	'individual' => array(
		'heading'        => __( 'Your personal fundraiser', 'ambassadorhub' ),
		'title'          => __( 'Your name', 'ambassadorhub' ),
		'titleHint'      => __( 'This is how you will appear to donors.', 'ambassadorhub' ),
		'content'        => __( 'Your personal motivation', 'ambassadorhub' ),
		'contentHint'    => __( 'Share your personal story and why you are fundraising.', 'ambassadorhub' ),
		'image'          => __( 'Your photo', 'ambassadorhub' ),
		'target'         => __( 'Your personal goal', 'ambassadorhub' ),
		'targetOptional' => true,
		'submit'         => __( 'Start fundraising', 'ambassadorhub' ),
		'allowNested'    => false,
	),
	'team'       => array(
		'heading'        => __( 'Create a team', 'ambassadorhub' ),
		'title'          => __( 'Team name', 'ambassadorhub' ),
		'titleHint'      => '',
		'content'        => __( 'What is your team about?', 'ambassadorhub' ),
		'contentHint'    => __( 'Tell others what motivates your team.', 'ambassadorhub' ),
		'image'          => __( 'Team photo', 'ambassadorhub' ),
		'target'         => __( 'Team goal', 'ambassadorhub' ),
		'targetOptional' => false,
		'submit'         => __( 'Create a team', 'ambassadorhub' ),
		'allowNested'    => $ambh_render_allow_nested,
	),
	'fundraiser' => array(
		'heading'        => __( 'Start new Fundraiser', 'ambassadorhub' ),
		'title'          => __( 'Fundraiser name', 'ambassadorhub' ),
		'titleHint'      => '',
		'content'        => __( 'Describe this fundraiser', 'ambassadorhub' ),
		'contentHint'    => __( 'What is the cause and who will it help?', 'ambassadorhub' ),
		'image'          => __( 'Fundraiser image', 'ambassadorhub' ),
		'target'         => __( 'Fundraiser goal', 'ambassadorhub' ),
		'targetOptional' => false,
		'submit'         => __( 'Start new Fundraiser', 'ambassadorhub' ),
		'allowNested'    => false,
		'teamsEnabled'   => $ambh_render_teams_enabled,
	),
);
?>

<div class="ambh-cff-step-inner"
	data-labels="<?php echo esc_attr( wp_json_encode( $ambh_labels_js ) ); ?>"
	data-forced-type="<?php echo esc_attr( $ambh_forced_type ); ?>">

	<h3 class="ambh-cff-step-title" data-field="heading">
		<?php echo esc_html( $ambh_heading ); ?>
	</h3>

	<form class="ambh-cff-form" novalidate>

		<input type="hidden" name="is_group" value="<?php echo 'team' === $ambh_forced_type ? '1' : '0'; ?>">

		<div class="ambh-cff-field">
			<label for="ambh-cff-title" data-field="title-label">
				<?php echo esc_html( $ambh_title_label ); ?> <span aria-hidden="true">*</span>
			</label>
			<?php if ( $ambh_title_hint ) : ?>
				<p class="ambh-cff-field__hint" data-field="title-hint"><?php echo esc_html( $ambh_title_hint ); ?></p>
			<?php else : ?>
				<p class="ambh-cff-field__hint" data-field="title-hint" hidden></p>
			<?php endif; ?>
			<input
				type="text"
				id="ambh-cff-title"
				name="title"
				required
				maxlength="200"
				autocomplete="off"
				value="<?php echo esc_attr( $ambh_edit_data['title'] ?? '' ); ?>">
			<span class="ambh-cff-field__error" aria-live="polite"></span>
		</div>

		<div class="ambh-cff-field">
			<label for="ambh-cff-content" data-field="content-label">
				<?php echo esc_html( $ambh_content_label ); ?> <span aria-hidden="true">*</span>
			</label>
			<?php if ( $ambh_content_hint ) : ?>
				<p class="ambh-cff-field__hint" data-field="content-hint"><?php echo esc_html( $ambh_content_hint ); ?></p>
			<?php else : ?>
				<p class="ambh-cff-field__hint" data-field="content-hint" hidden></p>
			<?php endif; ?>
			<textarea
				id="ambh-cff-content"
				name="content"
				required
				rows="5"><?php echo isset( $ambh_edit_data['content'] ) ? esc_textarea( $ambh_edit_data['content'] ) : ''; ?></textarea>
			<span class="ambh-cff-field__error" aria-live="polite"></span>
		</div>

		<div class="ambh-cff-field">
			<label data-field="image-label"><?php echo esc_html( $ambh_image_label ); ?></label>

			<?php if ( $ambh_edit_mode && ! empty( $ambh_edit_data['thumbnail_url'] ) ) : ?>
			<div class="ambh-cff-current-image">
				<img src="<?php echo esc_url( $ambh_edit_data['thumbnail_url'] ); ?>" alt="" class="ambh-cff-current-image__img">
				<button type="button" class="ambh-cff-current-image__change">
					<?php esc_html_e( 'Change image', 'ambassadorhub' ); ?>
				</button>
			</div>
			<?php endif; ?>

			<div class="ambh-cff-image-drop" tabindex="0" role="button"
				aria-label="<?php esc_attr_e( 'Click or drag an image here to upload', 'ambassadorhub' ); ?>"
				<?php echo ( $ambh_edit_mode && ! empty( $ambh_edit_data['thumbnail_url'] ) ) ? ' hidden' : ''; ?>>
				<span class="ambh-cff-image-drop-prompt"><?php esc_html_e( 'Click or drag an image here', 'ambassadorhub' ); ?></span>
				<input type="file" class="ambh-cff-image-input" accept="image/*" tabindex="-1" aria-hidden="true">
			</div>

			<div class="ambh-cff-image-crop" hidden>
				<div class="ambh-cff-crop-viewport">
					<img class="ambh-cff-crop-img" src="" alt="" draggable="false">
				</div>
				<p class="ambh-cff-crop-hint"><?php esc_html_e( 'Drag to move · Scroll or pinch to zoom', 'ambassadorhub' ); ?></p>
				<button type="button" class="ambh-cff-image-change">
					<?php esc_html_e( 'Change image', 'ambassadorhub' ); ?>
				</button>
			</div>

			<canvas class="ambh-cff-crop-canvas" hidden></canvas>
		</div>

		<div class="ambh-cff-field<?php echo $ambh_target_optional ? ' ambh-cff-field--optional' : ''; ?>"
			data-field-target-wrap>
			<label for="ambh-cff-target" data-field="target-label">
				<?php echo esc_html( $ambh_target_label ); ?>
				<?php if ( ! $ambh_target_optional ) : ?>
					<span aria-hidden="true"> *</span>
				<?php else : ?>
					<span class="ambh-cff-field__optional"><?php esc_html_e( 'optional', 'ambassadorhub' ); ?></span>
				<?php endif; ?>
			</label>
			<input
				type="text"
				inputmode="decimal"
				id="ambh-cff-target"
				name="target_amount"
				class="ambh-currency-input"
				<?php
				if ( ! $ambh_target_optional ) :
					?>
					required<?php endif; ?>
				value="<?php echo esc_attr( FundraiserSettingsManager::format_amount_for_input( floatval( $ambh_edit_data['target_amount'] ?? 0 ) ) ); ?>">
			<span class="ambh-cff-field__error" aria-live="polite"></span>
		</div>

		<?php
		if ( ! empty( $context['fundraiser_end_date_enabled'] ) ) :
			$ambh_end_campaign_gmt = $context['root_end_date_gmt'] ?? '';
			$ambh_end_current_gmt  = (string) get_post_meta( $context['edit_data']['id'] ?? 0, \AmbassadorHub\Fundraisers\FundraisersModule::META_END_DATE, true );
			$ambh_end_current_val  = $ambh_end_current_gmt ? DateTimeHelper::format_datetime_for_input( $ambh_end_current_gmt ) : '';
			$ambh_end_max_val      = $ambh_end_campaign_gmt ? DateTimeHelper::format_datetime_for_input( $ambh_end_campaign_gmt ) : '';
			?>
		<div class="ambh-cff-field">
			<label for="ambh-cff-end-date"><?php esc_html_e( 'End date', 'ambassadorhub' ); ?></label>
			<input
				type="datetime-local"
				id="ambh-cff-end-date"
				name="end_date"
				value="<?php echo esc_attr( $ambh_end_current_val ); ?>"
				<?php
				if ( $ambh_end_max_val ) :
					?>
					max="<?php echo esc_attr( $ambh_end_max_val ); ?>"<?php endif; ?>
			>
			<span class="description"><?php esc_html_e( 'Leave empty for no end date.', 'ambassadorhub' ); ?></span>
			<span class="ambh-cff-field__error" aria-live="polite"></span>
		</div>
		<?php endif; ?>

		<?php if ( $ambh_render_allow_nested ) : ?>
		<div class="ambh-cff-field ambh-cff-field--allow-nested"<?php echo ! $ambh_allow_nested_visible ? ' style="display:none"' : ''; ?>>
			<label>
				<input type="checkbox" name="allow_nested_team" value="1">
				<?php esc_html_e( 'Allow others to create a sub-team within this team', 'ambassadorhub' ); ?>
			</label>
			<p class="description"><?php esc_html_e( 'When enabled, ambassadors can create a nested team inside this one.', 'ambassadorhub' ); ?></p>
		</div>
		<?php endif; ?>

		<?php if ( $ambh_render_teams_enabled ) : ?>
		<div class="ambh-cff-field ambh-cff-field--teams-enabled"<?php echo ! $ambh_teams_enabled_visible ? ' style="display:none"' : ''; ?>>
			<label>
				<input type="checkbox" name="teams_enabled" value="1">
				<?php esc_html_e( 'Allow others to create teams within this fundraiser', 'ambassadorhub' ); ?>
			</label>
			<p class="description"><?php esc_html_e( 'When enabled, ambassadors can start a team under this fundraiser.', 'ambassadorhub' ); ?></p>
		</div>
		<?php endif; ?>

		<?php
		$ambh_form_context = array(
			'fundraiser_id' => (int) ( $context['edit_data']['id'] ?? 0 ),
			'mode'          => $ambh_edit_mode ? 'edit' : 'create',
			'root_id'       => (int) ( $context['root_id'] ?? 0 ),
			'type'          => (string) $ambh_forced_type,
			'parent_id'     => (int) ( $context['parent_id'] ?? 0 ),
		);
		?>
		<div class="ambh-cff-form-fields" data-ambh-form-fields-container>
			<?php do_action( 'ambh_render_fundraiser_form_fields', $ambh_form_context ); ?>
		</div>

		<div class="ambh-cff-actions">

			<?php if ( $ambh_show_back ) : ?>
				<button type="button" class="ambh-cff-back ambh-button ambh-button--secondary">
					<?php esc_html_e( '← Back', 'ambassadorhub' ); ?>
				</button>
			<?php endif; ?>

			<button type="submit" class="ambh-cff-submit ambh-button ambh-button--primary" data-field="submit-label">
				<?php echo esc_html( $ambh_submit_label ); ?>
			</button>

		</div>

		<p class="ambh-cff-status" role="status" aria-live="polite"></p>

	</form>

</div>
