/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of 
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#ifndef INC_FRAMEWORK_OMG_PARSER_MODEL_PARSER_H_
#define INC_FRAMEWORK_OMG_PARSER_MODEL_PARSER_H_

#include <google/protobuf/message.h>
#include "framework/omg/parser/parser_types.h"
#include "framework/omg/omg_inner_types.h"
#include "graph/attr_value.h"
#include "graph/compute_graph.h"
#include "graph/ge_tensor.h"
#include "graph/graph.h"
#include "graph/op_desc.h"
#include "graph/utils/attr_utils.h"
#include "graph/utils/graph_utils.h"
#include "graph/utils/graph_utils_ex.h"
#include "graph/utils/op_desc_utils.h"
#include "graph/utils/tensor_utils.h"
#include "graph/ascend_string.h"

namespace domi {
using GetGraphCallback = std::function<std::unique_ptr<google::protobuf::Message>(
  const google::protobuf::Message *root_proto, const std::string &graph)>;

using GetGraphCallbackV2 = std::function<std::string(const std::string &subgraph_name)>;

using GetGraphCallbackV3 = std::function<ge::AscendString(const ge::AscendString &subgraph_name)>;

class GE_FUNC_VISIBILITY ModelParser {
 public:
  ModelParser() {}

  virtual ~ModelParser() {}

  /**
   * @ingroup domi_omg
   * @brief Analyze network model data
   * @param [in] file  Network model file path
   * @param [in|out]  graph Save the network information after analysis
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status Parse(const char *file, ge::Graph &graph) = 0;

  /**
   * @ingroup domi_omg
   * @brief Parse relevant data from memory and save it to graph
   * @param [in] input Model file memory data
   * @param [in] input Model file memory size
   * @param [in|out] graph A graph for saving the model information after analysis
   * @return SUCCESS
   * @return FAILED
   * @author
   */
  virtual Status ParseFromMemory(const char *data, uint32_t size, ge::ComputeGraphPtr &graph) = 0;

  /**
   * @ingroup domi_omg
   * @brief Parse relevant data from memory and save it to graph
   * @param [in] input Model file memory data
   * @param [in] input Model file memory size
   * @param [in|out] graph A graph for saving the model information after analysis
   * @return SUCCESS
   * @return FAILED
   * @author
   */
  virtual Status ParseFromMemory(const char *data, uint32_t size, ge::Graph &graph) = 0;

  /**
   * @ingroup domi_omg
   * @brief Analyze network model data
   * @param [in] proto  network model
   * @param [in|out]  graph Save the network information after analysis
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status ParseProto(const google::protobuf::Message *proto, ge::ComputeGraphPtr &graph) = 0;

  /**
   * @ingroup domi_omg
   * @brief Analyze callback model data in subgraph
   * @param [in] proto network model
   * @param [in] callback callback of subgraph
   * @param [in|out] graph Save the network information after analysis
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status ParseProtoWithSubgraph(const google::protobuf::Message *proto, GetGraphCallback callback,
                                              ge::ComputeGraphPtr &graph) = 0;
  /**
   * @ingroup domi_omg
   * @brief Convert model files to JSON format
   * @param [in] model_file  Model file path to be converted
   * @param [out] json_file Converted JSON file path
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status ToJson(const char *model_file, const char *json_file) {
   (void)model_file;
   (void)json_file;
   return SUCCESS;
  }

  /*
   * @ingroup domi_omg
   * @brief Convert network data type
   * @param [in] type Data type to be converted
   * @return ge::DataType
   */
  virtual ge::DataType ConvertToGeDataType(const uint32_t type) = 0;

  virtual Status ParseAllGraph(const google::protobuf::Message *root_proto, ge::ComputeGraphPtr &root_graph) = 0;

  /**
   * @ingroup domi_omg
   * @brief Analyze network model data
   * @param [in] proto  serialized network model
   * @param [in|out]  graph Save the network information after analysis
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status ParseProto(const std::string &serialized_proto, ge::ComputeGraphPtr &graph) {
    (void)serialized_proto;
    (void)graph;
    return UNSUPPORTED;
  }

  /**
   * @ingroup domi_omg
   * @brief Analyze callback model data in subgraph
   * @param [in] proto serialized network model
   * @param [in] callback callback of subgraph
   * @param [in|out] graph Save the network information after analysis
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status ParseProtoWithSubgraph(const std::string &serialized_proto, GetGraphCallbackV2 callback,
                                        ge::ComputeGraphPtr &graph) {
    (void)serialized_proto;
    (void)callback;
    (void)graph;
    return UNSUPPORTED;
  }

  /**
   * @ingroup domi_omg
   * @brief Analyze callback model data in subgraph
   * @param [in] partitioned_serialized partitioned serialized network model
   * @param [in] const_value_map const value map, key: constant node name value: serialized constant output tensor
   * @param [in] callback callback of subgraph
   * @param [in|out] graph Save the network information after analysis
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status ParseProtoWithSubgraph(const std::vector<std::string> &partitioned_serialized,
                                        const std::map<std::string, std::string> &const_value_map,
                                        GetGraphCallbackV2 callback,
                                        ge::ComputeGraphPtr &graph) {
    (void)partitioned_serialized;
    (void)const_value_map;
    (void)callback;
    (void)graph;
    return UNSUPPORTED;
  }

  /**
   * @ingroup domi_omg
   * @brief Analyze network model data
   * @param [in] partitioned_serialized partitioned serialized network model
   * @param [in] const_value_map const value map, key: constant node name value: serialized constant output tensor
   * @param [in|out]  graph Save the network information after analysis
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status ParseProto(const std::vector<std::string> &partitioned_serialized,
                            const std::map<std::string, std::string> &const_value_map,
                            ge::ComputeGraphPtr &graph) {
    (void)partitioned_serialized;
    (void)const_value_map;
    (void)graph;
    return UNSUPPORTED;
  }

  /**
   * @ingroup domi_omg
   * @brief Analyze callback model data in subgraph
   * @param [in] partitioned_serialized partitioned serialized network model
   * @param [in] const_value_map const value map, key: constant node name value: serialized constant output tensor
   * @param [in] callback callback of subgraph
   * @param [in|out] graph Save the network information after analysis
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status ParseProtoWithSubgraph(const std::vector<ge::AscendString> &partitioned_serialized,
                                        const std::map<ge::AscendString, ge::AscendString> &const_value_map,
                                        GetGraphCallbackV3 callback,
                                        ge::ComputeGraphPtr &graph) {
    (void)partitioned_serialized;
    (void)const_value_map;
    (void)callback;
    (void)graph;
    return UNSUPPORTED;
  }

  /**
   * @ingroup domi_omg
   * @brief Analyze callback model data in subgraph
   * @param [in] proto serialized network model
   * @param [in] callback callback of subgraph
   * @param [in|out] graph Save the network information after analysis
   * @return SUCCESS
   * @return Others failed
   */
  virtual Status ParseProtoWithSubgraph(const ge::AscendString &serialized_proto, GetGraphCallbackV3 callback,
                                        ge::ComputeGraphPtr &graph) {
    (void)serialized_proto;
    (void)callback;
    (void)graph;
    return UNSUPPORTED;
  }

  virtual bool HasError() {
    return false;
  }

  virtual Status Save(const std::string &file) {
    (void)file;
    return SUCCESS;
  }

  virtual void Clear() {};
};
}  // namespace domi

#endif  // INC_FRAMEWORK_OMG_PARSER_MODEL_PARSER_H_