// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "transformations/op_conversions/convert_nms9_to_nms_ie_internal.hpp"

#include <gtest/gtest.h>

#include <memory>
#include <queue>
#include <string>

#include "common_test_utils/ov_test_utils.hpp"
#include "openvino/core/model.hpp"
#include "openvino/op/non_max_suppression.hpp"
#include "openvino/opsets/opset9_decl.hpp"
#include "openvino/pass/constant_folding.hpp"
#include "openvino/pass/manager.hpp"
#include "ov_ops/nms_ie_internal.hpp"
#include "transformations/init_node_info.hpp"
#include "transformations/op_conversions/convert_previous_nms_to_nms_9.hpp"
#include "transformations/utils/utils.hpp"

using namespace testing;
using namespace ov;

namespace v0 = ov::op::v0;
TEST_F(TransformationTestsF, ConvertPreviousNMSToNMSIEInternal) {
    {
        auto boxes = std::make_shared<v0::Parameter>(element::f32, Shape{1, 1000, 4});
        auto scores = std::make_shared<v0::Parameter>(element::f32, Shape{1, 1, 1000});
        auto max_output_boxes_per_class = v0::Constant::create(element::i64, Shape{}, {10});
        auto iou_threshold = v0::Constant::create(element::f32, Shape{}, {0.75});
        auto score_threshold = v0::Constant::create(element::f32, Shape{}, {0.7});
        auto nms = std::make_shared<ov::op::v1::NonMaxSuppression>(boxes,
                                                                   scores,
                                                                   max_output_boxes_per_class,
                                                                   iou_threshold,
                                                                   score_threshold,
                                                                   op::v1::NonMaxSuppression::BoxEncodingType::CORNER,
                                                                   true);
        nms->set_friendly_name("nms");

        model = std::make_shared<Model>(OutputVector{nms}, ParameterVector{boxes, scores});

        manager.register_pass<ov::pass::ConvertNMS1ToNMS9>();
        manager.register_pass<ov::pass::ConvertNMS9ToNMSIEInternal>();
        manager.register_pass<pass::ConstantFolding>();

        // as inside test infrastructure we can not predict output names for given Model
        // we have to enable soft names comparison manually
        enable_soft_names_comparison();
    }

    {
        auto boxes = std::make_shared<v0::Parameter>(element::f32, Shape{1, 1000, 4});
        auto scores = std::make_shared<v0::Parameter>(element::f32, Shape{1, 1, 1000});
        auto max_output_boxes_per_class = v0::Constant::create(element::i64, Shape{1}, {10});
        auto iou_threshold = v0::Constant::create(element::f32, Shape{1}, {0.75});
        auto score_threshold = v0::Constant::create(element::f32, Shape{1}, {0.7});
        auto nms = std::make_shared<ov::op::internal::NonMaxSuppressionIEInternal>(boxes,
                                                                                   scores,
                                                                                   max_output_boxes_per_class,
                                                                                   iou_threshold,
                                                                                   score_threshold,
                                                                                   0,
                                                                                   true,
                                                                                   element::i32);
        auto convert = std::make_shared<v0::Convert>(nms->output(0), element::i64);
        convert->set_friendly_name("nms:0");

        model_ref = std::make_shared<Model>(OutputVector{convert}, ParameterVector{boxes, scores});
    }
}

TEST_F(TransformationTestsF, ConvertNMS9ToNMSIEInternal) {
    {
        auto boxes = std::make_shared<v0::Parameter>(element::f32, Shape{1, 1000, 4});
        auto scores = std::make_shared<v0::Parameter>(element::f32, Shape{1, 1, 1000});
        auto max_output_boxes_per_class = v0::Constant::create(element::i32, Shape{}, {10});
        auto iou_threshold = v0::Constant::create(element::f32, Shape{}, {0.75});
        auto score_threshold = v0::Constant::create(element::f32, Shape{}, {0.7});
        auto soft_nms_sigma = v0::Constant::create(element::f32, Shape{}, {0.5});
        auto nms = std::make_shared<opset9::NonMaxSuppression>(boxes,
                                                               scores,
                                                               max_output_boxes_per_class,
                                                               iou_threshold,
                                                               score_threshold,
                                                               soft_nms_sigma,
                                                               opset9::NonMaxSuppression::BoxEncodingType::CORNER,
                                                               true,
                                                               element::i32);

        model = std::make_shared<Model>(OutputVector{nms}, ParameterVector{boxes, scores});

        manager.register_pass<ov::pass::ConvertNMS9ToNMSIEInternal>();
        manager.register_pass<pass::ConstantFolding>();
    }

    {
        auto boxes = std::make_shared<v0::Parameter>(element::f32, Shape{1, 1000, 4});
        auto scores = std::make_shared<v0::Parameter>(element::f32, Shape{1, 1, 1000});
        auto max_output_boxes_per_class = v0::Constant::create(element::i32, Shape{1}, {10});
        auto iou_threshold = v0::Constant::create(element::f32, Shape{1}, {0.75});
        auto score_threshold = v0::Constant::create(element::f32, Shape{1}, {0.7});
        auto soft_nms_sigma = v0::Constant::create(element::f32, Shape{1}, {0.5});
        auto nms = std::make_shared<ov::op::internal::NonMaxSuppressionIEInternal>(boxes,
                                                                                   scores,
                                                                                   max_output_boxes_per_class,
                                                                                   iou_threshold,
                                                                                   score_threshold,
                                                                                   soft_nms_sigma,
                                                                                   0,
                                                                                   true,
                                                                                   element::i32);

        model_ref = std::make_shared<Model>(OutputVector{nms}, ParameterVector{boxes, scores});
    }
}
