<?php
defined('ABSPATH') or die('Direct access not allowed');

class CodeEditor_Editor {
    public function __construct() {
        add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']);
        add_action('wp_ajax_code_editor_save_file', [$this, 'save_file_content']);
    }

    public function enqueue_assets() {
        wp_enqueue_script(
            'code-editor-js',
            plugins_url('assets/js/code-editor.js', dirname(__FILE__)),
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_script(
            'code-search-js',
            plugins_url('assets/js/code-search.js', dirname(__FILE__)),
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_style(
            'code-editor-css',
            plugins_url('assets/css/code-editor.css', dirname(__FILE__)),
            [],
            '1.0.0'
        );

        wp_enqueue_style(
            'code-search-css',
            plugins_url('assets/css/code-search.css', dirname(__FILE__)),
            [],
            '1.0.0'
        );

        wp_enqueue_script(
            'webdav-js',
            plugins_url('assets/js/webdav.js', dirname(__FILE__)),
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_style(
            'webdav-css',
            plugins_url('assets/css/webdav.css', dirname(__FILE__)),
            [],
            '1.0.0'
        );

        wp_enqueue_script(
            'zip-archive-js',
            plugins_url('assets/js/zip-archive.js', dirname(__FILE__)),
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_style(
            'zip-archive-css',
            plugins_url('assets/css/zip-archive.css', dirname(__FILE__)),
            [],
            '1.0.0'
        );

        wp_enqueue_script(
            'terminal-js',
            plugins_url('assets/js/terminal.js', dirname(__FILE__)),
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_style(
            'terminal-css',
            plugins_url('assets/css/terminal.css', dirname(__FILE__)),
            [],
            '1.0.0'
        );

        wp_localize_script('code-editor-js', 'codeEditorVars', [
            'ajaxurl' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('code_editor_nonce')
        ]);
    }

    public function save_file_content() {
        check_ajax_referer('code_editor_nonce', 'security');
        
        if (!current_user_can('manage_options')) {
            wp_send_json_error('Unauthorized', 403);
        }

        $path = isset($_POST['path']) ? sanitize_text_field($_POST['path']) : '';
        $content = isset($_POST['content']) ? $_POST['content'] : '';
        
        $base_path = ABSPATH;
        $full_path = realpath($base_path . $path);
        
        if (strpos($full_path, $base_path) !== 0 || !is_file($full_path)) {
            wp_send_json_error('Invalid file', 400);
        }

        if (file_put_contents($full_path, $content) !== false) {
            wp_send_json_success();
        } else {
            wp_send_json_error('Save failed', 500);
        }
    }
}

new CodeEditor_Editor();