//
// This file is a part of UERANSIM project.
// Copyright (c) 2023 ALİ GÜNGÖR.
//
// https://github.com/aligungr/UERANSIM/
// See README, LICENSE, and CONTRIBUTING files for licensing details.
//

#include "task.hpp"

namespace nr::gnb
{

NgapAmfContext *NgapTask::selectAmf(int ueId, int32_t &requestedSliceType)
{
    // 优先匹配切片
    for (auto &amf : m_amfCtx) {
        for (const auto &plmnSupport : amf.second->plmnSupportList) {
            for (const auto &singleSlice : plmnSupport->sliceSupportList.slices) {
                int32_t supportedSliceType = static_cast<int32_t>(singleSlice.sst);
                if (supportedSliceType == requestedSliceType) {
                    return amf.second;
                }
            }
        }
    }

    // 回退：选择任一已连接的AMF
    for (auto &amf : m_amfCtx) {
        if (amf.second->state == EAmfState::CONNECTED)
            return amf.second;
    }

    // 最后回退：任意AMF
    if (!m_amfCtx.empty())
        return m_amfCtx.begin()->second;

    return nullptr;
}

NgapAmfContext *NgapTask::selectNewAmfForReAllocation(int ueId, int initiatedAmfId, int amfSetId)
{
    // TODO an arbitrary AMF is selected for now
    return findAmfContext(initiatedAmfId);
}

} // namespace nr::gnb
