/*
 * Copyright (C) 2025 Dominik Drexler
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef TYR_PLANNING_GROUND_TASK_STATE_REPOSITORY_HPP_
#define TYR_PLANNING_GROUND_TASK_STATE_REPOSITORY_HPP_

#include "tyr/common/bit_packed_layout.hpp"   // for BitPackedArra...
#include "tyr/common/config.hpp"              // for uint_t, float_t
#include "tyr/common/indexed_hash_set.hpp"    // for IndexedHashSet
#include "tyr/common/raw_array_set.hpp"       // for SegmentedArra...
#include "tyr/common/shared_object_pool.hpp"  // for SharedObjectPool
#include "tyr/planning/declarations.hpp"      // for GroundTask
#include "tyr/planning/ground_task/state_data.hpp"
#include "tyr/planning/ground_task/state_view.hpp"
#include "tyr/planning/ground_task/unpacked_state.hpp"
#include "tyr/planning/state_index.hpp"

#include <memory>  // for shared_ptr
#include <valla/valla.hpp>
#include <vector>
#include <vector>  // for vector

namespace tyr::planning
{

template<>
class StateRepository<GroundTask> : public std::enable_shared_from_this<StateRepository<GroundTask>>
{
public:
    explicit StateRepository(std::shared_ptr<GroundTask> task);

    static std::shared_ptr<StateRepository<GroundTask>> create(std::shared_ptr<GroundTask> task);

    StateView<GroundTask> get_initial_state();

    StateView<GroundTask> get_registered_state(Index<State<GroundTask>> state_index);

    StateView<GroundTask>
    create_state(const std::vector<Data<formalism::planning::FDRFact<formalism::FluentTag>>>& fluent_facts,
                 const std::vector<std::pair<Index<formalism::planning::GroundFunctionTerm<formalism::FluentTag>>, float_t>>& fterm_values);

    StateView<GroundTask> create_state(const std::vector<formalism::planning::FDRFactView<formalism::FluentTag>>& fluent_facts,
                                       const std::vector<formalism::planning::GroundFunctionTermViewValuePair<formalism::FluentTag>>& fterm_values);

    SharedObjectPoolPtr<UnpackedState<GroundTask>> get_unregistered_state();

    StateView<GroundTask> register_state(SharedObjectPoolPtr<UnpackedState<GroundTask>> state);

    const auto& get_task() const noexcept { return m_task; }
    const auto& get_axiom_evaluator() const noexcept { return m_axiom_evaluator; }

private:
    std::shared_ptr<GroundTask> m_task;
    BitPackedArrayLayout<uint_t> m_fluent_layout;
    BitsetLayout<uint_t> m_derived_layout;

    valla::IndexedHashSet<valla::Slot<uint_t>, uint_t> m_uint_nodes;
    valla::IndexedHashSet<float_t, uint_t> m_float_nodes;
    std::vector<uint_t> m_nodes_buffer;
    IndexedHashSet<State<GroundTask>> m_packed_states;
    RawArraySet<uint_t> m_fluent_repository;
    RawArraySet<uint_t> m_derived_repository;
    std::vector<uint_t> m_fluent_buffer;
    std::vector<uint_t> m_derived_buffer;
    SharedObjectPool<UnpackedState<GroundTask>> m_unpacked_state_pool;

    std::shared_ptr<AxiomEvaluator<GroundTask>> m_axiom_evaluator;
};

}

#endif