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

class CodeEditor_GitSync {
    public function __construct() {
        add_action('wp_ajax_code_editor_git_clone', [$this, 'handle_git_clone']);
        add_action('wp_ajax_code_editor_git_pull', [$this, 'handle_git_pull']);
        add_action('wp_ajax_code_editor_git_push', [$this, 'handle_git_push']);
    }

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

        $repo_url = isset($_POST['repo_url']) ? esc_url_raw($_POST['repo_url']) : '';
        $target_dir = isset($_POST['target_dir']) ? sanitize_text_field($_POST['target_dir']) : '';
        
        try {
            $output = $this->run_git_command("clone {$repo_url} {$target_dir}");
            wp_send_json_success([
                'message' => '仓库克隆成功',
                'output' => $output
            ]);
        } catch (Exception $e) {
            wp_send_json_error('克隆失败: ' . $e->getMessage(), 500);
        }
    }

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

        $repo_path = isset($_POST['repo_path']) ? sanitize_text_field($_POST['repo_path']) : '';
        
        try {
            $output = $this->run_git_command("-C {$repo_path} pull");
            wp_send_json_success([
                'message' => '拉取成功',
                'output' => $output
            ]);
        } catch (Exception $e) {
            wp_send_json_error('拉取失败: ' . $e->getMessage(), 500);
        }
    }

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

        $repo_path = isset($_POST['repo_path']) ? sanitize_text_field($_POST['repo_path']) : '';
        $commit_message = isset($_POST['commit_message']) ? sanitize_text_field($_POST['commit_message']) : '';
        
        try {
            $output = [];
            $output[] = $this->run_git_command("-C {$repo_path} add .");
            $output[] = $this->run_git_command("-C {$repo_path} commit -m '{$commit_message}'");
            $output[] = $this->run_git_command("-C {$repo_path} push");
            
            wp_send_json_success([
                'message' => '推送成功',
                'output' => implode("\n", $output)
            ]);
        } catch (Exception $e) {
            wp_send_json_error('推送失败: ' . $e->getMessage(), 500);
        }
    }

    private function run_git_command($command) {
        $output = shell_exec("git {$command} 2>&1");
        
        if (strpos($output, 'fatal:') !== false) {
            throw new Exception($output);
        }
        
        return $output;
    }
}

new CodeEditor_GitSync();