<?php

namespace UltraguardPro\modules\uptime_monitor;

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

use UltraguardPro\Plugin;

class UptimeMonitorDashboardContributor {

    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; }

        $engine = new UptimeCheckEngine( $this->plugin );
        $ssl    = $engine->get_ssl_status();

        do_action( 'ultraguard_dashboard_register_stat', [
            'id'       => 'stat-uptime-24h',
            'title'    => __( 'Uptime (24h)', 'ultraguard-security-suite' ),
            'value'    => round( $engine->get_uptime_percentage( 24 ), 2 ),
            'format'   => 'percent',
            'icon'     => 'dashicons-clock',
            'class'    => 'up' === $engine->get_current_status() ? 'success' : 'warning',
            'animated' => true,
            'priority' => 120,
            'hide_when_empty' => true,
            'span'     => 3,
        ] );

        do_action( 'ultraguard_dashboard_register_status', [
            'id'      => 'status-uptime-monitor',
            'icon'    => 'dashicons-clock',
            'text'    => __( 'Uptime Monitor', 'ultraguard-security-suite' ),
            'badge'   => [
                'text'       => strtoupper( $engine->get_current_status() ),
                'badge_type' => 'up' === $engine->get_current_status() ? 'success' : 'warning',
            ],
            'actions' => [
                [
                    'type' => 'button',
                    'button_style' => 'secondary',
                    'size'  => 'small',
                    'text' => __( 'Manage', 'ultraguard-security-suite' ),
                    'url'  => admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=uptime-monitor' ),
                ],
            ],
        ] );

        if ( ! empty( $ssl['days_remaining'] ) && (int) $ssl['days_remaining'] < 30 ) {
            do_action( 'ultraguard_dashboard_register_stat', [
                'id'       => 'stat-ssl-days-remaining',
                'title'    => __( 'SSL Days Left', 'ultraguard-security-suite' ),
                'value'    => (int) $ssl['days_remaining'],
                'format'   => 'number',
                'icon'     => 'dashicons-lock',
                'class'    => (int) $ssl['days_remaining'] < 7 ? 'danger' : 'warning',
                'animated' => true,
                'hide_when_empty' => true,
                'priority' => 121,
                'span'     => 3,
            ] );
        }
    }
}
