<?php
/**
 * Shared passkey management partial for profile and frontend account-security views.
 *
 * @package GuardDog
 * @since 1.9.40
 *
 * @var WP_User $user         User whose passkeys are being managed.
 * @var array   $passkeys     Registered passkeys for the user.
 * @var string  $nonce        Passkey management nonce.
 * @var string  $context      Rendering context: profile or shortcode.
 * @var string  $style        Frontend visual style.
 * @var bool    $can_register Whether the current user can register new passkeys.
 */

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

$context      = isset( $context ) && 'profile' === $context ? 'profile' : 'shortcode';
$style        = isset( $style ) && is_string( $style ) ? sanitize_key( $style ) : 'default';
$style        = in_array( $style, array( 'default', 'minimal', 'compact' ), true ) ? $style : 'default';
$passkeys     = isset( $passkeys ) && is_array( $passkeys ) ? $passkeys : array();
$can_register = ! empty( $can_register );
$description  = __( 'Passkeys allow you to sign in without a password using biometrics (Face ID, Touch ID, Windows Hello) or a security key.', 'guard-dog' );
$instance_id  = wp_unique_id( 'guard-dog-passkeys-management-' );
$list_id      = $instance_id . '-list';
$status_id    = $instance_id . '-management-status';
$support_id   = $instance_id . '-browser-support';
$form_id      = $instance_id . '-register-form';
$name_id      = $instance_id . '-passkey-name';
$button_id    = $instance_id . '-register-passkey';
$register_id  = $instance_id . '-register-status';
$nonce_id     = $instance_id . '-nonce';

ob_start();
?>
<div
	id="<?php echo esc_attr( $list_id ); ?>"
	class="guard-dog-passkeys-management__list"
	data-guard-dog-passkeys-list
>
	<?php if ( empty( $passkeys ) ) : ?>
		<p class="guard-dog-passkeys-management__empty"><?php esc_html_e( 'No passkeys registered.', 'guard-dog' ); ?></p>
	<?php else : ?>
		<p class="description guard-dog-passkeys-management__count">
			<?php echo esc_html( sprintf( _n( 'You have %d passkey.', 'You have %d passkeys.', count( $passkeys ), 'guard-dog' ), count( $passkeys ) ) ); ?>
		</p>
		<div class="guard-dog-passkeys-management__table-wrap">
			<table class="widefat guard-dog-passkeys-management__table">
				<thead>
					<tr>
						<th scope="col"><?php esc_html_e( 'Name', 'guard-dog' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Added', 'guard-dog' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Last Used', 'guard-dog' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Actions', 'guard-dog' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $passkeys as $passkey ) : ?>
						<tr data-passkey-id="<?php echo esc_attr( $passkey->passkey_id ); ?>">
							<td>
								<span class="passkey-name"><?php echo esc_html( $passkey->friendly_name ); ?></span>
							</td>
							<td><?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $passkey->created_at ) ) ); ?></td>
							<td>
								<?php if ( $passkey->last_used_at ) : ?>
									<?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $passkey->last_used_at ) ) ); ?>
								<?php else : ?>
									<?php esc_html_e( 'Never', 'guard-dog' ); ?>
								<?php endif; ?>
							</td>
							<td>
								<div class="guard-dog-passkeys-management__actions">
									<button type="button" class="button button-small guard-dog-2fa-btn guard-dog-2fa-btn-secondary guard-dog-rename-passkey" data-passkey-id="<?php echo esc_attr( $passkey->passkey_id ); ?>">
										<?php esc_html_e( 'Rename', 'guard-dog' ); ?>
									</button>
									<button type="button" class="button button-small button-link-delete guard-dog-2fa-btn guard-dog-2fa-btn-danger guard-dog-delete-passkey" data-passkey-id="<?php echo esc_attr( $passkey->passkey_id ); ?>">
										<?php esc_html_e( 'Delete', 'guard-dog' ); ?>
									</button>
								</div>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	<?php endif; ?>
	<p
		id="<?php echo esc_attr( $status_id ); ?>"
		class="guard-dog-passkeys-management__feedback"
		role="status"
		aria-live="polite"
		hidden
		data-guard-dog-passkey-management-status
	></p>
</div>
<?php
$passkey_list_markup = ob_get_clean();

ob_start();
?>
<p
	id="<?php echo esc_attr( $support_id ); ?>"
	class="guard-dog-passkeys-management__browser-support"
	style="display: none;"
	data-guard-dog-passkey-browser-support
>
	<?php esc_html_e( 'Your browser does not support passkeys (WebAuthn).', 'guard-dog' ); ?>
</p>
<div
	id="<?php echo esc_attr( $form_id ); ?>"
	class="guard-dog-passkeys-management__register-form"
	data-guard-dog-passkey-register-form
>
	<div class="guard-dog-passkeys-management__form-controls">
		<input
			type="text"
			id="<?php echo esc_attr( $name_id ); ?>"
			class="regular-text guard-dog-passkeys-management__name-input"
			placeholder="<?php esc_attr_e( 'Passkey name (e.g., MacBook Touch ID)', 'guard-dog' ); ?>"
			data-guard-dog-passkey-name
		>
		<button
			type="button"
			id="<?php echo esc_attr( $button_id ); ?>"
			class="button button-primary guard-dog-2fa-btn guard-dog-2fa-btn-primary"
			data-guard-dog-register-passkey
		>
			<?php esc_html_e( 'Register Passkey', 'guard-dog' ); ?>
		</button>
	</div>
	<p
		id="<?php echo esc_attr( $register_id ); ?>"
		class="guard-dog-passkeys-management__feedback guard-dog-passkeys-management__register-status"
		role="status"
		aria-live="polite"
		hidden
		data-guard-dog-passkey-register-status
	></p>
</div>
<?php
$registration_markup = ob_get_clean();
?>

<?php if ( 'profile' === $context ) : ?>
	<div
		id="<?php echo esc_attr( $instance_id ); ?>"
		class="guard-dog-passkeys-management"
		data-guard-dog-passkeys-management="1"
		data-context="profile"
		data-style="<?php echo esc_attr( $style ); ?>"
	>
		<h2><?php esc_html_e( 'Passkeys', 'guard-dog' ); ?></h2>
		<p class="description">
			<?php echo esc_html( $description ); ?>
		</p>

		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><?php esc_html_e( 'Your Passkeys', 'guard-dog' ); ?></th>
				<td>
					<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted plugin-generated markup. ?>
					<?php echo $passkey_list_markup; ?>
				</td>
			</tr>
			<?php if ( $can_register ) : ?>
				<tr>
					<th scope="row"><?php esc_html_e( 'Add New Passkey', 'guard-dog' ); ?></th>
					<td>
						<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted plugin-generated markup. ?>
						<?php echo $registration_markup; ?>
					</td>
				</tr>
			<?php endif; ?>
		</table>

		<input type="hidden" id="<?php echo esc_attr( $nonce_id ); ?>" value="<?php echo esc_attr( $nonce ); ?>" data-guard-dog-passkey-nonce>
	</div>
<?php else : ?>
	<div
		id="<?php echo esc_attr( $instance_id ); ?>"
		class="guard-dog-2fa-passkeys-section guard-dog-passkeys-management guard-dog-passkeys-management--<?php echo esc_attr( $style ); ?>"
		data-guard-dog-passkeys-management="1"
		data-context="shortcode"
		data-style="<?php echo esc_attr( $style ); ?>"
	>
		<h4><?php esc_html_e( 'Passkeys', 'guard-dog' ); ?></h4>
		<p class="guard-dog-2fa-description guard-dog-passkeys-management__description">
			<?php echo esc_html( $description ); ?>
		</p>

		<div class="guard-dog-passkeys-management__panel">
			<h5><?php esc_html_e( 'Your Passkeys', 'guard-dog' ); ?></h5>
			<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted plugin-generated markup. ?>
			<?php echo $passkey_list_markup; ?>
		</div>

		<?php if ( $can_register ) : ?>
			<div class="guard-dog-passkeys-management__panel">
				<h5><?php esc_html_e( 'Add New Passkey', 'guard-dog' ); ?></h5>
				<p class="guard-dog-2fa-description">
					<?php esc_html_e( 'Register a new device or security key for passwordless sign-in.', 'guard-dog' ); ?>
				</p>
				<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted plugin-generated markup. ?>
				<?php echo $registration_markup; ?>
			</div>
		<?php endif; ?>

		<input type="hidden" id="<?php echo esc_attr( $nonce_id ); ?>" value="<?php echo esc_attr( $nonce ); ?>" data-guard-dog-passkey-nonce>
	</div>
<?php endif; ?>
