#pragma once

#include <gtkmm/window.h>
#include <gtkmm/headerbar.h>
#include <gtkmm/button.h>
#include <gtkmm/togglebutton.h>
#include <gtkmm/box.h>
#include <gtkmm/stack.h>
#include <gtkmm/image.h>
#include <gtkmm/label.h>
#include <gtkmm/searchbar.h>
#include <gtkmm/searchentry.h>
#include <gtkmm/overlay.h>
#include <gtkmm/revealer.h>

#include "BookList.hpp"
#include "BookShelf.hpp"
#include "BookDetails.hpp"

// Forward-declare the hash helper you already have.
std::string sha256_file(const std::string& path);


/**
 * @brief Top-level GTK window that wires the data model to the visual shelf.
 *
 * BibliothecaWindow owns the high-level widgets (header bar, search UI,
 * placeholder states) and orchestrates updates coming from @ref BookList.
 */
class BibliothecaWindow : public Gtk::Window {
public:
  explicit BibliothecaWindow(DatabaseManager& db, BookList& bookList);

private:
  // UI setup helpers
  void buildHeaderBar();
  void buildPlaceholder();
  void buildToast();
  void showPlaceholderIfEmpty();
  void updateVisibleView();

  // Toast notification
  void showToast(const Glib::ustring& message, bool is_error = false);
  void hideToast();

  // Callbacks
  void onAddBookClicked();
  void onBookActivated(const Book& book);
  void onSearchChanged();
  void onSearchToggle();
  void onSearchModeChanged();

  // Model (not owned)
  DatabaseManager& m_db;
  BookList& m_bookList;

  // UI
  Gtk::HeaderBar m_headerBar;
  Gtk::Button    m_addBookButton;
  Gtk::Button    m_backButton;
  Gtk::ToggleButton m_searchButton;

  Gtk::Box       m_mainBox {Gtk::Orientation::VERTICAL};
  Gtk::SearchBar m_searchBar;
  Gtk::SearchEntry m_searchEntry;
  Gtk::Overlay   m_overlay;
  Gtk::Stack     m_stack;

  // Toast notification widgets
  Gtk::Revealer  m_toastRevealer;
  Gtk::Box       m_toastBox {Gtk::Orientation::HORIZONTAL};
  Gtk::Label     m_toastLabel;
  Gtk::Button    m_toastCloseButton;
  sigc::connection m_toastTimeout;

  Gtk::Box       m_placeholder {Gtk::Orientation::VERTICAL};
  Gtk::Image     m_placeholderIcon;
  Gtk::Label     m_placeholderLabel;

  Gtk::Box       m_noResults {Gtk::Orientation::VERTICAL};
  Gtk::Label     m_noResultsLabel;

  // Owned shelf view
  std::unique_ptr<BookShelf> m_shelf;
  std::unique_ptr<BookDetails> m_bookDetails;
  std::string m_lastQuery;
};
