/****************************************************************************
 *   This file is part of the aGrUM/pyAgrum library.                        *
 *                                                                          *
 *   Copyright (c) 2005-2026 by                                             *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *                                                                          *
 *   The aGrUM/pyAgrum library is free software; you can redistribute it    *
 *   and/or modify it under the terms of either :                           *
 *                                                                          *
 *    - the GNU Lesser General Public License as published by               *
 *      the Free Software Foundation, either version 3 of the License,      *
 *      or (at your option) any later version,                              *
 *    - the MIT license (MIT),                                              *
 *    - or both in dual license, as here.                                   *
 *                                                                          *
 *   (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html)    *
 *                                                                          *
 *   This aGrUM/pyAgrum library is distributed in the hope that it will be  *
 *   useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,          *
 *   INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
 *   FOR A PARTICULAR PURPOSE  AND NONINFRINGEMENT. IN NO EVENT SHALL THE   *
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,        *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  *
 *   OTHER DEALINGS IN THE SOFTWARE.                                        *
 *                                                                          *
 *   See LICENCES for more details.                                         *
 *                                                                          *
 *   SPDX-FileCopyrightText: Copyright 2005-2026                            *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *   SPDX-License-Identifier: LGPL-3.0-or-later OR MIT                      *
 *                                                                          *
 *   Contact  : info_at_agrum_dot_org                                       *
 *   homepage : http://agrum.gitlab.io                                      *
 *   gitlab   : https://gitlab.com/agrumery/agrum                           *
 *                                                                          *
 ****************************************************************************/


#pragma once

#include <agrum/base/graphs/algorithms/generic/bayesBall.h>

namespace gum::graph {

  template < GUM_DiGraphable G, bool CollectAll >
  NodeSet
      _bayesBall_(const G& g, const NodeSet& query, const NodeSet& Zhard, const NodeSet& Zsoft) {
    NodeSet result;

    static constexpr std::pair< bool, bool > empty_mark{false, false};
    NodeProperty< std::pair< bool, bool > >  marks(g.size());

    List< std::pair< NodeId, bool > > to_visit;
    for (const auto node: query)
      to_visit.insert(std::pair{node, true});

    while (!to_visit.empty()) {
      const NodeId node       = to_visit.front().first;
      const bool   from_child = to_visit.front().second;
      to_visit.popFront();

      auto& [top, bot] = marks.getWithDefault(node, empty_mark);

      if (from_child) {
        result.insert(node);                // always requisite on upward visit
        if (Zhard.exists(node)) continue;   // hard evidence blocks upward

        if (!top) {
          top = true;
          for (const auto par: g.parents(node))
            to_visit.insert(std::pair{par, true});
        }
        if (!bot) {
          bot = true;
          for (const auto chi: g.children(node))
            to_visit.insert(std::pair{chi, false});
        }

      } else {
        if constexpr (CollectAll) result.insert(node);   // dConnected: all visits

        const bool is_hard = Zhard.exists(node);
        const bool is_ev   = is_hard || Zsoft.exists(node);

        if (is_ev && !top) {
          top = true;
          if constexpr (!CollectAll) result.insert(node);   // requisiteNodes: collider only
          for (const auto par: g.parents(node))
            to_visit.insert(std::pair{par, true});
        }
        if (!is_hard && !bot) {
          bot = true;
          for (const auto chi: g.children(node))
            to_visit.insert(std::pair{chi, false});
        }
      }
    }

    return result;
  }

  template < GUM_DiGraphable G >
  NodeSet
      requisiteNodes(const G& g, const NodeSet& query, const NodeSet& Zhard, const NodeSet& Zsoft) {
    return _bayesBall_< G, false >(g, query, Zhard, Zsoft);
  }

  template < GUM_DiGraphable G >
  NodeSet dConnected(const G& g, const NodeSet& query, const NodeSet& Zhard, const NodeSet& Zsoft) {
    return _bayesBall_< G, true >(g, query, Zhard, Zsoft);
  }

}   // namespace gum::graph
