<?php
/**
 * FilecheckerQuarantine — Quarantined files management subpage.
 *
 * Reads from fc_quarantine table (dedicated audit trail) rather than
 * the file_integrity table for a clean separation of concerns.
 *
 * Displays:
 *   • 3 stat-cards   (active, restored, deleted quarantine counts)
 *   • Quarantined files data-table with Restore / Delete row actions
 *   • Bulk restore-all / delete-all header actions
 *
 * @package UltraguardPro\modules\filechecker\pages
 * @since   6.5.0
 */

namespace UltraguardPro\modules\filechecker\pages;

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

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

class FilecheckerQuarantine 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();
        $qt  = $db ? $db->get_table( 'fc_quarantine' ) : null;

        $summary  = $qt ? $qt->get_quarantine_summary() : [ 'active' => 0, 'restored' => 0, 'deleted' => 0, 'total' => 0 ];
        $active   = (int) $summary['active'];
        $restored = (int) $summary['restored'];
        $deleted  = (int) $summary['deleted'];

        $rows  = $qt ? $qt->get_active( 200 ) : [];
        $nonce = $this->create_nonce( 'ultraguard_admin_nonce' );

        $table_data = array_map( function( $row ) use ( $nonce ) {
            $id   = (int) ( $row['id'] ?? 0 );
            $size = (int) ( $row['file_size'] ?? 0 );

            $view_modal = [
                'id'      => 'fc-view-quar-' . $id,
                /* translators: %d: integer */
                'title'   => sprintf( __( 'Quarantine Record #%d', 'ultraguard-security-suite' ), $id ),
                'size'    => 'medium',
                'content' => $this->modal_content( [
                    [ 'type' => 'text', 'name' => 'vfq_orig',  'label' => __( 'Original Path',    'ultraguard-security-suite' ), 'value' => esc_html( $row['original_path']   ?? '' ),                           'readonly' => true ],
                    [ 'type' => 'text', 'name' => 'vfq_qpath', 'label' => __( 'Quarantine Path',  'ultraguard-security-suite' ), 'value' => esc_html( $row['quarantine_path'] ?? '' ),                           'readonly' => true ],
                    [ 'type' => 'text', 'name' => 'vfq_rsn',   'label' => __( 'Reason',           'ultraguard-security-suite' ), 'value' => esc_html( ucfirst( $row['reason'] ?? '—' ) ),                       'readonly' => true ],
                    [ 'type' => 'text', 'name' => 'vfq_hash',  'label' => __( 'File Hash',        'ultraguard-security-suite' ), 'value' => esc_html( $row['file_hash']        ?? '—' ),                        'readonly' => true ],
                    [ 'type' => 'text', 'name' => 'vfq_size',  'label' => __( 'File Size',        'ultraguard-security-suite' ), 'value' => $size ? esc_html( size_format( $size ) ) : '—',                     'readonly' => true ],
                    [ 'type' => 'text', 'name' => 'vfq_sts',   'label' => __( 'Status',           'ultraguard-security-suite' ), 'value' => esc_html( ucfirst( $row['status']        ?? '' ) ),                  'readonly' => true ],
                    [ 'type' => 'text', 'name' => 'vfq_qat',   'label' => __( 'Quarantined',      'ultraguard-security-suite' ), 'value' => esc_html( $this->format_date_time( $row['quarantined_at'] ?? '' ) ), 'readonly' => true ],
                    [ 'type' => 'text', 'name' => 'vfq_rat',   'label' => __( 'Restored At',      'ultraguard-security-suite' ), 'value' => $row['restored_at'] ? esc_html( $this->format_date_time( $row['restored_at'] ) ) : '—', 'readonly' => true ],
                ] ),
            ];

            return [
                'id'             => 'fc-quar-row-' . $id,
                'id'           => 'ug-fc-quarantine-row-' . $id,
                'data-file-id'   => $id,
                'original_path'  => esc_html( $row['original_path']  ?? '' ),
                'reason'         => esc_html( ucfirst( $row['reason'] ?? '—' ) ),
                'file_hash'      => esc_html( substr( $row['file_hash'] ?? '', 0, 12 ) . '…' ),
                'file_size'      => $size ? esc_html( size_format( $size ) ) : '—',
                'quarantined_at' => esc_html( $this->format_date_time( $row['quarantined_at'] ?? '' ) ),
                'actions'        => $this->row_actions( [
                    [
                        'type'         => 'button',
                        'text'         => '',
                        'icon'         => 'dashicons-visibility',
                        'button_style' => 'secondary',
                        'title'        => __( 'View details', 'ultraguard-security-suite' ),
                        'modal'        => $view_modal,
                    ],
                    [
                        'type'         => 'button',
                        'text'         => '',
                        'icon'         => 'dashicons-undo',
                        'button_style' => 'secondary',
                        'title'        => __( 'Restore file', 'ultraguard-security-suite' ),
                        'ajax'         => [
                            'action'  => 'ultraguard_fc_restore_quarantined',
                            'nonce'   => $nonce,
                            'data'    => [ 'file_id' => $id ],
                            'confirm' => __( 'Restore this file to its original location?', 'ultraguard-security-suite' ),
                        ],
                    ],
                    [
                        'type'         => 'button',
                        'text'         => '',
                        'icon'         => 'dashicons-trash',
                        'button_style' => 'danger',
                        'title'        => __( 'Delete permanently', 'ultraguard-security-suite' ),
                        'ajax'         => [
                            'action'  => 'ultraguard_fc_delete_quarantined',
                            'nonce'   => $nonce,
                            'data'    => [ 'file_id' => $id ],
                            'confirm' => __( 'Permanently delete this file? This action cannot be undone.', 'ultraguard-security-suite' ),
                        ],
                    ],
                ] ),
            ];
        }, $rows );

        return apply_filters( 'ultraguard_filechecker_quarantine_scheme', [
            'type'      => 'page',
            'id'        => 'ultraguard-filechecker-quarantine',
            'page_slug' => 'filechecker-quarantine',
            'class'     => 'ultraguard-filechecker-quarantine-page',

            'header' => [
                'title'    => __( 'Quarantine', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Review, restore or permanently delete quarantined files.', 'ultraguard-security-suite' ),
                'badge'    => [
                    /* translators: %d: number */
                    'text'       => sprintf( _n( '%d Active', '%d Active', $active, 'ultraguard-security-suite' ), $active ),
                    'badge_type' => $active > 0 ? 'danger' : 'success',
                ],
                'actions' => [
                    $this->create_ajax_button(
                        __( 'Refresh', 'ultraguard-security-suite' ),
                        'ultraguard_fc_refresh_quarantine', [], 'secondary', 'dashicons-update'
                    ),
                    $this->create_ajax_button(
                        __( 'Restore All', 'ultraguard-security-suite' ),
                        'ultraguard_fc_restore_all', [], 'secondary', 'dashicons-undo',
                        __( 'Restore ALL quarantined files to their original locations?', 'ultraguard-security-suite' )
                    ),
                    $this->create_ajax_button(
                        __( 'Delete All', 'ultraguard-security-suite' ),
                        'ultraguard_fc_delete_all', [], 'danger', 'dashicons-trash',
                        __( 'Permanently delete ALL quarantined files? This cannot be undone.', 'ultraguard-security-suite' )
                    ),
                ],
            ],

            'data' => [
                // ── Contextual Pro nudge (free users only) ────────
                ...( UpgradeManager::should_show_nudge()
                    ? [ UpgradeManager::inline_hint( 'file_quarantine', __( 'Quarantine safely isolates suspicious files without deleting them.', 'ultraguard-security-suite' ) ) ]
                    : [] ),


                // Stat row
                [
                    'type'       => 'grid',
                    'columns'    => 12,
                    'gap'        => '16px',
                    'responsive' => true,
                    'items'      => [
                        [
                            'span'      => 4,
                            'responsive'=> [ 'sm' => 12, 'md' => 4 ],
                            'component' => [
                                'type'  => 'stat-card',
                                'title' => __( 'Active Quarantine', 'ultraguard-security-suite' ),
                                'value' => $active,
                                'format'=> 'number',
                                'icon'  => 'dashicons-lock',
                                'class' => $active > 0 ? 'danger' : 'success',
                            ],
                        ],
                        [
                            'span'      => 4,
                            'responsive'=> [ 'sm' => 12, 'md' => 4 ],
                            'component' => [
                                'type'  => 'stat-card',
                                'title' => __( 'Restored', 'ultraguard-security-suite' ),
                                'value' => $restored,
                                'format'=> 'number',
                                'icon'  => 'dashicons-undo',
                                'class' => 'info',
                            ],
                        ],
                        [
                            'span'      => 4,
                            'responsive'=> [ 'sm' => 12, 'md' => 4 ],
                            'component' => [
                                'type'  => 'stat-card',
                                'title' => __( 'Permanently Deleted', 'ultraguard-security-suite' ),
                                'value' => $deleted,
                                'format'=> 'number',
                                'icon'  => 'dashicons-trash',
                                'class' => 'secondary',
                            ],
                        ],
                    ],
                ],

                // Quarantined files table
                [
                    'type'               => 'data-table',
                    'id'                 => 'fc-quarantine-table',
                    'title'              => __( 'Quarantined Files', 'ultraguard-security-suite' ),
                    'columns'            => [
                        'original_path'  => [ 'title' => __( 'Original Path',   'ultraguard-security-suite' ), 'sortable' => true ],
                        'reason'         => [ 'title' => __( 'Reason',          'ultraguard-security-suite' ) ],
                        'file_hash'      => [ 'title' => __( 'Hash (Preview)',   'ultraguard-security-suite' ) ],
                        'file_size'      => [ 'title' => __( 'Size',             'ultraguard-security-suite' ) ],
                        'quarantined_at' => [ 'title' => __( 'Quarantined',      'ultraguard-security-suite' ), 'sortable' => true ],
                        'actions'        => [ 'title' => __( 'Actions',          'ultraguard-security-suite' ), 'width' => '110px' ],
                    ],
                    'data'               => $table_data,
                    'searchable'         => true,
                    'search_placeholder' => __( 'Search paths or reasons…', 'ultraguard-security-suite' ),
                    'pagination'         => [ 'enabled' => true, 'page_size' => 25 ],
                    'empty_state'        => [
                        'title'       => __( 'No active quarantined files', 'ultraguard-security-suite' ),
                        'description' => __( 'All files are clean or have been restored/deleted.', 'ultraguard-security-suite' ),
                    ],
                ],
            ],

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