<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<?php
$invited_email = isset( $invitation['email'] ) ? (string) $invitation['email'] : '';
$invited_name  = isset( $invitation['name'] ) ? (string) $invitation['name'] : '';
$custom_note   = isset( $invitation['custom_message'] ) ? (string) $invitation['custom_message'] : '';
$invited_products = isset( $invitation['products'] ) && is_array( $invitation['products'] ) ? array_values( $invitation['products'] ) : array();
$has_product_scope = ! empty( $invitation['has_product_scope'] );
?>
<div class="ugcify-portal-grid ugcify-portal-grid--two ugcify-portal-grid--registration">
	<article class="ugcify-portal-card">
		<div class="ugcify-portal-section__header">
			<div>
				<h3 class="ugcify-portal-section__title"><?php esc_html_e( 'Invitation details', 'ugcify' ); ?></h3>
				<p class="ugcify-portal-section__description"><?php esc_html_e( 'This secure link activates creator access for the invited email address below.', 'ugcify' ); ?></p>
			</div>
		</div>

		<dl class="ugcify-portal-definition-list">
			<div>
				<dt><?php esc_html_e( 'Invited email', 'ugcify' ); ?></dt>
				<dd><?php echo esc_html( $invited_email ); ?></dd>
			</div>
			<?php if ( '' !== $invited_name ) : ?>
				<div>
					<dt><?php esc_html_e( 'Suggested name', 'ugcify' ); ?></dt>
					<dd><?php echo esc_html( $invited_name ); ?></dd>
				</div>
			<?php endif; ?>
			<div>
				<dt><?php esc_html_e( 'Account handling', 'ugcify' ); ?></dt>
				<dd><?php echo esc_html( ! empty( $existing_user ) ? __( 'An existing WordPress account will be linked to creator access instead of creating a duplicate user.', 'ugcify' ) : __( 'A new WordPress user will be created and activated as a creator account.', 'ugcify' ) ); ?></dd>
			</div>
			<?php if ( $has_product_scope ) : ?>
				<div>
					<dt><?php esc_html_e( 'Requested products', 'ugcify' ); ?></dt>
					<dd><?php echo esc_html( ! empty( $invited_products ) ? implode( ', ', array_map( 'wp_strip_all_tags', wp_list_pluck( $invited_products, 'title' ) ) ) : __( 'This invitation requires product-specific uploads.', 'ugcify' ) ); ?></dd>
				</div>
			<?php endif; ?>
		</dl>

		<?php if ( '' !== $custom_note ) : ?>
			<div class="ugcify-review-form-message">
				<strong><?php esc_html_e( 'Brand note', 'ugcify' ); ?></strong><br>
				<?php echo esc_html( $custom_note ); ?>
			</div>
		<?php endif; ?>

		<ul class="ugcify-portal-checklist">
			<?php if ( $has_product_scope ) : ?>
				<li><?php esc_html_e( 'This invitation includes product-specific upload requests. You will tag each media item to one of the requested WooCommerce products after onboarding.', 'ugcify' ); ?></li>
			<?php endif; ?>
			<li><?php esc_html_e( 'Accept the current rights agreement to complete creator onboarding.', 'ugcify' ); ?></li>
			<li><?php esc_html_e( 'Add profile details now or refine them later from the dashboard profile tab.', 'ugcify' ); ?></li>
			<li><?php esc_html_e( 'Returning creators can use standard WordPress login after onboarding finishes.', 'ugcify' ); ?></li>
		</ul>

		<?php if ( ! empty( $invited_products ) ) : ?>
			<div class="ugcify-portal-invitation-products">
				<?php foreach ( $invited_products as $product ) : ?>
					<span class="ugcify-portal-product-pill"><?php echo esc_html( (string) ( $product['title'] ?? __( 'Product', 'ugcify' ) ) ); ?></span>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</article>

	<article class="ugcify-portal-card">
		<form method="post" enctype="multipart/form-data" class="ugcify-portal-auth-form">
			<?php wp_nonce_field( 'ugcify_influencer_portal_form', 'ugcify_influencer_nonce' ); ?>
			<input type="hidden" name="ugcify_influencer_action" value="register">
			<input type="hidden" name="token" value="<?php echo esc_attr( (string) ( $invitation['token'] ?? '' ) ); ?>">

			<div class="ugcify-form-field">
				<label for="ugcify-influencer-display-name"><?php esc_html_e( 'Display name', 'ugcify' ); ?> <span class="ugcify-required">*</span></label>
				<input id="ugcify-influencer-display-name" class="ugcify-form-input" type="text" name="display_name" value="<?php echo esc_attr( $invited_name ); ?>" required>
			</div>

			<div class="ugcify-form-field">
				<label for="ugcify-influencer-password"><?php esc_html_e( 'Password', 'ugcify' ); ?><?php if ( empty( $existing_user ) ) : ?> <span class="ugcify-required">*</span><?php endif; ?></label>
				<input id="ugcify-influencer-password" class="ugcify-form-input" type="password" name="password" <?php echo empty( $existing_user ) ? 'required' : ''; ?>>
				<div class="ugcify-form-hint"><?php echo esc_html( ! empty( $existing_user ) ? __( 'Optional when this invited email already owns a WordPress account.', 'ugcify' ) : __( 'Use at least 8 characters for the creator account password.', 'ugcify' ) ); ?></div>
			</div>

			<div class="ugcify-form-field">
				<label for="ugcify-influencer-confirm-password"><?php esc_html_e( 'Confirm password', 'ugcify' ); ?></label>
				<input id="ugcify-influencer-confirm-password" class="ugcify-form-input" type="password" name="confirm_password">
			</div>

			<div class="ugcify-form-field">
				<label for="ugcify-influencer-bio"><?php esc_html_e( 'Short bio', 'ugcify' ); ?></label>
				<textarea id="ugcify-influencer-bio" class="ugcify-form-textarea" name="bio" rows="4"></textarea>
			</div>

			<div class="ugcify-portal-grid ugcify-portal-grid--two is-compact">
				<div class="ugcify-form-field">
					<label for="ugcify-influencer-instagram"><?php esc_html_e( 'Instagram', 'ugcify' ); ?></label>
					<input id="ugcify-influencer-instagram" class="ugcify-form-input" type="text" name="instagram" placeholder="@handle">
				</div>

				<div class="ugcify-form-field">
					<label for="ugcify-influencer-tiktok"><?php esc_html_e( 'TikTok', 'ugcify' ); ?></label>
					<input id="ugcify-influencer-tiktok" class="ugcify-form-input" type="text" name="tiktok" placeholder="@handle">
				</div>
			</div>

			<div class="ugcify-form-field">
				<label for="ugcify-influencer-youtube"><?php esc_html_e( 'YouTube', 'ugcify' ); ?></label>
				<input id="ugcify-influencer-youtube" class="ugcify-form-input" type="text" name="youtube" placeholder="Channel or handle">
			</div>

			<div class="ugcify-form-field">
				<label for="ugcify-influencer-avatar"><?php esc_html_e( 'Profile photo', 'ugcify' ); ?></label>
				<input id="ugcify-influencer-avatar" class="ugcify-form-input" type="file" name="avatar_file" accept="image/*">
			</div>

			<div class="ugcify-portal-agreement">
				<div class="ugcify-portal-agreement__header">
					<strong><?php echo esc_html( sprintf( __( 'Rights agreement version %s', 'ugcify' ), (string) ( $rights['current_version'] ?? '1' ) ) ); ?></strong>
				</div>
				<div class="ugcify-portal-agreement__body"><?php echo wp_kses_post( $rights['current_text'] ?? '' ); ?></div>
			</div>

			<div class="ugcify-form-checkbox">
				<input id="ugcify-influencer-rights" type="checkbox" name="rights_agreed" value="1" required>
				<label for="ugcify-influencer-rights"><?php esc_html_e( 'I agree to the creator rights terms above.', 'ugcify' ); ?></label>
			</div>

			<div class="ugcify-form-checkbox">
				<input id="ugcify-influencer-remember" type="checkbox" name="remember" value="1" checked>
				<label for="ugcify-influencer-remember"><?php esc_html_e( 'Keep me signed in after onboarding.', 'ugcify' ); ?></label>
			</div>

			<div class="ugcify-step-buttons">
				<button type="submit" class="ugcify-portal-button is-primary"><?php esc_html_e( 'Activate Creator Access', 'ugcify' ); ?></button>
			</div>
		</form>
	</article>
</div>