<?php

namespace UltraguardPro\modules\woo_security;

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

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

class WooSecurityNotifications {

    private Plugin $plugin;

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

    public function register(): void {
        add_action( 'ultraguard_pro/woo_security/skimmer_scan_complete', [ $this, 'maybe_send_skimmer_alert' ], 10, 1 );
        add_action( 'ultraguard_pro/woo_security/honeypot_triggered', [ $this, 'maybe_send_honeypot_alert' ], 10, 1 );
    }

    public function maybe_send_skimmer_alert( array $results ): void {
        if ( empty( $results['threats_found'] ) || ! $this->plugin->settings->get_option( 'woo_security_alert_on_skimmer', true ) ) {
            return;
        }

        MailManager::notification( [
            'to'        => get_option( 'admin_email' ),
            'subject'   => __( 'UltraGuard detected possible WooCommerce skimmer code', 'ultraguard-security-suite' ),
            'title'     => __( 'Possible card skimmer code detected', 'ultraguard-security-suite' ),
            'message'   => __( 'UltraGuard found suspicious JavaScript patterns during the latest WooCommerce script scan.', 'ultraguard-security-suite' ),
            'tone'      => 'danger',
            'details'   => [
                __( 'Threats found', 'ultraguard-security-suite' ) => (int) $results['threats_found'],
                __( 'Store URL', 'ultraguard-security-suite' )     => home_url(),
            ],
            'cta_url'   => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=woo-security' ),
            'cta_label' => __( 'Review Woo Security', 'ultraguard-security-suite' ),
        ] );
    }

    public function maybe_send_honeypot_alert( array $context ): void {
        MailManager::notification( [
            'to'        => get_option( 'admin_email' ),
            'subject'   => __( 'WooCommerce checkout honeypot triggered', 'ultraguard-security-suite' ),
            'title'     => __( 'Checkout honeypot triggered', 'ultraguard-security-suite' ),
            'message'   => __( 'UltraGuard detected bot-like behavior on the WooCommerce checkout flow.', 'ultraguard-security-suite' ),
            'tone'      => 'warning',
            'details'   => [
                __( 'Store URL', 'ultraguard-security-suite' )  => home_url(),
                __( 'IP Address', 'ultraguard-security-suite' ) => sanitize_text_field( (string) ( $context['ip'] ?? '' ) ),
            ],
            'cta_url'   => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=woo-security' ),
            'cta_label' => __( 'Open Woo Security', 'ultraguard-security-suite' ),
        ] );
    }
}
