<?php

namespace UltraguardPro\modules\auto_updater;

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

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

class AutoUpdaterNotifications {

    private Plugin $plugin;

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

    public function register(): void {
        add_action( 'ultraguard_pro/auto_updater/batch_complete', [ $this, 'notify_batch_complete' ], 10, 1 );
        add_action( 'ultraguard_pro/auto_updater/update_failed', [ $this, 'notify_update_failed' ], 10, 1 );
    }

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

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

        $subject = __( 'UltraGuard Auto Updater summary', 'ultraguard-security-suite' );
        MailManager::notification( [
            'to'        => $email,
            'subject'   => $subject,
            'title'     => __( 'Automatic update batch completed', 'ultraguard-security-suite' ),
            'message'   => __( 'UltraGuard finished an automatic update run for your site.', 'ultraguard-security-suite' ),
            'tone'      => 'success',
            'details'   => [
                __( 'Applied', 'ultraguard-security-suite' )   => (int) ( $summary['applied'] ?? 0 ),
                __( 'Failed', 'ultraguard-security-suite' )    => (int) ( $summary['failed'] ?? 0 ),
                __( 'Skipped', 'ultraguard-security-suite' )   => (int) ( $summary['skipped'] ?? 0 ),
                __( 'Run type', 'ultraguard-security-suite' )  => (string) ( $summary['trigger_type'] ?? 'auto' ),
                __( 'Completed', 'ultraguard-security-suite' ) => current_time( 'mysql', true ),
            ],
            'cta_url'   => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=auto-updater' ),
            'cta_label' => __( 'Review Update History', 'ultraguard-security-suite' ),
        ] );
    }

    public function notify_update_failed( array $entry ): void {
        if ( ! $this->plugin->settings->get_option( 'auto_updater_alert_on_failure', true ) ) {
            return;
        }

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

        /* translators: %s: string */
        $subject = sprintf( __( 'UltraGuard update failed: %s', 'ultraguard-security-suite' ), (string) ( $entry['component_name'] ?? $entry['component_slug'] ?? '' ) );
        MailManager::notification( [
            'to'        => $email,
            'subject'   => $subject,
            'title'     => __( 'Automatic update failed', 'ultraguard-security-suite' ),
            'message'   => __( 'UltraGuard could not complete one of the requested updates.', 'ultraguard-security-suite' ),
            'tone'      => 'danger',
            'details'   => [
                __( 'Component', 'ultraguard-security-suite' ) => (string) ( $entry['component_name'] ?? '' ),
                __( 'Type', 'ultraguard-security-suite' )      => (string) ( $entry['component_type'] ?? '' ),
                __( 'From', 'ultraguard-security-suite' )      => (string) ( $entry['old_version'] ?? '' ),
                __( 'To', 'ultraguard-security-suite' )        => (string) ( $entry['new_version'] ?? '' ),
                __( 'Error', 'ultraguard-security-suite' )     => (string) ( $entry['error_message'] ?? __( 'Unknown error', 'ultraguard-security-suite' ) ),
            ],
            'cta_url'   => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=auto-updater' ),
            'cta_label' => __( 'Open Auto Updater', 'ultraguard-security-suite' ),
        ] );
    }
}
