#pragma once

#ifdef LUMACS_WITH_GTK

#include <gtkmm.h>
#include <sigc++/sigc++.h> // Added explicit include
#include <vector>
#include <string>

#include "lumacs/completion_common.hpp" // For CompletionCandidate

namespace lumacs {

/// @brief A GTK widget that displays a list of completion candidates in a popup.
class GtkCompletionPopup : public Gtk::Popover {
public:
    GtkCompletionPopup();
    ~GtkCompletionPopup() override;

    /// @brief Displays the completion popup with the given candidates.
    /// @param candidates The list of completion candidates to display.
    /// @param active_index The index of the currently active candidate.
    /// @param relative_to The widget to position the popup relative to.
    /// @param x_offset The x-offset from the relative widget's origin.
    /// @param y_offset The y-offset from the relative widget's origin.
    void show_popup(const std::vector<CompletionCandidate>& candidates, size_t active_index, Gtk::Widget& relative_to, int x_offset, int y_offset);

    /// @brief Hides the completion popup.
    void hide_popup();

    /// @brief Navigates to the next candidate in the list.
    void select_next();

    /// @brief Navigates to the previous candidate in the list.
    void select_previous();

    /// @brief Returns the currently selected candidate, if any.
    std::optional<CompletionCandidate> get_selected_candidate() const;

    /// Signals
    using type_signal_candidate_selected = sigc::signal<void(CompletionCandidate)>;
    type_signal_candidate_selected signal_candidate_selected();

    using type_signal_cancelled = sigc::signal<void()>;
    type_signal_cancelled signal_cancelled();

protected:
    // Signal handlers
    void on_row_activated(Gtk::ListBoxRow* row);

private:
    Gtk::ScrolledWindow list_scrolled_window_;
    Gtk::ListBox list_box_;
    std::vector<CompletionCandidate> candidates_;
    size_t active_index_;

    type_signal_candidate_selected signal_candidate_selected_;
    type_signal_cancelled signal_cancelled_;

    // Helper to update the displayed list and selection
    void update_list();
    void set_active_row(size_t index);
};

} // namespace lumacs

#endif // LUMACS_WITH_GTK
