#pragma once

#include <softbus_core/models/DOMMessage.h>
#include <softbus_core/models/RawPacket.h>

#include <cstdint>
#include <deque>
#include <map>
#include <mutex>
#include <string>
#include <vector>

namespace softbus::core {

enum class AlignStrategy {
    Nearest,
    Linear
};

struct TimeAlignerConfig {
    int64_t windowNs{50'000'000};
    AlignStrategy strategy{AlignStrategy::Nearest};
};

struct AlignedSample {
    int64_t anchorTimestampNs{0};
    std::map<std::string, DOMMessage> channels;
};

class TimeAligner {
public:
    explicit TimeAligner(TimeAlignerConfig config = {});

    void ingest(const RawPacket& raw);
    void ingestDom(const DOMMessage& dom);
    std::vector<AlignedSample> drainAligned();

    void setConfig(const TimeAlignerConfig& config);
    TimeAlignerConfig config() const;

private:
    struct ChannelBuffer {
        std::deque<DOMMessage> samples;
    };

    DOMMessage rawToDom(const RawPacket& raw) const;
    AlignedSample alignAt(int64_t anchorNs);

    mutable std::mutex mutex_;
    TimeAlignerConfig config_;
    std::map<std::string, ChannelBuffer> channels_;
    int64_t lastAnchorNs_{0};
};

} // namespace softbus::core
