#pragma once

#include <QObject>
#include <memory>
#include <vector>
#include <QHash>
#include <QDateTime>
#include <functional>

#include "device_bus/monitor/IDeviceMonitor.h"

namespace softbus::device_bus::registry { class IDeviceRegistry; }

namespace softbus::device_bus::monitor
{

class DeviceMonitorService : public QObject
{
    Q_OBJECT
public:
    explicit DeviceMonitorService(QObject* parent = nullptr);
    ~DeviceMonitorService() override;

    bool start();
    void stop();
    bool initialScan();
    void setRegistry(softbus::device_bus::registry::IDeviceRegistry* registry);
    void setDirtyCallback(std::function<void()> cb);

private:
    void onEvent(const DeviceEvent& ev);

private:
    std::vector<std::unique_ptr<IDeviceMonitor>> m_monitors;
    bool m_started{false};
    softbus::device_bus::registry::IDeviceRegistry* m_registry{nullptr};
    std::function<void()> m_dirtyCb;

    // Lightweight de-dup: key -> last timestamp (ms)
    QHash<QString, qint64> m_lastEventMsByKey;
};

} // namespace softbus::device_bus::monitor

