<?php
/**
 * LoginLimiterDashboard — Login Protection overview page.
 *
 * Displays:
 *   • 6 stat-cards  (failed today, success today, active lockouts,
 *                    active sessions, blocked IPs, max attempts setting)
 *   • 24h failed-login bar chart + protection status sidebar
 *   • Active lockout summary card + quick actions
 *
 * @package UltraguardPro\modules\loginlimiter\pages
 * @since   6.5.0
 */

namespace UltraguardPro\modules\loginlimiter\pages;

use UltraguardPro\Plugin;
use UltraguardPro\modules\core\BasePage;

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

class LoginLimiterDashboard extends BasePage {

    public function __construct( Plugin $plugin ) {
        parent::__construct( $plugin );
        $this->ajax_nonce_action = 'ultraguard_admin_nonce';
    }

    public function get_scheme( $context ): array {
        $db   = $this->plugin->modules->get_db_tables();
        $att  = $db ? $db->get_table( 'login_attempts' )         : null;
        $lo   = $db ? $db->get_table( 'login_limiter_lockouts' ) : null;
        $sess = $db ? $db->get_table( 'user_sessions' )          : null;
        $bl   = $db ? $db->get_table( 'll_blocked_ips' )         : null;

        $today = gmdate( 'Y-m-d 00:00:00' );

        $failed_today   = $att  ? $att->count( [ 'status' => 'failed',  'created_at >=' => $today ] ) : 0;
        $success_today  = $att  ? $att->count( [ 'status' => 'success', 'created_at >=' => $today ] ) : 0;
        $active_lockouts= $lo   ? $lo->count( [ 'is_active' => 1 ] )                                   : 0;
        $active_sessions= $sess ? $sess->count()                                                        : 0;
        $blocked_ips    = $bl   ? $bl->count(  [ 'is_active' => 1 ] )                                  : 0;

        $max_attempts = (int) $this->get_setting( 'loginlimiter_max_attempts', 5 );
        $lockout_mins = (int) $this->get_setting( 'loginlimiter_lockout_duration', 30 );
        $enabled      = (bool)$this->get_setting( 'loginlimiter_enabled', true );
        $two_fa       = (bool)$this->get_setting( 'loginlimiter_two_factor_enabled', false );
        $progressive  = (bool)$this->get_setting( 'loginlimiter_progressive_lockout', false );

        $chart = $att
            ? $att->get_chart_data( 'created_at', 'id', 'hour', [ 'status' => 'failed' ], 24 )
            : [ 'labels' => [], 'data' => [] ];

        $nonce = $this->create_nonce( 'ultraguard_admin_nonce' );

        return apply_filters( 'ultraguard_loginlimiter_dashboard_scheme', [
            'type'      => 'page',
            'id'        => 'ultraguard-login-limiter-dashboard',
            'page_slug' => 'login-limiter',
            'class'     => 'ultraguard-login-limiter-dashboard-page',

            'header' => [
                'title'    => __( 'Login Protection Dashboard', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Brute-force protection, lockout monitoring and session management.', 'ultraguard-security-suite' ),
                'badge'    => [
                    'text'       => $enabled ? __( 'Protection Active', 'ultraguard-security-suite' ) : __( 'Protection Disabled', 'ultraguard-security-suite' ),
                    'badge_type' => $enabled ? 'success' : 'danger',
                ],
                'actions' => [
                    $this->create_ajax_button( __( 'Refresh Stats',   'ultraguard-security-suite' ), 'ultraguard_ll_refresh_stats', [], 'primary',   'dashicons-update'     ),
                    $this->create_url_button(  __( 'Login Attempts',  'ultraguard-security-suite' ), admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=login-attempts' ), 'secondary', 'dashicons-list-view'  ),
                    $this->create_url_button(  __( 'Manage IPs',      'ultraguard-security-suite' ), admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=ip-management' ), 'secondary', 'dashicons-networking' ),
                    $this->create_url_button(  __( 'Lockouts',        'ultraguard-security-suite' ), admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=lockouts' ), 'secondary', 'dashicons-dismiss'    ),
                ],
            ],

            'data' => [
                // Stat row
                [
                    'type'    => 'grid',
                    'columns' => 12,
                    'gap'     => '16px',
                    'items'   => [
                        [ 'span' => 3, 'component' => [ 'type' => 'stat-card', 'title' => __( 'Failed Today',    'ultraguard-security-suite' ), 'value' => $failed_today,    'format' => 'number', 'icon' => 'dashicons-dismiss',     'class' => $failed_today    > 10 ? 'danger'  : ( $failed_today > 0 ? 'warning' : 'success' ) ] ],
                        [ 'span' => 3, 'component' => [ 'type' => 'stat-card', 'title' => __( 'Success Today',  'ultraguard-security-suite' ), 'value' => $success_today,   'format' => 'number', 'icon' => 'dashicons-yes-alt',     'class' => 'success' ] ],
                        [ 'span' => 3, 'component' => [ 'type' => 'stat-card', 'title' => __( 'Active Lockouts','ultraguard-security-suite' ), 'value' => $active_lockouts, 'format' => 'number', 'icon' => 'dashicons-lock',        'class' => $active_lockouts > 0 ? 'warning' : 'success' ] ],
                        [ 'span' => 3, 'component' => [ 'type' => 'stat-card', 'title' => __( 'Active Sessions','ultraguard-security-suite' ), 'value' => $active_sessions, 'format' => 'number', 'icon' => 'dashicons-admin-users', 'class' => 'info' ] ],
                        [ 'span' => 3, 'component' => [ 'type' => 'stat-card', 'title' => __( 'Blocked IPs',    'ultraguard-security-suite' ), 'value' => $blocked_ips,     'format' => 'number', 'icon' => 'dashicons-shield-alt',  'class' => $blocked_ips > 0 ? 'warning' : 'success' ] ],
                        [ 'span' => 3, 'component' => [ 'type' => 'stat-card', 'title' => __( 'Max Attempts',   'ultraguard-security-suite' ), 'value' => $max_attempts,    'format' => 'number', 'icon' => 'dashicons-warning',     'class' => 'info' ] ],
                    ],
                ],

                // Chart + Status sidebar
                [
                    'type'    => 'grid',
                    'columns' => 12,
                    'gap'     => '20px',
                    'items'   => [
                        [
                            'span'      => 8,
                            'component' => [
                                'type'    => 'card',
                                'title'   => __( 'Failed Logins — Last 24 Hours', 'ultraguard-security-suite' ),
                                'icon'    => 'dashicons-chart-bar',
                                'actions' => [
                                    [
                                        'type'         => 'button',
                                        'text'         => __( '6h', 'ultraguard-security-suite' ),
                                        'button_style' => 'secondary',
                                        'ajax'         => [ 'action' => 'ultraguard_ll_update_chart_period', 'nonce' => $nonce, 'data' => [ 'period' => 6 ] ],
                                    ],
                                    [
                                        'type'         => 'button',
                                        'text'         => __( '24h', 'ultraguard-security-suite' ),
                                        'button_style' => 'primary',
                                        'ajax'         => [ 'action' => 'ultraguard_ll_update_chart_period', 'nonce' => $nonce, 'data' => [ 'period' => 24 ] ],
                                    ],
                                    [
                                        'type'         => 'button',
                                        'text'         => __( '7d', 'ultraguard-security-suite' ),
                                        'button_style' => 'secondary',
                                        'ajax'         => [ 'action' => 'ultraguard_ll_update_chart_period', 'nonce' => $nonce, 'data' => [ 'period' => 168 ] ],
                                    ],
                                ],
                                'data'    => [
                                    [
                                        'type'       => 'chart',
                                        'chart_type' => 'bar',
                                        'height'     => '240px',
                                        'labels'     => $chart['labels'],
                                        'datasets'   => [
                                            [
                                                'label'           => __( 'Failed Logins', 'ultraguard-security-suite' ),
                                                'data'            => $chart['data'],
                                                'backgroundColor' => 'rgba(239,68,68,0.6)',
                                                'borderColor'     => '#ef4444',
                                                'borderWidth'     => 1,
                                            ],
                                        ],
                                        'security_theme' => true,
                                    ],
                                ],
                            ],
                        ],
                        [
                            'span'      => 4,
                            'component' => [
                                'type'  => 'card',
                                'title' => __( 'Protection Status', 'ultraguard-security-suite' ),
                                'icon'  => 'dashicons-shield',
                                'badge' => [
                                    'text'       => $enabled ? __( 'Active', 'ultraguard-security-suite' ) : __( 'Disabled', 'ultraguard-security-suite' ),
                                    'badge_type' => $enabled ? 'success' : 'danger',
                                ],
                                'data'   => [
                                    [
                                        'type'  => 'list',
                                        'items' => [
                                            [ 'icon' => 'dashicons-lock',      'text' => __( 'Login Limiting',   'ultraguard-security-suite' ), 'badge' => [ 'text' => $enabled     ? __( 'Active',   'ultraguard-security-suite' ) : __( 'Off', 'ultraguard-security-suite' ), 'badge_type' => $enabled     ? 'success' : 'danger'    ] ],
                                            [ 'icon' => 'dashicons-warning',   'text' => __( 'Max Attempts',     'ultraguard-security-suite' ), 'badge' => [ 'text' => $max_attempts . ' ' . __( 'attempts', 'ultraguard-security-suite' ),                         'badge_type' => 'info'                              ] ],
                                            [ 'icon' => 'dashicons-clock',     'text' => __( 'Lockout Duration', 'ultraguard-security-suite' ), 'badge' => [ 'text' => $lockout_mins . ' ' . __( 'min', 'ultraguard-security-suite' ),                               'badge_type' => 'info'                              ] ],
                                            [ 'icon' => 'dashicons-smartphone','text' => __( '2FA',              'ultraguard-security-suite' ), 'badge' => [ 'text' => $two_fa      ? __( 'Enabled',  'ultraguard-security-suite' ) : __( 'Off', 'ultraguard-security-suite' ), 'badge_type' => $two_fa      ? 'success' : 'secondary' ] ],
                                            [ 'icon' => 'dashicons-chart-line','text' => __( 'Progressive',      'ultraguard-security-suite' ), 'badge' => [ 'text' => $progressive  ? __( 'Enabled',  'ultraguard-security-suite' ) : __( 'Off', 'ultraguard-security-suite' ), 'badge_type' => $progressive  ? 'success' : 'secondary' ] ],
                                            [ 'icon' => 'dashicons-dismiss',   'text' => __( 'Active Lockouts',  'ultraguard-security-suite' ), 'badge' => [ 'text' => $active_lockouts,                                                                   'badge_type' => $active_lockouts > 0 ? 'danger' : 'success' ] ],
                                        ],
                                    ],
                                ],
                                'footer' => $this->create_url_button( __( 'Settings', 'ultraguard-security-suite' ), admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=login-limiter-settings' ), 'secondary', 'dashicons-admin-settings' ),
                            ],
                        ],
                    ],
                ],

                // Lockout summary + Quick actions
                [
                    'type'    => 'grid',
                    'columns' => 12,
                    'gap'     => '20px',
                    'items'   => [
                        [
                            'span'      => 8,
                            'component' => [
                                'type'    => 'card',
                                'title'   => __( 'Active Lockouts', 'ultraguard-security-suite' ),
                                'icon'    => 'dashicons-lock',
                                'badge'   => [
                                    'text'       => $active_lockouts,
                                    'badge_type' => $active_lockouts > 0 ? 'danger' : 'success',
                                ],
                                'actions' => [
                                    $this->create_url_button( __( 'View All', 'ultraguard-security-suite' ), admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=lockouts' ), 'secondary', 'dashicons-external' ),
                                ],
                                'data'    => [
                                    [
                                        'type'    => 'paragraph',
                                        'content' => $active_lockouts > 0
                                            ? sprintf(
                                                /* translators: 1: number of locked out IP addresses, 2: URL to the lockouts management page. */
                                                _n(
                                                    '%1$d IP address is currently locked out. <a href="%2$s">Manage lockouts</a>.',
                                                    '%1$d IP addresses are currently locked out. <a href="%2$s">Manage lockouts</a>.',
                                                    $active_lockouts,
                                                    'ultraguard-security-suite'
                                                ),
                                                $active_lockouts,
                                                esc_url( admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=lockouts' ) )
                                            )
                                            : __( 'No IP addresses are currently locked out.', 'ultraguard-security-suite' ),
                                    ],
                                ],
                                'footer'  => $active_lockouts > 0
                                    ? $this->create_ajax_button(
                                        __( 'Clear All Lockouts', 'ultraguard-security-suite' ),
                                        'ultraguard_ll_unlock_all',
                                        [], 'warning', 'dashicons-unlock',
                                        __( 'Release all active lockouts?', 'ultraguard-security-suite' )
                                    )
                                    : null,
                            ],
                        ],
                        [
                            'span'      => 4,
                            'component' => [
                                'type'  => 'card',
                                'title' => __( 'Quick Actions', 'ultraguard-security-suite' ),
                                'icon'  => 'dashicons-admin-tools',
                                'data'  => [
                                    [
                                        'type'      => 'button-group',
                                        'alignment' => 'center',
                                        'buttons'   => [
                                            $this->create_url_button(  __( 'Login Attempts',  'ultraguard-security-suite' ), admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=login-attempts' ), 'primary',   'dashicons-list-view'  ),
                                            $this->create_url_button(  __( 'Manage IPs',      'ultraguard-security-suite' ), admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=ip-management' ), 'secondary', 'dashicons-networking' ),
                                            $this->create_url_button(  __( 'Lockouts',        'ultraguard-security-suite' ), admin_url( 'admin.php?page=ultraguard-security-suite-dashboard&ug_page=lockouts' ), 'secondary', 'dashicons-dismiss'    ),
                                            $this->create_ajax_button(
                                                __( 'Clear Attempts', 'ultraguard-security-suite' ),
                                                'ultraguard_ll_clear_attempts', [], 'secondary', 'dashicons-trash',
                                                __( 'Clear all login attempt logs?', 'ultraguard-security-suite' )
                                            ),
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],

            'form' => [ 'enable' => false ],
        ], $context );
    }
}
