<?php

namespace UltraguardPro\modules\uptime_monitor;

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

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

class UptimeMonitorNotifications {

    private Plugin $plugin;

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

    public function register(): void {
        add_action( 'ultraguard_pro/uptime_monitor/status_changed', [ $this, 'notify_status_change' ], 10, 2 );
        add_action( 'ultraguard_pro/uptime_monitor/ssl_checked', [ $this, 'notify_ssl_warning' ], 10, 1 );
    }

    public function notify_status_change( array $previous, array $current ): void {
        $email = get_option( 'admin_email' );
        if ( ! is_email( $email ) ) {
            return;
        }

        $new_status = (string) ( $current['status'] ?? '' );
        if ( in_array( $new_status, [ 'down', 'timeout', 'error' ], true ) && ! $this->plugin->settings->get_option( 'uptime_monitor_downtime_email', true ) ) {
            return;
        }

        if ( 'up' === $new_status && ! $this->plugin->settings->get_option( 'uptime_monitor_recovery_email', true ) ) {
            return;
        }

        /* translators: %s: string */
        $subject = sprintf( __( 'UltraGuard Uptime Monitor: site status changed to %s', 'ultraguard-security-suite' ), strtoupper( $new_status ) );
        MailManager::notification( [
            'to'        => $email,
            'subject'   => $subject,
            'title'     => __( 'Site status changed', 'ultraguard-security-suite' ),
            'message'   => 'up' === $new_status
                ? __( 'Your site has recovered and is responding again.', 'ultraguard-security-suite' )
                : __( 'UltraGuard detected a downtime or degraded uptime event.', 'ultraguard-security-suite' ),
            'tone'      => 'up' === $new_status ? 'success' : 'danger',
            'details'   => [
                __( 'URL', 'ultraguard-security-suite' )             => (string) ( $current['check_url'] ?? home_url() ),
                __( 'Previous status', 'ultraguard-security-suite' ) => (string) ( $previous['status'] ?? 'unknown' ),
                __( 'Current status', 'ultraguard-security-suite' )  => $new_status,
                __( 'Response code', 'ultraguard-security-suite' )   => (string) ( $current['response_code'] ?? '' ),
                __( 'Checked at', 'ultraguard-security-suite' )      => (string) ( $current['created_at'] ?? current_time( 'mysql', true ) ),
            ],
            'cta_url'   => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=uptime-monitor' ),
            'cta_label' => __( 'Open Uptime Monitor', 'ultraguard-security-suite' ),
        ] );
    }

    public function notify_ssl_warning( array $ssl ): void {
        $email = get_option( 'admin_email' );
        if ( ! is_email( $email ) ) {
            return;
        }

        $days = isset( $ssl['days_remaining'] ) ? (int) $ssl['days_remaining'] : 0;
        if ( $days < 7 && ! $this->plugin->settings->get_option( 'uptime_monitor_ssl_alert_7', true ) ) {
            return;
        }
        if ( $days < 30 && $days >= 7 && ! $this->plugin->settings->get_option( 'uptime_monitor_ssl_alert_30', true ) ) {
            return;
        }
        if ( $days >= 30 || $days <= 0 ) {
            return;
        }

        /* translators: %d: integer */
        $subject = sprintf( __( 'UltraGuard SSL Alert: certificate expires in %d day(s)', 'ultraguard-security-suite' ), $days );
        MailManager::notification( [
            'to'        => $email,
            'subject'   => $subject,
            'title'     => __( 'SSL certificate expiry warning', 'ultraguard-security-suite' ),
            'message'   => __( 'Your SSL certificate is approaching expiry and should be renewed soon.', 'ultraguard-security-suite' ),
            'tone'      => $days < 7 ? 'danger' : 'warning',
            'details'   => [
                __( 'Domain', 'ultraguard-security-suite' )         => (string) ( $ssl['domain'] ?? wp_parse_url( home_url(), PHP_URL_HOST ) ),
                __( 'Expiry date', 'ultraguard-security-suite' )    => (string) ( $ssl['expiry_date'] ?? '' ),
                __( 'Days remaining', 'ultraguard-security-suite' ) => $days,
                __( 'Issuer', 'ultraguard-security-suite' )         => (string) ( $ssl['issuer'] ?? '' ),
            ],
            'cta_url'   => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=uptime-monitor' ),
            'cta_label' => __( 'Review SSL Status', 'ultraguard-security-suite' ),
        ] );
    }
}
