<?php
/**
 * Dashboard Widget Module
 *
 * @package     WPUltimateSecurity\UltimateSecurity\Modules\Monitoring\Dashboard
 * @since       1.0.0
 */

declare(strict_types=1);

namespace WPUltimateSecurity\UltimateSecurity\Modules\Monitoring\Dashboard;

use WPUltimateSecurity\UltimateSecurity\Modules\Base\AbstractModule;
use WPUltimateSecurity\UltimateSecurity\Presentation\API\Controllers\SecurityController;
use WPUltimateSecurity\UltimateSecurity\Presentation\API\Controllers\SystemController;
use WPUltimateSecurity\UltimateSecurity\Presentation\API\Controllers\UpdateController;
use WP_REST_Request;
use WP_REST_Response;

/**
 * Dashboard Widget Module
 *
 * @since 1.0.0
 * @since 2.0.0 Migrated to extend AbstractModule.
 */
class DashboardWidget extends AbstractModule
{
    public const MODULE_ID = 'monitoring.dashboard_widget';
    protected const VERSION = '2.0.0';

    public static function getId(): string
    {
        return self::MODULE_ID;
    }

    public static function getName(): string
    {
        return __('Dashboard Widget', 'ultimate-security');
    }

    public static function getDomain(): string
    {
        return 'monitoring';
    }

    public static function boot(): void
    {
        self::action('wp_dashboard_setup', [self::class, 'ultimate_security_add_dashboard_widgets']);
        self::action('admin_enqueue_scripts', [self::class, 'enqueue_styles']);
    }

    public static function ultimate_security_add_dashboard_widgets(): void
    {
        wp_add_dashboard_widget(
            'ultimate_security_dashboard_widget',
            esc_html__('Ultimate Security at a glance', 'ultimate-security'),
            [self::class, 'ultimate_security_dashboard_widget_render']
        );

        global $wp_meta_boxes;

        $default_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
        $example_widget_backup = array('ultimate_security_dashboard_widget' => $default_dashboard['ultimate_security_dashboard_widget']);
        unset($default_dashboard['ultimate_security_dashboard_widget']);

        $sorted_dashboard = array_merge($example_widget_backup, $default_dashboard);
        $wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
    }

    public static function enqueue_styles(string $hook): void
    {
        if ('index.php' !== $hook) {
            return;
        }

        wp_enqueue_style(
            'ultimate-security-dashboard-widget',
            ULTIMATE_SECURITY_URL . 'assets/admin/css/dashboard-widget.css',
            [],
            ULTIMATE_SECURITY_VERSION
        );
    }

    /**
     * @return array<string, int>
     */
    public static function get_widget_data(): array
    {
        $login_response = SecurityController::get_login_failed_logs(new WP_REST_Request());
        $login_stats = $login_response instanceof WP_REST_Response
            ? $login_response->get_data()
            : (array) $login_response;

        $site_health_response = SystemController::get_site_health_data(new WP_REST_Request());
        $site_health_report = $site_health_response instanceof WP_REST_Response ? $site_health_response->get_data() : (array) $site_health_response;

        $plugins_response = UpdateController::get_update_plugin_info(new WP_REST_Request());
        $updated_plugins_info = $plugins_response instanceof WP_REST_Response ? $plugins_response->get_data() : (array) $plugins_response;

        return [
            'total_failed_logins' => $login_stats['data']['total_failed'] ?? 0,
            'total_issues' => $site_health_report['data']['total_issues'] ?? 0,
            'critical_issues_count' => $site_health_report['data']['critical_count'] ?? 0,
            'outdated_plugins_count' => $updated_plugins_info['data']['count'] ?? 0,
        ];
    }

    public static function ultimate_security_dashboard_widget_render(): void
    {
        $widget_data = self::get_widget_data();
        ?>
        <div class="ultimate-security-dashboard-widget">
            <div class="ultimate-security-dashboard-header">
                <div class="ultimate-security-dashboard-header-left">
                    <div class="ultimate-security-plugin-icon">
                        <img src="<?php echo esc_url(ULTIMATE_SECURITY_URL . 'assets/admin/images/widget-logo.svg'); ?>"
                            alt="plugin logo">
                    </div>
                    <div class="ultimate-security-plugin-info">
                        <p style="font-size: 14px; margin: 0;">
                            <span><?php echo esc_html(ULTIMATE_SECURITY_NAME); ?></span>
                            <span>v<?php echo esc_html(ULTIMATE_SECURITY_VERSION); ?></span>
                        </p>
                    </div>
                </div>
                <a href="<?php echo esc_url(admin_url('admin.php?page=ultimate-security')); ?>" class="button">
                    <?php echo esc_html__('Go to Dashboard', 'ultimate-security'); ?>
                </a>
            </div>

            <div class="ultimate-security-stats-grid">
                <div class="ultimate-security-stat-card">
                    <div class="ultimate-security-stat-icon success">
                        <img src="<?php echo esc_url(ULTIMATE_SECURITY_URL . 'assets/admin/images/issues_found.svg'); ?>"
                            alt="status logo">
                    </div>
                    <div class="ultimate-security-stat-number"><?php echo esc_html((string) $widget_data['total_issues']); ?></div>
                    <div class="ultimate-security-stat-label"><?php echo esc_html__('Issues Found', 'ultimate-security'); ?></div>
                </div>

                <div class="ultimate-security-stat-card">
                    <div class="ultimate-security-stat-icon danger">
                        <img src="<?php echo esc_url(ULTIMATE_SECURITY_URL . 'assets/admin/images/critical_threats.svg'); ?>"
                            alt="status logo">
                    </div>
                    <div class="ultimate-security-stat-number"><?php echo esc_html((string) $widget_data['critical_issues_count']); ?>
                    </div>
                    <div class="ultimate-security-stat-label"><?php echo esc_html__('Critical Threats', 'ultimate-security'); ?>
                    </div>
                </div>

                <div class="ultimate-security-stat-card">
                    <div class="ultimate-security-stat-icon warning">
                        <img src="<?php echo esc_url(ULTIMATE_SECURITY_URL . 'assets/admin/images/outdated_plugins.svg'); ?>"
                            alt="status logo">
                    </div>
                    <div class="ultimate-security-stat-number"><?php echo esc_html((string) $widget_data['outdated_plugins_count']); ?>
                    </div>
                    <div class="ultimate-security-stat-label"><?php echo esc_html__('Outdated Plugins', 'ultimate-security'); ?>
                    </div>
                </div>

                <div class="ultimate-security-stat-card">
                    <div class="ultimate-security-stat-icon info">
                        <img src="<?php echo esc_url(ULTIMATE_SECURITY_URL . 'assets/admin/images/failed_logins.svg'); ?>"
                            alt="status logo">
                    </div>
                    <div class="ultimate-security-stat-number"><?php echo esc_html((string) $widget_data['total_failed_logins']); ?>
                    </div>
                    <div class="ultimate-security-stat-label"><?php echo esc_html__('Failed Logins', 'ultimate-security'); ?></div>
                </div>
            </div>

            <div class="ultimate-security-dashboard-footer">
                <a href="https://wpultimatesecurity.com/" class="ultimate-security-footer-link" target="_blank"
                    style="font-size: 13px;">
                    <?php echo esc_html__('About', 'ultimate-security'); ?>
                    <img src="<?php echo esc_url(ULTIMATE_SECURITY_URL . 'assets/admin/images/widget-blue-goto.svg'); ?>"
                        alt="goto">
                </a>
            </div>
        </div>
        <?php
    }
}
