//+------------------------------------------------------------------+
//|                                                  ONNXRuntime.mqh |
//|                        GIT under Copyright 2025, MetaQuotes Ltd. |
//|                     https://www.mql5.com/en/users/johnhlomohang/ |
//+------------------------------------------------------------------+
#property copyright "GIT under Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com/en/users/johnhlomohang/"

//--- ONNX flags (keep it in sync with MT5 if changed)
#define ONNX_DEFAULT       0
#define ONNX_DEBUG_LOGS    1
#define ONNX_NO_CONVERSION 2

//--- Simple ONNX model wrapper that calls MT5 built-in Onnx* functions.
//--- This wrapper DOES NOT redeclare OnnxTypeInfo and DOES NOT import any DLL.

//+------------------------------------------------------------------+
//|                     Simple ONNX Runtime Wrapper                  |
//+------------------------------------------------------------------+
class CSimpleONNXModel
  {
private:
   long              session;
   bool              initialized;

public:

                     CSimpleONNXModel()
     {
      session = INVALID_HANDLE;
      initialized = false;
     }

                    ~CSimpleONNXModel()
     {
      Release();
     }

   //--- Initialize model from resource
   bool              InitFromResource(const uchar &model[])
     {
      session = OnnxCreateFromBuffer(model, ONNX_DEFAULT);

      if(session == INVALID_HANDLE)
        {
         Print("ONNX create failed: ", GetLastError());
         return false;
        }

      initialized = true;

      Print("ONNX model loaded successfully");

      return true;
     }

   bool              SetInputShape(int index,long &shape[])
     {
      return OnnxSetInputShape(session,index,shape);
     }

   bool              SetOutputShape(int index,long &shape[])
     {
      return OnnxSetOutputShape(session,index,shape);
     }

   bool              Run(double &input_features[],
            long &edge_index[],
            double &output[])
     {
      if(!initialized)
         return false;

      bool result = OnnxRun(session,
                            ONNX_NO_CONVERSION,
                            input_features,
                            edge_index,
                            output);

      if(!result)
         Print("ONNX run failed: ",GetLastError());

      return result;
     }

   void              Release()
     {
      if(session!=INVALID_HANDLE)
        {
         OnnxRelease(session);
         session=INVALID_HANDLE;
        }
     }
  };
//+------------------------------------------------------------------+
