// Copyright (c) ONNX Project Contributors
//
// SPDX-License-Identifier: Apache-2.0

#include "onnx_core/backend_test/expect.h"
#include "onnx_extensions/backend_test/cases/traditionalml/include_traditionalml_cases.h"
#include "onnx_extensions/kernels/kernels/traditionalml/include_traditionalml_kernels.h"

#include <cstdint>
#include <vector>

namespace ONNX_LIGHT_NAMESPACE::onnx_backend_test {

void RegisterSVMClassifierCases(std::vector<TestCase> &registry, TestMode mode) {
  const OpsetId opset("ai.onnx.ml", 1);
  const KernelContext ctx{opset};
  const OpsetId default_opset = DefaultOpset(13);
  const onnx_kernels::kernel::SVMClassifier svm{ctx};

  if (mode == TestMode::BENCHMARK) {
    NodeProto node;
    node.set_op_type("SVMClassifier");
    node.set_domain("ai.onnx.ml");
    node.add_input("x");
    node.add_output("y");
    node.add_output("z");

    AttributeProto *kernel_type = node.add_attribute();
    kernel_type->set_name("kernel_type");
    kernel_type->set_type(AttributeProto::AttributeType::STRING);
    kernel_type->set_s("LINEAR");

    AttributeProto *kernel_params = node.add_attribute();
    kernel_params->set_name("kernel_params");
    kernel_params->set_type(AttributeProto::AttributeType::FLOATS);
    kernel_params->add_floats(0.0f);
    kernel_params->add_floats(0.0f);
    kernel_params->add_floats(0.0f);

    AttributeProto *vectors_per_class = node.add_attribute();
    vectors_per_class->set_name("vectors_per_class");
    vectors_per_class->set_type(AttributeProto::AttributeType::INTS);
    vectors_per_class->add_ints(static_cast<int64_t>(1));
    vectors_per_class->add_ints(static_cast<int64_t>(1));

    AttributeProto *support_vectors = node.add_attribute();
    support_vectors->set_name("support_vectors");
    support_vectors->set_type(AttributeProto::AttributeType::FLOATS);
    support_vectors->add_floats(1.0f);
    support_vectors->add_floats(0.0f);
    support_vectors->add_floats(0.0f);
    support_vectors->add_floats(1.0f);

    AttributeProto *coefficients = node.add_attribute();
    coefficients->set_name("coefficients");
    coefficients->set_type(AttributeProto::AttributeType::FLOATS);
    coefficients->add_floats(1.0f);
    coefficients->add_floats(-1.0f);

    AttributeProto *rho = node.add_attribute();
    rho->set_name("rho");
    rho->set_type(AttributeProto::AttributeType::FLOATS);
    rho->add_floats(0.0f);

    AttributeProto *post_transform = node.add_attribute();
    post_transform->set_name("post_transform");
    post_transform->set_type(AttributeProto::AttributeType::STRING);
    post_transform->set_s("NONE");

    AttributeProto *labels = node.add_attribute();
    labels->set_name("classlabels_ints");
    labels->set_type(AttributeProto::AttributeType::INTS);
    labels->add_ints(static_cast<int64_t>(0));
    labels->add_ints(static_cast<int64_t>(1));

    Expect(registry, std::move(node), "test_cc_svmclassifier_int64_binary_benchmark",
           {default_opset, opset}, {16384}, {8192, 16384}, [svm]() -> IoData {
             Tensor x = RandnTensor(DataType::FLOAT, {8192, 2}, 2671);
             auto [y, z] = svm.operator()<float>(x, {1.0f, 0.0f, 0.0f, 1.0f}, {1.0f, -1.0f}, {0.0f},
                                                 {1, 1}, {0, 1}, "LINEAR", 0.0f, 0.0f, 0.0f);
             return IoData{{std::move(x)}, {std::move(y), std::move(z)}};
           });
    return;
  }

  NodeProto node;
  node.set_op_type("SVMClassifier");
  node.set_domain("ai.onnx.ml");
  node.add_input("x");
  node.add_output("y");
  node.add_output("z");

  AttributeProto *kernel_type = node.add_attribute();
  kernel_type->set_name("kernel_type");
  kernel_type->set_type(AttributeProto::AttributeType::STRING);
  kernel_type->set_s("LINEAR");

  AttributeProto *kernel_params = node.add_attribute();
  kernel_params->set_name("kernel_params");
  kernel_params->set_type(AttributeProto::AttributeType::FLOATS);
  kernel_params->add_floats(0.0f);
  kernel_params->add_floats(0.0f);
  kernel_params->add_floats(0.0f);

  AttributeProto *vectors_per_class = node.add_attribute();
  vectors_per_class->set_name("vectors_per_class");
  vectors_per_class->set_type(AttributeProto::AttributeType::INTS);
  vectors_per_class->add_ints(static_cast<int64_t>(1));
  vectors_per_class->add_ints(static_cast<int64_t>(1));

  AttributeProto *support_vectors = node.add_attribute();
  support_vectors->set_name("support_vectors");
  support_vectors->set_type(AttributeProto::AttributeType::FLOATS);
  support_vectors->add_floats(1.0f);
  support_vectors->add_floats(0.0f);
  support_vectors->add_floats(0.0f);
  support_vectors->add_floats(1.0f);

  AttributeProto *coefficients = node.add_attribute();
  coefficients->set_name("coefficients");
  coefficients->set_type(AttributeProto::AttributeType::FLOATS);
  coefficients->add_floats(1.0f);
  coefficients->add_floats(-1.0f);

  AttributeProto *rho = node.add_attribute();
  rho->set_name("rho");
  rho->set_type(AttributeProto::AttributeType::FLOATS);
  rho->add_floats(0.0f);

  AttributeProto *post_transform = node.add_attribute();
  post_transform->set_name("post_transform");
  post_transform->set_type(AttributeProto::AttributeType::STRING);
  post_transform->set_s("NONE");

  AttributeProto *labels = node.add_attribute();
  labels->set_name("classlabels_ints");
  labels->set_type(AttributeProto::AttributeType::INTS);
  labels->add_ints(static_cast<int64_t>(0));
  labels->add_ints(static_cast<int64_t>(1));

  Expect(registry, std::move(node), "test_cc_svmclassifier_int64_binary", {default_opset, opset},
         [=]() -> IoData {
           Tensor x = Tensor::FromFloat("", {2, 2}, {2.0f, 1.0f, 0.0f, 3.0f});
           auto yz = svm.operator()<float>(x, {1.0f, 0.0f, 0.0f, 1.0f}, {1.0f, -1.0f}, {0.0f},
                                           {1, 1}, {0, 1}, "LINEAR", 0.0f, 0.0f, 0.0f);
           return IoData{{std::move(x)}, {std::move(yz.first), std::move(yz.second)}};
         });
}

} // namespace ONNX_LIGHT_NAMESPACE::onnx_backend_test
