<?php
/**
 * AntivirusPage — Admin page scheme for the Antivirus module.
 *
 * @package UltraguardPro\modules\antivirus\pages
 */

namespace UltraguardPro\modules\antivirus\pages;

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

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

class AntivirusPage extends BasePage {

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

    public function get_scheme( $context ): array {
        $active_scan = get_option( 'ultraguard_active_scan', [] );
        $is_running  = ( $active_scan['status'] ?? '' ) === 'running';

        return apply_filters( 'ultraguard_antivirus_page_scheme', [
            'type'      => 'page',
            'id'        => 'ultraguard-antivirus',
            'page_slug' => 'antivirus',
            'class'     => 'ultraguard-antivirus-page',
            'header'    => [
                'id'       => 'antivirus-header',
                'title'    => __( 'Antivirus Scanner', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Scan for malware, viruses and vulnerabilities', 'ultraguard-security-suite' ),
                'actions'  => [
                    $this->create_ajax_button(
                        $is_running
                            ? __( 'Scanning…', 'ultraguard-security-suite' )
                            : __( 'Start Full Scan', 'ultraguard-security-suite' ),
                        'ultraguard_antivirus_start_scan',
                        [ 'scan_type' => 'full' ],
                        'primary',
                        'dashicons-search'
                    ),
                ],
            ],
            'data' => [
                [
                    'type' => 'tabs',
                    'id'   => 'antivirus-tabs',
                    'tabs' => [
                        [
                            'id'       => 'scan-results',
                            'title'    => __( 'Scan Results', 'ultraguard-security-suite' ),
                            'icon'     => 'dashicons-search',
                            'data'    => [ $this->scan_results_widget() ],
                        ],
                        [
                            'id'       => 'quarantine',
                            'title'    => __( 'Quarantine', 'ultraguard-security-suite' ),
                            'icon'     => 'dashicons-lock',
                            'data'    => [ $this->quarantine_widget() ],
                        ],
                        [
                            'id'       => 'vulnerabilities',
                            'title'    => __( 'Vulnerabilities', 'ultraguard-security-suite' ),
                            'icon'     => 'dashicons-warning',
                            'data'    => [ $this->vulnerabilities_widget() ],
                        ],
                    ],
                ],
            ],
            'form' => [ 'enable' => false ],
        ], $context );
    }

    private function scan_results_widget(): array {
        return [
            'type'    => 'data-table',
            'id'      => 'scan-results-table',
            'title'   => __( 'Scan Results', 'ultraguard-security-suite' ),
            'source'  => [
                'ajax'  => 'ultraguard_antivirus_get_results',
                'nonce' => $this->create_nonce( 'ultraguard_antivirus_get_results' ),
            ],
            'columns' => [
                'file_path'    => [ 'title' => __( 'File',     'ultraguard-security-suite' ), 'sortable' => true ],
                'threat_type'  => [ 'title' => __( 'Threat',   'ultraguard-security-suite' ) ],
                'threat_level' => [ 'title' => __( 'Level',    'ultraguard-security-suite' ) ],
                'status'       => [ 'title' => __( 'Status',   'ultraguard-security-suite' ) ],
                'created_at'   => [ 'title' => __( 'Detected', 'ultraguard-security-suite' ), 'sortable' => true ],
            ],
            'row_actions' => [
                [
                    'label'   => __( 'Quarantine', 'ultraguard-security-suite' ),
                    'action'  => 'ultraguard_antivirus_quarantine_file',
                    'nonce'   => $this->create_nonce( 'ultraguard_antivirus_quarantine_file' ),
                    'confirm' => __( 'Move this file to quarantine?', 'ultraguard-security-suite' ),
                    'style'   => 'warning',
                ],
                [
                    'label'  => __( 'False Positive', 'ultraguard-security-suite' ),
                    'action' => 'ultraguard_antivirus_mark_false_positive',
                    'nonce'  => $this->create_nonce( 'ultraguard_antivirus_mark_false_positive' ),
                ],
            ],
        ];
    }

    private function quarantine_widget(): array {
        return [
            'type'    => 'data-table',
            'id'      => 'quarantine-table',
            'title'   => __( 'Quarantined Files', 'ultraguard-security-suite' ),
            'source'  => [
                'ajax'  => 'ultraguard_antivirus_get_quarantine',
                'nonce' => $this->create_nonce( 'ultraguard_antivirus_get_quarantine' ),
            ],
            'columns' => [
                'original_path' => [ 'title' => __( 'Original Path', 'ultraguard-security-suite' ), 'sortable' => true ],
                'threat_type'   => [ 'title' => __( 'Threat',        'ultraguard-security-suite' ) ],
                'threat_level'  => [ 'title' => __( 'Level',         'ultraguard-security-suite' ) ],
                'status'        => [ 'title' => __( 'Status',        'ultraguard-security-suite' ) ],
                'created_at'    => [ 'title' => __( 'Quarantined',   'ultraguard-security-suite' ), 'sortable' => true ],
            ],
            'row_actions' => [
                [
                    'label'   => __( 'Restore', 'ultraguard-security-suite' ),
                    'action'  => 'ultraguard_antivirus_restore_file',
                    'nonce'   => $this->create_nonce( 'ultraguard_antivirus_restore_file' ),
                    'confirm' => __( 'Restore this file to its original location?', 'ultraguard-security-suite' ),
                ],
                [
                    'label'   => __( 'Delete', 'ultraguard-security-suite' ),
                    'action'  => 'ultraguard_antivirus_delete_quarantine',
                    'nonce'   => $this->create_nonce( 'ultraguard_antivirus_delete_quarantine' ),
                    'confirm' => __( 'Permanently delete this file? This cannot be undone.', 'ultraguard-security-suite' ),
                    'style'   => 'danger',
                ],
            ],
        ];
    }

    private function vulnerabilities_widget(): array {
        return [
            'type'    => 'data-table',
            'id'      => 'vulnerabilities-table',
            'title'   => __( 'Vulnerabilities', 'ultraguard-security-suite' ),
            'source'  => [
                'ajax'  => 'ultraguard_antivirus_get_vulnerabilities',
                'nonce' => $this->create_nonce( 'ultraguard_antivirus_get_vulnerabilities' ),
            ],
            'columns' => [
                'component_slug'     => [ 'title' => __( 'Component',     'ultraguard-security-suite' ), 'sortable' => true ],
                'component_type'     => [ 'title' => __( 'Type',          'ultraguard-security-suite' ) ],
                'vulnerability_type' => [ 'title' => __( 'Vulnerability', 'ultraguard-security-suite' ) ],
                'severity'           => [ 'title' => __( 'Severity',      'ultraguard-security-suite' ), 'sortable' => true ],
                'cve_id'             => [ 'title' => __( 'CVE',           'ultraguard-security-suite' ) ],
                'status'             => [ 'title' => __( 'Status',        'ultraguard-security-suite' ) ],
            ],
        ];
    }
}
