#ifndef DATABASEMANAGER_H
#define DATABASEMANAGER_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QString>
#include <QList>
#include <QPair>
#include <QThread>
#include <QMap>

/**
 * @brief The DatabaseManager class handles all database operations
 * for the screenshot gallery application.
 */
class DatabaseManager : public QObject
{
    Q_OBJECT

public:
    /**
     * @brief Struct to represent an image item with its path and OCR text
     */
    struct ImageItem {
        QString filePath;
        QString ocrText;
    };

    /**
     * @brief DatabaseManager constructor
     * @param parent The parent QObject
     */
    explicit DatabaseManager(QObject *parent = nullptr);
    
    /**
     * @brief DatabaseManager destructor
     */
    ~DatabaseManager();

    /**
     * @brief Initialize the database connection
     * @param dbPath Path to the SQLite database
     * @return True if connection was successful, false otherwise
     */
    bool initialize(const QString &dbPath);

    /**
     * @brief Get all images from the database
     * @return List of ImageItem objects
     */
    QList<ImageItem> getAllImages();

public slots:
    /**
     * @brief Search for images matching the search text
     * @param searchText Text to search for in OCR results
     * @return List of ImageItem objects that match the search
     */
    QList<ImageItem> searchImages(const QString &searchText);

private:
    QSqlDatabase m_db;
    bool m_initialized;
    
    // Cache for search results to improve response time
    QMap<QString, QList<ImageItem>> m_searchCache;
    
    // Maximum number of cached search queries
    static const int MAX_CACHE_SIZE = 50;
};

#endif // DATABASEMANAGER_H