<?php
/**
 * User profile active sessions box.
 *
 * @package LoginSessionManager
 */

namespace LoginSessionManager\Includes;

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

class Session_Profile {

	private Session_Repository $repository;

	/** @var array<string, mixed> */
	private array $settings;

	private Session_Admin $admin;

	public function __construct( Session_Repository $repository, array $settings, Session_Admin $admin ) {
		$this->repository = $repository;
		$this->settings   = $settings;
		$this->admin      = $admin;
	}

	public function register_hooks(): void {
		if ( ! Session_Capabilities::is_manager_active( $this->settings ) || empty( $this->settings['show_on_profile'] ) ) {
			return;
		}

		add_action( 'show_user_profile', [ $this, 'render_profile_box' ] );
		add_action( 'edit_user_profile', [ $this, 'render_profile_box' ] );
		add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_wc_assets' ] );

		if ( class_exists( 'WooCommerce' ) ) {
			add_action( 'woocommerce_account_dashboard', [ $this, 'render_wc_sessions' ], 20 );
		}
	}

	public function maybe_enqueue_wc_assets(): void {
		if ( ! function_exists( 'is_account_page' ) || ! is_account_page() ) {
			return;
		}

		$this->admin->enqueue_shared_assets();
	}

	public function render_wc_sessions(): void {
		$user = wp_get_current_user();
		if ( ! $user instanceof \WP_User || $user->ID < 1 ) {
			return;
		}

		echo '<div class="wpps-lsm-wc-account-sessions">';
		$this->render_profile_box( $user );
		echo '</div>';
	}

	public function render_profile_box( \WP_User $user ): void {
		$actor_id = get_current_user_id();

		if ( ! Session_Capabilities::can_view_user_sessions( $actor_id, (int) $user->ID ) ) {
			return;
		}

		$this->repository->set_privacy_options(
			[
				'hide_ip'         => ! empty( $this->settings['hide_ip'] ),
				'hide_user_agent' => ! empty( $this->settings['hide_user_agent'] ),
			]
		);

		$sessions   = $this->repository->get_sessions_for_user( (int) $user->ID );
		$can_revoke = Session_Capabilities::can_revoke_user_sessions( $actor_id, (int) $user->ID );
		$show_ip    = empty( $this->settings['hide_ip'] );
		?>
		<h2 id="wpps-lsm-profile-sessions"><?php esc_html_e( 'Active Sessions', 'wp-powersuite' ); ?></h2>
		<p class="description">
			<?php esc_html_e( 'Devices and browsers currently signed in to this account. Revoke any session you do not recognize.', 'wp-powersuite' ); ?>
		</p>

		<div class="wpps-lsm-profile-wrap" data-user-id="<?php echo esc_attr( (string) $user->ID ); ?>">
			<?php if ( $sessions === [] ) : ?>
				<p><?php esc_html_e( 'No active sessions.', 'wp-powersuite' ); ?></p>
			<?php else : ?>
				<table class="widefat striped wpps-lsm-profile-table">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Device', 'wp-powersuite' ); ?></th>
							<?php if ( $show_ip ) : ?>
								<th><?php esc_html_e( 'IP', 'wp-powersuite' ); ?></th>
							<?php endif; ?>
							<th><?php esc_html_e( 'Login', 'wp-powersuite' ); ?></th>
							<th><?php esc_html_e( 'Expires', 'wp-powersuite' ); ?></th>
							<?php if ( $can_revoke ) : ?>
								<th><?php esc_html_e( 'Actions', 'wp-powersuite' ); ?></th>
							<?php endif; ?>
						</tr>
					</thead>
					<tbody>
						<?php foreach ( $sessions as $session ) : ?>
							<tr data-verifier="<?php echo esc_attr( (string) ( $session['verifier'] ?? '' ) ); ?>">
								<td>
									<?php echo esc_html( (string) ( $session['device'] ?? '' ) ); ?>
									<?php if ( ! empty( $session['is_current'] ) ) : ?>
										<span class="wpps-lsm-badge wpps-lsm-badge-current"><?php esc_html_e( 'Current session', 'wp-powersuite' ); ?></span>
									<?php endif; ?>
									<?php if ( in_array( 'login_as', $session['badges'] ?? [], true ) ) : ?>
										<span class="wpps-lsm-badge wpps-lsm-badge-login-as"><?php esc_html_e( 'Login as User', 'wp-powersuite' ); ?></span>
									<?php endif; ?>
									<?php if ( in_array( 'temporary_login', $session['badges'] ?? [], true ) ) : ?>
										<span class="wpps-lsm-badge wpps-lsm-badge-temp"><?php esc_html_e( 'Temporary Login', 'wp-powersuite' ); ?></span>
									<?php endif; ?>
								</td>
								<?php if ( $show_ip ) : ?>
									<td><?php echo esc_html( (string) ( $session['ip'] ?? '' ) ); ?></td>
								<?php endif; ?>
								<td><?php echo esc_html( (string) ( $session['login_formatted'] ?? '' ) ); ?></td>
								<td><?php echo esc_html( (string) ( $session['expiration_formatted'] ?? '' ) ); ?></td>
								<?php if ( $can_revoke ) : ?>
									<td>
										<button type="button" class="button button-secondary wpps-lsm-destroy-session" data-user-id="<?php echo esc_attr( (string) $user->ID ); ?>" data-verifier="<?php echo esc_attr( (string) ( $session['verifier'] ?? '' ) ); ?>">
											<?php esc_html_e( 'Log out session', 'wp-powersuite' ); ?>
										</button>
									</td>
								<?php endif; ?>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			<?php endif; ?>

			<?php if ( $can_revoke && $sessions !== [] ) : ?>
				<p>
					<button type="button" class="button wpps-lsm-destroy-all-user" data-user-id="<?php echo esc_attr( (string) $user->ID ); ?>">
						<?php esc_html_e( 'Log out everywhere', 'wp-powersuite' ); ?>
					</button>
				</p>
			<?php endif; ?>
		</div>
		<?php
	}
}
