<?php

namespace UltraguardPro\modules\authenticator;

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

use UltraguardPro\Plugin;

class TwoFactorDashboardContributor {

    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();
        $table  = $db ? $db->get_table( 'totp_credentials' ) : null;
        $engine = new TwoFactorEngine( $this->plugin );

        $enabled  = $table && method_exists( $table, 'count_enabled_users' ) ? (int) $table->count_enabled_users() : 0;
        $enforced = $table && method_exists( $table, 'count_enforced_users' ) ? (int) $table->count_enforced_users() : 0;
        $blocked  = $engine->count_blocked_today();

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-2fa-users-enabled',
            'title'    => __( '2FA Users', 'ultraguard-security-suite' ),
            'value'    => $enabled,
            'format'   => 'number',
            'icon'     => 'dashicons-shield-alt',
            'class'    => $enabled > 0 ? 'success' : 'secondary',
            'animated' => true,
            'priority' => 56,
            'hide_when_empty' => true,
            'span'     => 3,
        ] );

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-2fa-logins-verified-today',
            'title'    => __( '2FA Blocks Today', 'ultraguard-security-suite' ),
            'value'    => $blocked,
            'format'   => 'number',
            'icon'     => 'dashicons-lock',
            'class'    => $blocked > 0 ? 'warning' : 'success',
            'animated' => true,
            'hide_when_empty' => true,
            'priority' => 57,
            'span'     => 3,
        ] );

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-2fa-roles-enforced',
            'title'    => __( 'Roles Enforced', 'ultraguard-security-suite' ),
            'value'    => count( (array) $this->plugin->settings->get_option( 'two_factor_required_roles', [] ) ),
            'format'   => 'number',
            'icon'     => 'dashicons-admin-users',
            'class'    => 'info',
            'animated' => true,
            'hide_when_empty' => true,
            'priority' => 58,
            'span'     => 3,
        ] );

        do_action( 'ultraguard_dashboard_register_status', [
            'id'      => 'status-two-factor',
            'icon'    => 'dashicons-shield-alt',
            'text'    => __( 'Two-Factor Authentication', 'ultraguard-security-suite' ),
            'badge'   => [
                'text'       => $this->plugin->settings->get_option( 'two_factor_enabled', true ) ? __( 'Active', 'ultraguard-security-suite' ) : __( 'Disabled', 'ultraguard-security-suite' ),
                'badge_type' => $this->plugin->settings->get_option( 'two_factor_enabled', true ) ? 'success' : 'secondary',
            ],
            'actions' => [
                [
                    'type' => 'button',
                    'button_style' => 'secondary',
                    'size'  => 'small',
                    'text' => __( 'Manage', 'ultraguard-security-suite' ),
                    'url'  => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=two-factor' ),
                ],
            ],
        ] );
    }
}
