<?php
/**
 * AntivirusDashboardContributor — Registers antivirus data into the dashboard registry.
 *
 * @package UltraguardPro\modules\antivirus
 */

namespace UltraguardPro\modules\antivirus;

use UltraguardPro\Plugin;

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

class AntivirusDashboardContributor {

    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();
        $scan_t  = $db ? $db->get_table( 'scan_results' ) : null;
        $quar_t  = $db ? $db->get_table( 'quarantine' )   : null;
        $enabled = (bool) get_option( 'antivirus_enabled', true );

        $threats_found  = $scan_t ? $scan_t->count( [ 'status' => 'infected' ] ) : 0;
        $quarantined    = $quar_t ? $quar_t->count()                              : 0;

        // Stat: Malware found
        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-malware-found',
            'title'    => __( 'Malware Found', 'ultraguard-security-suite' ),
            'value'    => $threats_found,
            'format'   => 'number',
            'icon'     => 'dashicons-warning',
            'class'    => $threats_found > 0 ? 'danger' : 'success',
            'animated' => true,
            'priority' => 50,
            'span'     => 3,
            'hide_when_empty' => true,
        ] );

        // Status item
        do_action( 'ultraguard_dashboard_register_status', [
            'id'       => 'status-antivirus',
            'icon'     => 'dashicons-search',
            'text'     => __( 'Antivirus', 'ultraguard-security-suite' ),
            'badge'    => [ 'text' => $enabled ? __( 'Active', 'ultraguard-security-suite' ) : __( 'Off', 'ultraguard-security-suite' ), 'badge_type' => $enabled ? 'success' : 'secondary' ],
            'actions'  => [ [
                'type' => 'button',
                'text' => __( 'Manage', 'ultraguard-security-suite' ),
                'size' => 'small',
                'button_style' => 'secondary',
                'url' => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=antivirus' )
                ] ],
            'priority' => 30,
        ] );

        // Coverage bar
        do_action( 'ultraguard_dashboard_register_coverage', [
            'id'       => 'coverage-antivirus',
            'title'    => __( 'Antivirus', 'ultraguard-security-suite' ),
            'value'    => $enabled ? ( $threats_found > 0 ? 60 : 100 ) : 0,
            'max'      => 100,
            'status'   => $threats_found > 0 ? 'danger' : ( $enabled ? 'success' : 'secondary' ),
            'label'    => $threats_found > 0
                /* translators: %d: number */
                ? sprintf( _n( '%d threat found', '%d threats found', $threats_found, 'ultraguard-security-suite' ), $threats_found )
                : __( 'Clean', 'ultraguard-security-suite' ),
            'priority' => 30,
        ] );

        // Quick tile
        do_action( 'ultraguard_dashboard_register_tile', [
            'title'    => __( 'Antivirus', 'ultraguard-security-suite' ),
            'icon'     => 'dashicons-search',
            'content'  => $enabled ? __( 'Active', 'ultraguard-security-suite' ) : __( 'Inactive', 'ultraguard-security-suite' ),
            'footer'   => [ 'type' => 'button', 'text' => __( 'Open', 'ultraguard-security-suite' ), 'button_style' => 'secondary', 'url' => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=antivirus' ) ],
            'priority' => 30,
        ] );

        if ( $threats_found > 0 ) {
            do_action( 'ultraguard_dashboard_register_recommendation', [
                /* translators: %d: number */
                'title'    => sprintf( _n( '%d malware threat detected — scan and quarantine immediately', '%d malware threats detected — scan and quarantine immediately', $threats_found, 'ultraguard-security-suite' ), $threats_found ),
                'priority' => 'critical',
            ] );
        }
    }
}