<?php
/**
 * Handles users.
 *
 * @package flo-checkout
 */

namespace Flo_Checkout;

use WC_Payment_Tokens;
use WP_User;

/**
 * The Users class.
 */
class Users {
	/**
	 * Sets up the necessary actions for the Flo Checkout plugin.
	 */
	public static function setup(): void {
		if ( Shared::instance()->debug->is_on() ) {
			add_action( 'show_user_profile', array( self::class, 'show_user_tokens' ), 30 );
		}
	}

	/**
	 * Show user tokens.
	 *
	 * @param WP_User $user The user.
	 *
	 * @return void
	 */
	public static function show_user_tokens( WP_User $user ): void {
		$gateways = WC()->payment_gateways->payment_gateways();

		echo '<h2>' . esc_html__( 'Payment Tokens', 'flo-checkout-plugin' ) . '</h2>';

		echo '<ul class="token-list">';
		foreach ( $gateways as $gateway ) {
			if ( ! $gateway->supports( 'tokenization' ) ) {
				continue;
			}
			echo '<li>';

			echo esc_html( $gateway->get_title() );

			$tokens = WC_Payment_Tokens::get_customer_tokens( $user->ID, $gateway->id );

			if ( ! empty( $tokens ) ) {
				echo '<ul>';
				foreach ( $tokens as $token ) {
					echo '<li>';
					echo esc_html( $token->get_display_name() );
					echo '</li>';
				}
				echo '</ul>';
			}
		}
		echo '</ul>';
	}
}
