<?php
/**
 * NotificationsDashboardContributor — Registers Notifications data into the main dashboard.
 *
 * Contributes:
 *   • stat-card  : "Sent Today" with trend
 *   • status item: Notifications active/inactive
 *   • tile       : Quick-access tile with failed count badge
 *   • recommendations: failed deliveries, no rules configured
 *
 * @package UltraguardPro\modules\notifications
 * @since   6.5.0
 */

namespace UltraguardPro\modules\notifications;

use UltraguardPro\Plugin;

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

class NotificationsDashboardContributor {

    /** @var Plugin */
    private Plugin $plugin;

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

    public function register(): void {
        add_action( 'admin_init', [ $this, 'contribute' ], 20 );
    }

    public function contribute(): void {
        if ( ! \UltraguardPro\core\Admin::is_ultraguard_dashboard() ) { return; }


        $db      = $this->plugin->modules->get_db_tables();
        $log_t   = $db ? $db->get_table( 'notification_log' )   : null;
        $rules_t = $db ? $db->get_table( 'notification_rules' ) : null;

        $today        = gmdate( 'Y-m-d 00:00:00' );
        $sent_today   = $log_t   ? $log_t->count( [ 'status' => 'sent', 'sent_at >=' => $today ] ) : 0;
        $failed       = $log_t   ? $log_t->count( [ 'status' => 'failed' ] )                       : 0;
        $active_rules = $rules_t ? $rules_t->count( [ 'is_active' => 1 ] )                         : 0;

        // ── Stat: Notifications sent today ────────────────────────────────────
        do_action( 'ultraguard_dashboard_register_stat', [
            'id'             => 'stat-notifications-sent',
            'title'          => __( 'Alerts Sent Today', 'ultraguard-security-suite' ),
            'value'          => $sent_today,
            'format'         => 'number',
            'icon'           => 'dashicons-bell',
            'class'          => 'info',
            'animated'       => true,
            'priority'       => 60,
            'span'           => 2,
            'hide_when_empty'=> true,
        ] );

        // ── Status item ────────────────────────────────────────────────────────
        do_action( 'ultraguard_dashboard_register_status', [
            'id'       => 'status-notifications',
            'icon'     => 'dashicons-bell',
            'text'     => __( 'Notifications', 'ultraguard-security-suite' ),
            'badge'    => [
                'text'       => $active_rules > 0
                    /* translators: %d: number */
                    ? sprintf( _n( '%d rule', '%d rules', $active_rules, 'ultraguard-security-suite' ), $active_rules )
                    : __( 'No Rules', 'ultraguard-security-suite' ),
                'badge_type' => $active_rules > 0 ? 'success' : 'secondary',
            ],
            'actions'  => [
                [
                    'type'         => 'button',
                    'size'         => 'small',
                    'text'         => __( 'Manage', 'ultraguard-security-suite' ),
                    'button_style' => 'secondary',
                    'url'          => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=notifications' ),
                ],
            ],
            'priority' => 60,
        ] );

        // ── Quick tile ─────────────────────────────────────────────────────────
        do_action( 'ultraguard_dashboard_register_tile', [
            'title'    => __( 'Notifications', 'ultraguard-security-suite' ),
            'icon'     => 'dashicons-bell',
            'content'  => $active_rules > 0
                /* translators: %d: number */
                ? sprintf( _n( '%d active rule', '%d active rules', $active_rules, 'ultraguard-security-suite' ), $active_rules )
                : __( 'No rules', 'ultraguard-security-suite' ),
            'badge'    => $failed > 0
                ? [ 'text' => $failed . ' ' . __( 'failed', 'ultraguard-security-suite' ), 'badge_type' => 'danger' ]
                : [],
            'footer'   => [
                'type'         => 'button',
                'text'         => __( 'Open', 'ultraguard-security-suite' ),
                'button_style' => 'secondary',
                'url'          => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=notifications' ),
            ],
            'priority' => 60,
        ] );

        // ── Recommendations ────────────────────────────────────────────────────
        if ( $failed > 0 ) {
            do_action( 'ultraguard_dashboard_register_recommendation', [
                'title'    => sprintf(
                    /* translators: %d: number of failed notification deliveries. */
                    _n(
                        '%d notification failed to deliver — check email settings or channel configuration.',
                        '%d notifications failed to deliver — check email settings or channel configuration.',
                        $failed,
                        'ultraguard-security-suite'
                    ),
                    $failed
                ),
                'priority' => 'medium',
                'url'      => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=notifications-log' ),
            ] );
        }

        if ( $active_rules === 0 ) {
            do_action( 'ultraguard_dashboard_register_recommendation', [
                'title'    => __( 'No active notification rules — you will not receive alerts about security events.', 'ultraguard-security-suite' ),
                'priority' => 'low',
                'url'      => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=notifications-rules' ),
            ] );
        }
    }
}
