<?php
/**
 * HtaccessSettings — .htaccess settings subpage.
 *
 * Tabs:
 *   Editor    — syntax highlighting, line numbers, validation, change tracking
 *   Backups   — auto-backup, backup-before-save, retention count
 *   File Info — live status list and file management actions
 *
 * @package UltraguardPro\modules\htaccess\pages
 */

namespace UltraguardPro\modules\htaccess\pages;

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

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

class HtaccessSettings extends BasePage {

    public function __construct( Plugin $plugin ) {
        parent::__construct( $plugin );
    }

    public function get_scheme( $context ): array {
        $s     = fn( $k, $d = '' ) => $this->get_setting( $k, $d );
        $nonce = $this->create_nonce( 'ultraguard_admin_nonce' );
        $path  = ABSPATH . '.htaccess';

        return apply_filters( 'ultraguard_htaccess_settings_scheme', [
            'type'      => 'page',
            'id'        => 'ultraguard-htaccess-settings',
            'page_slug' => 'htaccess-settings',
            'class'     => 'ultraguard-htaccess-settings-page',

            'header' => [
                'title'    => __( '.htaccess Settings', 'ultraguard-security-suite' ),
                'subtitle' => __( 'Configure .htaccess editor behaviour, backup preferences and file management', 'ultraguard-security-suite' ),
                'badge'    => [
                    'text'       => file_exists( $path ) ? __( 'File Found', 'ultraguard-security-suite' ) : __( 'File Missing', 'ultraguard-security-suite' ),
                    'badge_type' => file_exists( $path ) ? 'success' : 'danger',
                ],
                'actions' => [
                    $this->create_ajax_button( __( 'Test .htaccess',   'ultraguard-security-suite' ), 'ultraguard_test_htaccess',  [], 'secondary', 'dashicons-yes'        ),
                    $this->create_ajax_button( __( 'View File',         'ultraguard-security-suite' ), 'ultraguard_view_htaccess',  [], 'secondary', 'dashicons-visibility' ),
                    $this->create_ajax_button( __( 'Reset to Default', 'ultraguard-security-suite' ), 'ultraguard_reset_htaccess', [], 'danger',    'dashicons-undo',       __( 'Reset .htaccess to WordPress default? A backup will be created first.', 'ultraguard-security-suite' ) ),
                ],
            ],

            'data' => [ [
                'type' => 'tabs',
                'id'   => 'htaccess-settings-tabs',
                'tabs' => [

                    // ── Tab 1: Editor ────────────────────────────────────
                    [
                        'id'    => 'editor',
                        'title' => __( 'Editor', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-editor-code',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Editor Settings', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-editor-code',
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'htaccess_syntax_highlight', 'label' => __( 'Syntax Highlighting',  'ultraguard-security-suite' ), 'description' => __( 'Colour-code the .htaccess editor', 'ultraguard-security-suite' ),    'default' => $s( 'htaccess_syntax_highlight', true  ) ],
                                        [ 'type' => 'toggle', 'name' => 'htaccess_line_numbers',     'label' => __( 'Show Line Numbers',    'ultraguard-security-suite' ), 'description' => __( 'Display line numbers in the editor', 'ultraguard-security-suite' ),   'default' => $s( 'htaccess_line_numbers',     true  ) ],
                                        [ 'type' => 'toggle', 'name' => 'htaccess_validate_on_save', 'label' => __( 'Validate on Save',     'ultraguard-security-suite' ), 'description' => __( 'Run syntax check before writing file', 'ultraguard-security-suite' ), 'default' => $s( 'htaccess_validate_on_save', true  ) ],
                                        [ 'type' => 'toggle', 'name' => 'htaccess_track_changes',    'label' => __( 'Track Changes',        'ultraguard-security-suite' ), 'description' => __( 'Log every modification to the file', 'ultraguard-security-suite' ),   'default' => $s( 'htaccess_track_changes',    false ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 2: Backups ───────────────────────────────────
                    [
                        'id'    => 'backups',
                        'title' => __( 'Backups', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-backup',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'Backup Settings', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-backup',
                                    'data'  => [
                                        [ 'type' => 'toggle', 'name' => 'htaccess_auto_backup',    'label' => __( 'Auto Backup',           'ultraguard-security-suite' ), 'description' => __( 'Create backup automatically on modification', 'ultraguard-security-suite' ), 'default' => $s( 'htaccess_auto_backup', true ) ],
                                        [ 'type' => 'toggle', 'name' => 'htaccess_backup_on_save', 'label' => __( 'Backup Before Save',    'ultraguard-security-suite' ), 'description' => __( 'Always backup before writing changes', 'ultraguard-security-suite' ),      'default' => $s( 'htaccess_backup_on_save', true ) ],
                                        [ 'type' => 'number', 'name' => 'htaccess_backup_keep',    'label' => __( 'Backups to Keep',       'ultraguard-security-suite' ), 'min' => 1, 'max' => 100, 'description' => __( 'Maximum number of backup files to retain', 'ultraguard-security-suite' ), 'default' => $s( 'htaccess_backup_keep', 10 ) ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                    // ── Tab 3: File Info ─────────────────────────────────
                    [
                        'id'    => 'file-info',
                        'title' => __( 'File Info', 'ultraguard-security-suite' ),
                        'icon'  => 'dashicons-media-code',
                        'data'  => [ [
                            'type'    => 'grid',
                            'columns' => 12,
                            'gap'     => '20px',
                            'items'   => [
                                [ 'span' => 12, 'component' => [
                                    'type'  => 'card',
                                    'title' => __( 'File Management', 'ultraguard-security-suite' ),
                                    'icon'  => 'dashicons-media-code',
                                    'data'  => [ [
                                        'type'  => 'list',
                                        'items' => [
                                            [ 'text' => __( 'File Path',       'ultraguard-security-suite' ), 'badge' => [ 'text' => ABSPATH . '.htaccess',                                                                                        'badge_type' => 'secondary' ] ],
                                            [ 'text' => __( 'File Exists',     'ultraguard-security-suite' ), 'badge' => [ 'text' => file_exists( $path )   ? __( 'Yes', 'ultraguard-security-suite' ) : __( 'No', 'ultraguard-security-suite' ),  'badge_type' => file_exists( $path )   ? 'success' : 'danger'   ] ],
                                            [ 'text' => __( 'Writable',        'ultraguard-security-suite' ), 'badge' => [ 'text' => /* phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- direct writability check for page diagnostics. */ is_writable( $path )   ? __( 'Yes', 'ultraguard-security-suite' ) : __( 'No', 'ultraguard-security-suite' ),  'badge_type' => is_writable( $path )   ? 'success' : 'warning'  ] ],
                                            [ 'text' => __( 'Apache Version',  'ultraguard-security-suite' ), 'badge' => [ 'text' => function_exists( 'apache_get_version' ) ? apache_get_version() : __( 'Unknown', 'ultraguard-security-suite' ),            'badge_type' => 'info'      ] ],
                                        ],
                                    ] ],
                                    'footer' => [
                                        'type'      => 'button-group',
                                        'alignment' => 'center',
                                        'buttons'   => [
                                            $this->create_ajax_button( __( 'Download .htaccess', 'ultraguard-security-suite' ), 'ultraguard_download_htaccess_backup', [], 'secondary', 'dashicons-download' ),
                                            $this->create_ajax_button( __( 'Restore Backup',     'ultraguard-security-suite' ), 'ultraguard_restore_htaccess_backup',  [], 'secondary', 'dashicons-undo',     __( 'Restore the most recent backup?', 'ultraguard-security-suite' ) ),
                                        ],
                                    ],
                                ] ],
                            ],
                        ] ],
                    ],

                ],
            ] ],

            'form' => [
                'enable' => true,
                'id'     => 'htaccess-settings-form',
                'ajax'   => true,
                'action' => 'ultraguard_save_htaccess_settings',
                'nonce'  => $nonce,
                'submit' => [ 'text' => __( 'Save Settings', 'ultraguard-security-suite' ), 'button_style' => 'primary' ],
            ],
        ], $context );
    }
}
