#ifndef PLS_INTERNAL_PROFILING_DAG_NODE_H_
#define PLS_INTERNAL_PROFILING_DAG_NODE_H_

#include <memory>
#include <list>
#include <algorithm>

namespace pls::internal::profiling {

struct dag_node {
  dag_node(unsigned spawning_thread_id) : spawning_thread_id_{spawning_thread_id} {};

  unsigned spawning_thread_id_;
  unsigned long max_memory_{0};
  unsigned long total_runtime_{0};

  std::unique_ptr<dag_node> next_node_;
  std::list<dag_node> child_nodes_;

  unsigned dag_max_memory() {
    unsigned max = max_memory_;
    if (next_node_) {
      max = std::max(max, next_node_->dag_max_memory());
    }
    for (auto &child : child_nodes_) {
      max = std::max(max, child.dag_max_memory());
    }
    return max;
  }

  unsigned long dag_total_user_time() {
    unsigned total_user_time = total_runtime_;
    if (next_node_) {
      total_user_time += next_node_->dag_total_user_time();
    }
    for (auto &child : child_nodes_) {
      total_user_time += child.dag_total_user_time();
    }
    return total_user_time;
  }
};

}

#endif //PLS_INTERNAL_PROFILING_DAG_NODE_H_
