// Copyright (c) 2019, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.

#ifndef MAMBA_CORE_POOL_HPP
#define MAMBA_CORE_POOL_HPP

#include <memory>
#include <optional>

#include <solv/pooltypes.h>

#include "mamba/core/package_info.hpp"
#include "mamba/core/repo.hpp"

namespace mamba
{
    class MatchSpec;

    /**
     * Pool of solvable involved in resolving en environment.
     *
     * The pool contains the solvable (packages) information required from the ``MSolver``.
     * The pool can be reused by multiple solvers to solve differents requirements with the same
     * ecosystem.
     * Behaves like a ``std::shared_ptr``, meaning ressources are shared on copy.
     */
    class MPool
    {
    public:

        MPool();
        ~MPool();

        std::size_t n_solvables() const;

        void set_debuglevel();
        void create_whatprovides();

        std::vector<Id> select_solvables(Id id, bool sorted = false) const;
        Id matchspec2id(const MatchSpec& ms);

        std::optional<PackageInfo> id2pkginfo(Id solv_id) const;
        std::optional<std::string> dep2str(Id dep_id) const;

        operator Pool*();
        operator const Pool*() const;

        MRepo& add_repo(MRepo&& repo);
        void remove_repo(Id repo_id);

    private:

        struct MPoolData;

        Pool* pool();
        const Pool* pool() const;

        /**
         * Make MPool behave like a shared_ptr (with move and copy).
         *
         * The pool is passed to the ``MSolver`` for its lifetime but the pool can legitimely
         * be reused with different solvers (in fact it is in ``conda-forge``'s ``regro-bot``
         * and ``boa``).
         * An alternative considered was to make ``MPool`` a move-only type, moving it in and out
         * of ``MSolver`` (effectively borrowing).
         * It was decided to make ``MPool`` share ressources for the following reasons.
         *    - Rvalue semantics would be unexpected in Python (and breaking ``conda-forge``);
         *    - Facilitate (potential) future investigation of parallel solves.
         */
        std::shared_ptr<MPoolData> m_data;
    };
}  // namespace mamba

#endif  // MAMBA_POOL_HPP
