<?php

namespace UltraguardPro\modules\traffic_monitor;

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

use UltraguardPro\Plugin;

class TrafficMonitorDashboardContributor {

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

        $engine = new TrafficCaptureEngine( $this->plugin );
        $stats  = $engine->get_overview_stats( 24 );

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-traffic-requests-24h',
            'title'    => __( 'Traffic Requests', 'ultraguard-security-suite' ),
            'value'    => $stats['total_requests'],
            'format'   => 'number',
            'icon'     => 'dashicons-chart-area',
            'class'    => 'info',
            'hide_when_empty' => true,
            'animated' => true,
            'priority' => 65,
            'span'     => 3,
        ] );

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-traffic-threats-24h',
            'title'    => __( 'Traffic Threats', 'ultraguard-security-suite' ),
            'value'    => $stats['threats'],
            'format'   => 'number',
            'icon'     => 'dashicons-warning',
            'class'    => $stats['threats'] > 0 ? 'warning' : 'success',
            'animated' => true,
            'priority' => 66,
            'hide_when_empty' => true,
            'span'     => 3,
        ] );

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-traffic-blocked-24h',
            'title'    => __( 'Blocked Traffic', 'ultraguard-security-suite' ),
            'value'    => $stats['blocked'],
            'format'   => 'number',
            'icon'     => 'dashicons-shield',
            'class'    => $stats['blocked'] > 0 ? 'success' : 'secondary',
            'animated' => true,
            'priority' => 67,
            'hide_when_empty' => true,
            'span'     => 3,
        ] );

        do_action( 'ultraguard_dashboard_register_status', [
            'id'      => 'status-traffic-monitor',
            'icon'    => 'dashicons-chart-area',
            'text'    => __( 'Live Traffic Monitor', 'ultraguard-security-suite' ),
            'badge'   => [
                'text'       => $this->plugin->settings->get_option( 'traffic_monitor_enabled', true ) ? __( 'Active', 'ultraguard-security-suite' ) : __( 'Disabled', 'ultraguard-security-suite' ),
                'badge_type' => $this->plugin->settings->get_option( 'traffic_monitor_enabled', true ) ? 'success' : 'secondary',
            ],
            'actions' => [
                [
                    'type' => 'button',
                    'button_style' => 'secondary',
                    'size'  => 'small',
                    'text' => __( 'Open', 'ultraguard-security-suite' ),
                    'url'  => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=traffic-monitor' ),
                ],
            ],
        ] );
    }
}
