<?php
/**
 * Frontend active-session management partial.
 *
 * @package GuardDog
 * @since 1.9.40
 *
 * @var WP_User $user     User whose sessions are being managed.
 * @var array   $sessions Formatted active-session rows.
 * @var string  $nonce    Session management nonce.
 * @var string  $context  Rendering context.
 * @var string  $style    Display style.
 */

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

$sessions = isset( $sessions ) && is_array( $sessions ) ? $sessions : array();
$context  = isset( $context ) ? sanitize_key( $context ) : 'shortcode';
$style    = isset( $style ) ? sanitize_key( $style ) : 'default';
$nonce    = isset( $nonce ) ? (string) $nonce : '';
$instance_id = wp_unique_id( 'guard-dog-frontend-sessions-' );
$table_id    = $instance_id . '-table';
?>

<div
	id="<?php echo esc_attr( $instance_id ); ?>"
	class="guard-dog-frontend-sessions"
	data-guard-dog-sessions-surface="1"
	data-context="<?php echo esc_attr( $context ); ?>"
	data-style="<?php echo esc_attr( $style ); ?>"
>
	<h4><?php esc_html_e( 'Active Sessions', 'guard-dog' ); ?></h4>
	<p class="guard-dog-frontend-sessions__description">
		<?php esc_html_e( 'Review where your account is signed in and terminate sessions you do not recognize.', 'guard-dog' ); ?>
	</p>

	<?php if ( empty( $sessions ) ) : ?>
		<p class="guard-dog-frontend-sessions__empty"><?php esc_html_e( 'No active sessions found.', 'guard-dog' ); ?></p>
	<?php else : ?>
		<div class="guard-dog-frontend-sessions__table-wrap">
			<table
				id="<?php echo esc_attr( $table_id ); ?>"
				class="guard-dog-frontend-sessions__table"
				data-guard-dog-sessions-table
			>
				<thead>
					<tr>
						<th scope="col"><?php esc_html_e( 'Device / Browser', 'guard-dog' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Location', 'guard-dog' ); ?></th>
						<th scope="col"><?php esc_html_e( 'IP Address', 'guard-dog' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Last Activity', 'guard-dog' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Status', 'guard-dog' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Actions', 'guard-dog' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $sessions as $session ) : ?>
						<tr class="<?php echo ! empty( $session['is_compromised'] ) ? 'guard-dog-compromised-session' : ''; ?>">
							<td>
								<?php echo esc_html( $session['device'] ); ?>
								<?php if ( ! empty( $session['is_current'] ) ) : ?>
									<span class="guard-dog-current-session-badge"><?php esc_html_e( 'Current', 'guard-dog' ); ?></span>
								<?php endif; ?>
							</td>
							<td><?php echo esc_html( $session['location'] ); ?></td>
							<td><?php echo esc_html( $session['ip_address'] ); ?></td>
							<td><?php echo esc_html( $session['last_activity'] ); ?></td>
							<td>
								<?php if ( ! empty( $session['is_compromised'] ) ) : ?>
									<span class="guard-dog-status-warning" title="<?php echo esc_attr( $session['compromised_reason'] ); ?>">
										<?php esc_html_e( 'Suspicious', 'guard-dog' ); ?>
									</span>
								<?php else : ?>
									<span class="guard-dog-status-ok"><?php esc_html_e( 'Active', 'guard-dog' ); ?></span>
								<?php endif; ?>
							</td>
							<td>
								<?php if ( empty( $session['is_current'] ) ) : ?>
									<button
										type="button"
										class="guard-dog-frontend-sessions__button guard-dog-terminate-session"
										data-session-id="<?php echo esc_attr( $session['session_id'] ); ?>"
										data-nonce="<?php echo esc_attr( $nonce ); ?>"
									>
										<?php esc_html_e( 'Terminate', 'guard-dog' ); ?>
									</button>
								<?php else : ?>
									<span class="guard-dog-frontend-sessions__current-note"><?php esc_html_e( 'This session', 'guard-dog' ); ?></span>
								<?php endif; ?>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>

		<?php if ( count( $sessions ) > 1 ) : ?>
			<p class="guard-dog-frontend-sessions__bulk-actions">
				<button
					type="button"
					class="guard-dog-frontend-sessions__button guard-dog-frontend-sessions__button--secondary guard-dog-terminate-all-sessions"
					data-user-id="<?php echo esc_attr( $user->ID ); ?>"
					data-nonce="<?php echo esc_attr( $nonce ); ?>"
				>
					<?php esc_html_e( 'Terminate All Other Sessions', 'guard-dog' ); ?>
				</button>
			</p>
		<?php endif; ?>
	<?php endif; ?>
</div>
