<?php
/**
 * FirewallDashboardContributor — Registers firewall data into the dashboard registry.
 *
 * Called from FirewallModule::boot(). Contributes:
 *   - Stat card: Threats Blocked
 *   - Status item: Firewall
 *   - Coverage bar: Firewall
 *   - Quick tile: Firewall
 *
 * @package UltraguardPro\modules\firewall
 */

namespace UltraguardPro\modules\firewall;

use UltraguardPro\Plugin;

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

class FirewallDashboardContributor {

    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();
        $bl_t  = $db ? $db->get_table( 'blocked_requests' ) : null;
        $r_t   = $db ? $db->get_table( 'firewall_rules' )   : null;
        $today = gmdate( 'Y-m-d 00:00:00' );

        $total_blocked  = $bl_t ? $bl_t->count() : 0;
        $blocked_today  = $bl_t ? $bl_t->count( [ 'created_at >=' => $today ] ) : 0;
        $active_rules   = $r_t  ? $r_t->count( [ 'is_active' => 1 ] ) : 0;
        $enabled        = (bool) get_option( 'firewall_waf_enabled', true );

        // Stat card
        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-threats-blocked',
            'title'    => __( 'Threats Blocked', 'ultraguard-security-suite' ),
            'value'    => $total_blocked,
            'format'   => 'number',
            'icon'     => 'dashicons-lock',
            'class'    => 'info',
            'animated' => true,
            'priority' => 10,
            'span'     => 3,
            'hide_when_empty' => true,
        ] );

        // Module status item
        do_action( 'ultraguard_dashboard_register_status', [
            'id'       => 'status-firewall',
            'icon'     => 'dashicons-shield',
            'text'     => __( 'Firewall', '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' ),
                'button_style' => 'secondary',
                'size'=>'small',
                'url' => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=firewall' )
                ] ],
            'priority' => 10,
        ] );

        // Coverage bar
        do_action( 'ultraguard_dashboard_register_coverage', [
            'id'       => 'coverage-firewall',
            'title'    => __( 'Firewall', 'ultraguard-security-suite' ),
            'value'    => $enabled ? 100 : 0,
            'max'      => 100,
            'status'   => $enabled ? 'success' : 'secondary',
            'label'    => $active_rules . ' ' . __( 'active rules', 'ultraguard-security-suite' ),
            'priority' => 10,
        ] );

        // Quick tile
        do_action( 'ultraguard_dashboard_register_tile', [
            'title'    => __( 'Firewall', 'ultraguard-security-suite' ),
            'icon'     => 'dashicons-shield',
            '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=firewall' ) ],
            'priority' => 10,
        ] );
    }
}
