#pragma once

#include "lumacs/buffer.hpp"
#include "lumacs/window.hpp"
#include "lumacs/config.hpp"
#include "lumacs/theme.hpp"
#include <string>
#include <memory>
#include <vector>
#include <optional>
#include <filesystem>

namespace lumacs {

// Forward declarations
class LuaApi;
class KillRing;
class KeyBindingManager;
class ModelineManager;
class MinibufferManager;
class CommandSystem;
class CompletionSystem;

/// @brief Interface defining the essential editor services that commands can interact with.
/// This interface aims to reduce the direct coupling of CommandSystem and individual commands
/// to the entire EditorCore class, making them more testable and modular.
class ICommandTarget {
public:
    virtual ~ICommandTarget() = default;

    // Buffer Management
    virtual const Buffer& buffer() const noexcept = 0;
    virtual Buffer& buffer() noexcept = 0;
    virtual bool load_file(const std::filesystem::path& path) = 0;
    virtual void new_buffer(std::string name = "*scratch*") = 0;
    virtual std::vector<std::string> get_buffer_names() const = 0;
    virtual std::shared_ptr<Buffer> get_buffer_by_name(const std::string& name) = 0;
    virtual bool switch_buffer_in_window(const std::string& name) = 0;
    virtual bool close_buffer(const std::string& name) = 0;
    
    // Window Management
    virtual std::shared_ptr<Window> active_window() const = 0;
    virtual bool set_active_window(std::shared_ptr<Window> window) = 0;
    virtual void split_horizontally() = 0;
    virtual void split_vertically() = 0;
    virtual void close_active_window() = 0;
    virtual void next_window() = 0;

    // Cursor Management
    virtual Position cursor() const noexcept = 0;
    virtual void set_cursor(Position pos) = 0;

    // Basic Editing (proxied from EditorCore for commands)
    virtual void move_up() = 0;
    virtual void move_down() = 0;
    virtual void move_left() = 0;
    virtual void move_right() = 0;
    virtual void move_to_line_start() = 0;
    virtual void move_to_line_end() = 0;
    virtual void move_forward_word() = 0;
    virtual void move_backward_word() = 0;
    virtual void page_up() = 0;
    virtual void page_down() = 0;
    virtual void goto_beginning() = 0;
    virtual void goto_end() = 0;
    virtual void goto_line(size_t line) = 0;
    virtual void kill_line() = 0;
    virtual void kill_region() = 0; // Fixed: Removed duplicate 'void'
    virtual void copy_region_as_kill() = 0;
    virtual void yank() = 0;
    virtual void yank_pop() = 0;
    virtual void kill_word() = 0;
    virtual void backward_kill_word() = 0;

    // Message Display
    virtual void set_message(std::string msg) = 0;

    // Quit
    virtual void request_quit() = 0;

    // Configuration
    virtual Config& config() = 0;
    virtual const Config& config() const = 0;

    // Theme Management
    virtual ThemeManager& theme_manager() = 0;
    virtual const ThemeManager& theme_manager() const = 0;

    // Registers Management
    virtual void copy_to_register(char register_name, const std::string& text) = 0;
    virtual bool insert_register(char register_name) = 0;
    virtual void copy_region_to_register(char register_name) = 0;
    virtual bool yank_from_register(char register_name) = 0;

    // Keyboard Macro Management
    virtual void start_kbd_macro() = 0;
    virtual void end_kbd_macro_or_call() = 0;
    virtual void record_key_sequence(const std::string& key_sequence) = 0;
    virtual bool is_recording_macro() const noexcept = 0;

    // Rectangle Operations Management
    virtual void kill_rectangle() = 0;
    virtual void yank_rectangle() = 0;
    virtual void string_rectangle(const std::string& text) = 0;
};

} // namespace lumacs