#pragma once

#include <cstdint>

#include <QString>

#include "core/models/RawBusMessage.h"

namespace softbus::core::models
{

// Hash physical endpoint string into a compact routing key.
inline std::uint32_t makeEndpointHash(const QString& endpoint)
{
    return qHash(endpoint);
}

// Build a deterministic string key from endpoint hash (for session maps).
inline QString makeEndpointKey(std::uint32_t endpointHash)
{
    return QString::number(endpointHash, 16);
}

// Build a 64-bit routing key for fast lookup paths.
inline std::uint64_t makeRoutingKey(std::uint32_t endpointHash, int deviceId)
{
    return (static_cast<std::uint64_t>(endpointHash) << 32) | static_cast<std::uint32_t>(deviceId);
}

// Bridge routing key to legacy string-based matching interfaces.
inline QString routingKeyToStableKey(std::uint64_t routingKey)
{
    return routingKey == 0 ? QString() : QString::number(routingKey);
}

inline ProtocolType protocolTypeFromHint(const QString& hint)
{
    if (hint.compare(QStringLiteral("modbus_rtu"), Qt::CaseInsensitive) == 0) {
        return ProtocolType::MODBUS_RTU;
    }
    if (hint.compare(QStringLiteral("modbus_ascii"), Qt::CaseInsensitive) == 0) {
        return ProtocolType::MODBUS_ASCII;
    }
    if (hint.compare(QStringLiteral("modbus_tcp"), Qt::CaseInsensitive) == 0) {
        return ProtocolType::MODBUS_TCP;
    }
    if (hint.compare(QStringLiteral("can_raw"), Qt::CaseInsensitive) == 0) {
        return ProtocolType::CAN_RAW;
    }
    if (hint.compare(QStringLiteral("can_open"), Qt::CaseInsensitive) == 0) {
        return ProtocolType::CAN_OPEN;
    }
    if (hint.compare(QStringLiteral("udp_custom"), Qt::CaseInsensitive) == 0) {
        return ProtocolType::UDP_CUSTOM;
    }
    return ProtocolType::UNKNOWN;
}

} // namespace softbus::core::models
