#pragma once

#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QProgressBar>
#include <QFutureWatcher>
#include <QString>
#include <QStringList>

// ---------------------------------------------------------------------------
// FileSearchPanel – Suche in allen Dateien eines Verzeichnisses.
//
// Ergebnisse werden als aufklappbare Liste angezeigt:
//   Dateiname (N Treffer)
//     └ Zeile 12: <Zeileninhalt>
//     └ Zeile 34: <Zeileninhalt>
//
// Klick auf einen Treffer öffnet die Datei im Editor und springt zur Zeile.
// ---------------------------------------------------------------------------
class FileSearchPanel : public QWidget
{
    Q_OBJECT

public:
    explicit FileSearchPanel(QWidget *parent = nullptr);

    void setSearchRoot(const QString &path);

public slots:
    void activate();

signals:
    void fileLineRequested(const QString &filePath, int line);

private slots:
    void onSearch();
    void onResultActivated(QTreeWidgetItem *item, int column);
    void onSearchFinished();

private:
    struct Match
    {
        QString filePath;
        int     line;
        QString content;
    };

    void setupUi();
    QList<Match> searchInFiles(const QString &root,
                               const QString &needle,
                               bool caseSensitive,
                               bool wholeWord,
                               bool useRegex,
                               const QStringList &extensions) const;

    QString      m_searchRoot;

    QLineEdit   *m_searchEdit   = nullptr;
    QCheckBox   *m_chkCase      = nullptr;
    QCheckBox   *m_chkWord      = nullptr;
    QCheckBox   *m_chkRegex     = nullptr;
    QLineEdit   *m_filterEdit   = nullptr;   // Dateiendungen-Filter
    QPushButton *m_btnSearch    = nullptr;
    QPushButton *m_btnClose     = nullptr;
    QLabel      *m_statusLabel  = nullptr;
    QTreeWidget *m_results      = nullptr;
    QProgressBar *m_progress    = nullptr;

    QFutureWatcher<QList<Match>> *m_watcher = nullptr;
};
