#pragma once

#include <shared_mutex>
#include <vector>

#include <QHash>
#include <QJsonObject>
#include <QString>

namespace softbus::message_bus::pipeline
{

struct RuleScope
{
    QString endpoint;
    int deviceId{-1};
    QString stableKey;
};

struct FilterRule
{
    QString id;
    RuleScope scope;
    QJsonObject condition;
};

struct TransformRule
{
    QString id;
    RuleScope scope;
    double scale{1.0};
    double offset{0.0};
    QString unit;
};

class DynamicRuleRegistry
{
public:
    static DynamicRuleRegistry& instance();

    bool addFilterRule(const FilterRule& rule);
    bool removeFilterRule(const QString& id);
    bool addTransformRule(const TransformRule& rule);
    bool removeTransformRule(const QString& id);

    std::vector<FilterRule> snapshotFilterRules(
        const QString& endpoint, int deviceId, const QString& stableKey) const;
    std::vector<TransformRule> snapshotTransformRules(
        const QString& endpoint, int deviceId, const QString& stableKey) const;

    QJsonObject status() const;

private:
    DynamicRuleRegistry() = default;
    bool matchScope(const RuleScope& scope, const QString& endpoint, int deviceId, const QString& stableKey) const;

private:
    mutable std::shared_mutex m_mtx;
    QHash<QString, FilterRule> m_filterRules;
    QHash<QString, TransformRule> m_transformRules;
};

} // namespace softbus::message_bus::pipeline

