<?php
/**
 * PasskeysDashboardContributor - registers passkey stats on the main dashboard.
 *
 * @package UltraguardPro\modules\authenticator
 */

namespace UltraguardPro\modules\authenticator;

use UltraguardPro\Plugin;

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

class PasskeysDashboardContributor {

    private Plugin $plugin;

    public function __construct( Plugin $plugin ) {
        $this->plugin = $plugin;
    }

    public function register(): void {
        add_action( 'admin_init', [ $this, 'contribute' ], 20 );
    }

    public function contribute(): void {
        if ( ! \UltraguardPro\core\Admin::is_ultraguard_dashboard() ) { return; }

        $db = $this->plugin->modules->get_db_tables();
        if ( ! $db ) { return; }

        $passkeys_table = $db->get_table( 'passkeys' );
        if ( ! $passkeys_table ) { return; }

        // Total enrolled users (distinct user_id)
        $enrolled_users = (int) $passkeys_table->count_unique( 'user_id' );

        // Total users for context
        $total_users = (int) count_users()['total_users'];
        $pct         = $total_users > 0 ? round( ( $enrolled_users / $total_users ) * 100 ) : 0;

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'              => 'stat-passkeys-enrolled',
            'title'           => __( 'Passkey Users', 'ultraguard-security-suite' ),
            'value'           => $enrolled_users,
            'format'          => 'number',
            'icon'            => 'dashicons-fingerprint',
            'class'           => $enrolled_users > 0 ? 'success' : 'info',
            'animated'        => true,
            'priority'        => 22,
            'span'            => 3,
            'hide_when_empty' => true,
            'link'            => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=passkeys' ),
            'tooltip'         => sprintf(
                /* translators: %d enrolled users, %d percent */
                __( '%1$d of %2$d users enrolled (%3$d%%)', 'ultraguard-security-suite' ),
                $enrolled_users, $total_users, $pct
            ),
        ] );

        // Status item
        $enforced_roles = (array) $this->plugin->settings->get_option( 'passkey_required_roles', [] );
        do_action( 'ultraguard_dashboard_register_status', [
            'id'       => 'status-passkeys',
            'label'    => __( 'Passkeys', 'ultraguard-security-suite' ),
            'status'   => $enrolled_users > 0 ? 'active' : 'warning',
            'detail'   => empty( $enforced_roles )
                ? __( 'Optional - not enforced for any role', 'ultraguard-security-suite' )
                /* translators: %s: string */
                : sprintf( __( 'Required for: %s', 'ultraguard-security-suite' ), implode( ', ', $enforced_roles ) ),
            'icon'     => 'dashicons-fingerprint',
            'priority' => 35,
            'button_style' => 'secondary',
            'size'  => 'small',
            'link'     => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=passkeys' ),
        ] );
    }
}
