<?php

namespace UltraguardPro\modules\woo_security;

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

use UltraguardPro\Plugin;

class WooSecurityDashboardContributor {

    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() || ! class_exists( 'WooCommerce' ) ) {
            return;
        }

        $table   = $this->plugin->modules->get_db_tables()->get_table( 'skimmer_scan_results' );
        $records = $table && method_exists( $table, 'get_recent' ) ? $table->get_recent( 100 ) : [];
        $threats = count( array_filter( $records, static fn( $row ) => ! empty( $row['finding_type'] ) ) );

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-woo-threats-found',
            'title'    => __( 'Store Threat Findings', 'ultraguard-security-suite' ),
            'value'    => $threats,
            'format'   => 'number',
            'icon'     => 'dashicons-cart',
            'class'    => $threats > 0 ? 'warning' : 'success',
            'animated' => true,
            'hide_when_empty' => true,
            'priority' => 130,
            'span'     => 3,
        ] );

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