<?php

namespace UltraguardPro\modules\ip_intelligence;

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

use UltraguardPro\Plugin;
use UltraguardPro\core\MailManager;

class IpIntelligenceNotifications {

    private Plugin $plugin;

    public function __construct( Plugin $plugin ) {
        $this->plugin = $plugin;
    }

    public function register(): void {
        add_action( 'ultraguard_pro/ip_intelligence/high_confidence_added', [ $this, 'notify_high_confidence' ], 10, 1 );
    }

    public function notify_high_confidence( array $summary ): void {
        if ( ! $this->plugin->settings->get_option( 'ip_intelligence_alert_on_high_confidence', true ) ) {
            return;
        }

        $email = get_option( 'admin_email' );
        if ( ! is_email( $email ) ) {
            return;
        }

        $subject = __( 'UltraGuard IP Intelligence synced new high-confidence threats', 'ultraguard-security-suite' );
        MailManager::notification( [
            'to'        => $email,
            'subject'   => $subject,
            'title'     => __( 'New high-confidence threats added', 'ultraguard-security-suite' ),
            'message'   => __( 'UltraGuard synchronized new high-confidence threat intelligence records.', 'ultraguard-security-suite' ),
            'tone'      => 'danger',
            'details'   => [
                __( 'AbuseIPDB', 'ultraguard-security-suite' )          => (int) ( $summary['abuseipdb'] ?? 0 ),
                __( 'Spamhaus', 'ultraguard-security-suite' )           => (int) ( $summary['spamhaus'] ?? 0 ),
                __( 'UltraGuard Network', 'ultraguard-security-suite' ) => (int) ( $summary['ultraguard_network'] ?? 0 ),
                __( 'Total', 'ultraguard-security-suite' )              => (int) ( $summary['total'] ?? 0 ),
                __( 'Synced', 'ultraguard-security-suite' )             => (string) ( $summary['synced_at'] ?? current_time( 'mysql', true ) ),
            ],
            'cta_url'   => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=ip-intelligence' ),
            'cta_label' => __( 'Open IP Intelligence', 'ultraguard-security-suite' ),
        ] );
    }
}
