<?php

namespace UltraguardPro\modules\api_security;

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

use UltraguardPro\Plugin;

class ApiSecurityDashboardContributor {

    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;
        }

        $stats = ( new ApiSecurityEngine( $this->plugin ) )->get_stats();

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-api-blocked-24h',
            'title'    => __( 'API Blocks (24h)', 'ultraguard-security-suite' ),
            'value'    => (int) $stats['blocked_24h'],
            'format'   => 'number',
            'icon'     => 'dashicons-rest-api',
            'class'    => (int) $stats['blocked_24h'] > 0 ? 'warning' : 'success',
            'animated' => true,
            'priority' => 18,
            'span'     => 3,
            'hide_when_empty' => true,
        ] );

        do_action( 'ultraguard_dashboard_register_status', [
            'id'      => 'status-api-security',
            'icon'    => 'dashicons-rest-api',
            'text'    => __( 'API Security', 'ultraguard-security-suite' ),
            'badge'   => [ 'text' => __( 'Active', 'ultraguard-security-suite' ), 'badge_type' => 'success' ],
            'actions' => [ [ 'type' => 'button', 'button_style' => 'secondary', 'size'  => 'small', 'text' => __( 'Manage', 'ultraguard-security-suite' ) ] ],
        ] );
    }
}
