<?php

namespace UltraguardPro\modules\authenticator;

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

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

class TwoFactorNotifications {

    private Plugin $plugin;

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

    public function register(): void {
        add_action( 'ultraguard_pro/two_factor/enabled', [ $this, 'notify_enabled' ], 10, 2 );
        add_action( 'ultraguard_pro/two_factor/disabled', [ $this, 'notify_disabled' ], 10, 2 );
        add_action( 'ultraguard_pro/two_factor/backup_regenerated', [ $this, 'notify_backup_regenerated' ], 10, 2 );
        add_action( 'ultraguard_pro/two_factor/account_locked', [ $this, 'notify_account_locked' ], 10, 2 );
    }

    public function notify_enabled( int $user_id, array $context = [] ): void {
        $this->send_user_notice( $user_id, __( 'Two-factor authentication enabled', 'ultraguard-security-suite' ), __( 'Two-factor authentication has been enabled on your account.', 'ultraguard-security-suite' ), 'two_factor_enabled', $context );
    }

    public function notify_disabled( int $user_id, array $context = [] ): void {
        $this->send_user_notice( $user_id, __( 'Two-factor authentication disabled', 'ultraguard-security-suite' ), __( 'Two-factor authentication has been disabled on your account.', 'ultraguard-security-suite' ), 'two_factor_disabled', $context );
    }

    public function notify_backup_regenerated( int $user_id, array $context = [] ): void {
        $this->send_user_notice( $user_id, __( 'Backup codes regenerated', 'ultraguard-security-suite' ), __( 'Your two-factor backup codes were regenerated. Store the new codes somewhere safe.', 'ultraguard-security-suite' ), 'two_factor_backup_codes', $context );
    }

    public function notify_account_locked( int $user_id, array $context = [] ): void {
        $this->send_user_notice( $user_id, __( 'Account locked after failed two-factor attempts', 'ultraguard-security-suite' ), __( 'Your account was temporarily locked after multiple invalid two-factor codes.', 'ultraguard-security-suite' ), 'two_factor_locked', $context );
    }

    private function send_user_notice( int $user_id, string $subject, string $message, string $event_type, array $context ): void {
        if ( ! (bool) $this->plugin->settings->get_option( 'two_factor_email_notifications', true ) ) {
            return;
        }

        $user = get_userdata( $user_id );
        if ( ! $user || ! is_email( $user->user_email ) ) {
            return;
        }

        MailManager::notification( [
            'to'        => $user->user_email,
            'subject'   => $subject,
            'title'     => $subject,
            'message'   => $message,
            'tone'      => 'warning',
            'details'   => array_merge( [
                __( 'Site', 'ultraguard-security-suite' )     => get_bloginfo( 'name' ),
                __( 'Username', 'ultraguard-security-suite' ) => $user->user_login,
            ], $context ),
            'cta_url'   => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=authenticator' ),
            'cta_label' => __( 'Open Authenticator', 'ultraguard-security-suite' ),
        ] );

        do_action( 'ultraguard_security_event_fired', $event_type, 'medium', array_merge( [
            'user_id'  => $user_id,
            'username' => $user->user_login,
        ], $context ) );
    }
}
