<?php

namespace UltraguardPro\modules\reputation_monitor;

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

use UltraguardPro\Plugin;

class ReputationMonitorDashboardContributor {

    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 ReputationCheckEngine( $this->plugin );
        $summary = $engine->run_quick_summary();

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-reputation-sources',
            'title'    => __( 'Reputation Sources', 'ultraguard-security-suite' ),
            'value'    => (int) $summary['sources_monitoring'],
            'format'   => 'number',
            'icon'     => 'dashicons-search',
            'class'    => 'info',
            'animated' => true,
            'priority' => 115,
            'span'     => 3,
                        'hide_when_empty' => true,
        ] );

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-reputation-blacklisted',
            'title'    => __( 'Blacklisted Sources', 'ultraguard-security-suite' ),
            'value'    => (int) $summary['currently_blacklisted'],
            'format'   => 'number',
            'icon'     => 'dashicons-warning',
            'class'    => (int) $summary['currently_blacklisted'] > 0 ? 'danger' : 'success',
            'animated' => true,
            'priority' => 116,
            'hide_when_empty' => true,
            'span'     => 3,
        ] );

        do_action( 'ultraguard_dashboard_register_status', [
            'id'      => 'status-reputation-monitor',
            'icon'    => 'dashicons-search',
            'text'    => __( 'Reputation Monitor', 'ultraguard-security-suite' ),
            'badge'   => [
                'text'       => 'clean' === $summary['overall_status'] ? __( 'Clean', 'ultraguard-security-suite' ) : __( 'Needs Attention', 'ultraguard-security-suite' ),
                'badge_type' => 'clean' === $summary['overall_status'] ? 'success' : 'danger',
            ],
            '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=reputation-monitor' ),
                ],
            ],
        ] );
    }
}
