<?php
/**
 * Account Dashboard Template
 *
 * Default dashboard/overview for the account area.
 *
 * @package AmbassadorHub\Accounts\Blocks\Account
 */

defined( 'ABSPATH' ) || exit;

use AmbassadorHub\Accounts\EmailVerification;
use AmbassadorHub\Admin\InlineAssets;

$ambh_user        = wp_get_current_user();
$ambh_is_verified = EmailVerification::user_is_verified( $ambh_user->ID );
$ambh_verif_param = isset( $_GET['ambh_verification'] ) ? sanitize_key( $_GET['ambh_verification'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display feedback param.
?>

<div class="ambassador-account__dashboard">
	<h2>
	<?php
		// translators: %s: user's display name.
		echo esc_html( sprintf( __( 'Welcome, %s', 'ambassadorhub' ), $ambh_user->display_name ) );
	?>
	</h2>

	<?php if ( ! $ambh_is_verified ) : ?>
		<div class="ambassador-account__notice ambassador-account__notice--warning">
			<p class="ambassador-account__notice-title">
				<?php esc_html_e( 'Your email address is not verified yet.', 'ambassadorhub' ); ?>
			</p>
			<p>
				<?php esc_html_e( 'We sent you a verification email when you registered. Check your inbox — and your spam folder — and click the link inside to activate your account.', 'ambassadorhub' ); ?>
			</p>
			<?php if ( 'sent' === $ambh_verif_param ) : ?>
				<p class="ambassador-account__notice-feedback ambassador-account__notice-feedback--success">
					<?php esc_html_e( 'Verification email sent! Check your inbox.', 'ambassadorhub' ); ?>
				</p>
			<?php elseif ( 'cooldown' === $ambh_verif_param ) : ?>
				<p class="ambassador-account__notice-feedback ambassador-account__notice-feedback--error">
					<?php esc_html_e( 'Please wait a moment before requesting another email.', 'ambassadorhub' ); ?>
				</p>
			<?php elseif ( 'error' === $ambh_verif_param ) : ?>
				<p class="ambassador-account__notice-feedback ambassador-account__notice-feedback--error">
					<?php esc_html_e( 'Could not send the email. Please try again in a moment.', 'ambassadorhub' ); ?>
				</p>
			<?php endif; ?>
			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="ambassador-account__notice-form" id="ambh-resend-form">
				<input type="hidden" name="action" value="ambh_resend_verification_email">
				<?php wp_nonce_field( 'ambh_resend_verification', 'ambh_nonce' ); ?>
				<button type="submit" class="ambassador-account__button ambassador-account__button--secondary" id="ambh-resend-btn">
					<?php esc_html_e( 'Resend verification email', 'ambassadorhub' ); ?><span id="ambh-resend-countdown"></span>
				</button>
			</form>
			<?php
			InlineAssets::script(
				'( function () {
					var userId   = ' . (int) $ambh_user->ID . ';
					var cooldown = ' . (int) EmailVerification::RESEND_COOLDOWN . ';
					var key      = \'ambh_resend_cd_\' + userId;
					var btn      = document.getElementById( \'ambh-resend-btn\' );
					var label    = document.getElementById( \'ambh-resend-countdown\' );
					var form     = document.getElementById( \'ambh-resend-form\' );

					function startCountdown( remaining ) {
						btn.disabled = true;
						( function tick() {
							if ( remaining <= 0 ) {
								btn.disabled = false;
								label.textContent = \'\';
								localStorage.removeItem( key );
								return;
							}
							label.textContent = \' (\' + remaining + \'s)\';
							remaining--;
							setTimeout( tick, 1000 );
						} )();
					}

					var stored = localStorage.getItem( key );
					if ( stored ) {
						var elapsed   = Math.floor( ( Date.now() - parseInt( stored, 10 ) ) / 1000 );
						var remaining = cooldown - elapsed;
						if ( remaining > 0 ) {
							startCountdown( remaining );
						} else {
							localStorage.removeItem( key );
						}
					}

					form.addEventListener( \'submit\', function () {
						localStorage.setItem( key, Date.now().toString() );
					} );
				} )();'
			);
			?>
		</div>
	<?php endif; ?>

	<?php if ( 'success' === $ambh_verif_param ) : ?>
		<div class="ambassador-account__notice ambassador-account__notice--success">
			<p><?php esc_html_e( 'Your email address has been verified successfully. Welcome!', 'ambassadorhub' ); ?></p>
		</div>
	<?php elseif ( 'already_verified' === $ambh_verif_param ) : ?>
		<div class="ambassador-account__notice ambassador-account__notice--success">
			<p><?php esc_html_e( 'Your account is already verified.', 'ambassadorhub' ); ?></p>
		</div>
	<?php endif; ?>

	<div class="ambassador-account__dashboard-content">
		<?php
		/**
		 * Fires in the account dashboard.
		 *
		 * Modules can add dashboard widgets here (stats, recent campaigns, etc).
		 *
		 * @since 0.1.0
		 *
		 * @param WP_User $ambh_user Current user object.
		 */
		do_action( 'ambh_account_dashboard', $ambh_user );
		?>
	</div>
</div>
