<?php
defined( 'ABSPATH' ) || exit;

global $wpdb;

$current_page     = isset( $_GET['paged'] ) ? max( 1, intval( wp_unslash( $_GET['paged'] ) ) ) : 1;
$records_per_page = 30;
$offset           = ( $current_page - 1 ) * $records_per_page;

$total_users   = count_users();
$total_count   = $total_users['total_users'];
$enabled_count = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
	"SELECT COUNT(*) FROM {$wpdb->prefix}zacls_2fa_status WHERE enabled = 1" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
);

$results = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
	$wpdb->prepare(
		"SELECT s.*, u.user_login, u.display_name, u.user_email
		FROM {$wpdb->prefix}zacls_2fa_status s
		LEFT JOIN {$wpdb->users} u ON s.user_id = u.ID
		ORDER BY s.updated_at DESC
		LIMIT %d OFFSET %d",
		$records_per_page,
		$offset
	),
	ARRAY_A
);

$total_2fa_users = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}zacls_2fa_status" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery
$total_pages     = ceil( $total_2fa_users / $records_per_page );
$pct             = $total_count > 0 ? round( ( $enabled_count / $total_count ) * 100 ) : 0;
?>
<div class="wrap">
	<div class="zacls-action-bar">
		<div class="zacls-action-left">
			<h1 class="zacls-page-title"><?php esc_html_e( '2FA User Status', 'za-creative-login-shield' ); ?></h1>
		</div>
		<div class="zacls-action-right">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=settings' ) ); ?>" class="zacls-btn zacls-btn-outline"><?php esc_html_e( '2FA Settings', 'za-creative-login-shield' ); ?></a>
		</div>
	</div>

	<div class="zacls-stats-grid">
		<div class="zacls-stat-card">
			<div class="stat-icon twofa"><span class="dashicons dashicons-admin-users"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $enabled_count ); ?>/<?php echo esc_html( $total_count ); ?></span>
				<span class="stat-label"><?php esc_html_e( '2FA Enabled', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
		<div class="zacls-stat-card">
			<div class="stat-icon twofa"><span class="dashicons dashicons-yes-alt"></span></div>
			<div class="stat-content">
				<span class="stat-value"><?php echo esc_html( $pct ); ?>%</span>
				<span class="stat-label"><?php esc_html_e( 'Adoption Rate', 'za-creative-login-shield' ); ?></span>
			</div>
		</div>
	</div>

	<?php if ( empty( $results ) ) : ?>
		<div class="zacls-empty-state">
			<span class="dashicons dashicons-lock"></span>
			<p><?php esc_html_e( 'No users have configured 2FA yet.', 'za-creative-login-shield' ); ?></p>
			<p><a href="<?php echo esc_url( admin_url( 'admin.php?page=zacls&tab=settings' ) ); ?>"><?php esc_html_e( 'Enable 2FA', 'za-creative-login-shield' ); ?></a> <?php esc_html_e( 'from the settings page to require it for all users or specific roles.', 'za-creative-login-shield' ); ?></p>
		</div>
	<?php else : ?>
		<div class="zacls-table-wrap">
			<table class="wp-list-table widefat fixed striped">
				<thead>
					<tr>
						<th><?php esc_html_e( 'User', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( 'Role', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( '2FA Status', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( 'Method', 'za-creative-login-shield' ); ?></th>
						<th><?php esc_html_e( 'Last Updated', 'za-creative-login-shield' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $results as $row ) : ?>
						<?php $user_info = get_userdata( $row['user_id'] ); ?>
						<tr>
							<td>
								<strong><?php echo esc_html( $row['display_name'] ? $row['display_name'] : $row['user_login'] ); ?></strong>
								<br><small><?php echo esc_html( $row['user_email'] ); ?></small>
							</td>
							<td><?php echo $user_info && ! empty( $user_info->roles ) ? esc_html( implode( ', ', $user_info->roles ) ) : '-'; ?></td>
							<td><span class="zacls-status-badge status-<?php echo esc_attr( $row['enabled'] ? 'active' : 'inactive' ); ?>"><?php echo $row['enabled'] ? esc_html_e( 'Enabled', 'za-creative-login-shield' ) : esc_html_e( 'Disabled', 'za-creative-login-shield' ); ?></span></td>
							<td><?php $methods = array( 'email' => __( 'Email OTP', 'za-creative-login-shield' ), 'totp' => __( 'Google Authenticator', 'za-creative-login-shield' ) ); echo esc_html( $methods[ $row['method'] ] ?? ucfirst( $row['method'] ) ); ?></td>
							<td><?php echo esc_html( $row['updated_at'] ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
			<?php if ( $total_pages > 1 ) : ?>
				<div class="tablenav bottom">
					<div class="tablenav-pages">
						<span class="displaying-num"><?php printf( esc_html__( '%d items', 'za-creative-login-shield' ), esc_html( $total_2fa_users ) ); ?></span>
						<?php echo wp_kses_post( paginate_links( array( 'base' => add_query_arg( 'paged', '%#%' ), 'format' => '', 'prev_text' => '&laquo;', 'next_text' => '&raquo;', 'total' => $total_pages, 'current' => $current_page, 'type' => 'list' ) ) ); ?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	<?php endif; ?>
</div>
