#pragma once

#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

namespace Resource {

    // Registre lleuger d'assets carregat des de `data/config/assets.yaml`.
    // Map<basename → Item> per a lookup O(1). Cache l'utilitza per a
    // iterar per categoria a l'hora de carregar.
    class List {
       public:
        enum class Type : std::uint8_t {
            DATA,
            BITMAP,
            MUSIC,
            SOUND,
            FONT,
            SIZE,
        };

        static void init(const std::string& yaml_path);
        static void destroy();
        static auto get() -> List*;

        ~List() = default;
        List(const List&) = delete;
        auto operator=(const List&) -> List& = delete;

        [[nodiscard]] auto get(const std::string& filename) const -> std::string;
        [[nodiscard]] auto getListByType(Type type) const -> std::vector<std::string>;
        [[nodiscard]] auto exists(const std::string& filename) const -> bool;
        [[nodiscard]] auto totalCount() const -> int { return static_cast<int>(file_list_.size()); }

       private:
        struct Item {
            std::string path;  // ruta relativa al pack (ex: "music/menu.ogg")
            Type type;

            Item(std::string p, Type t)
                : path(std::move(p)),
                  type(t) {}
        };

        List() = default;

        void loadFromYaml(const std::string& yaml_path);
        void loadFromString(const std::string& yaml_content);
        void addToMap(const std::string& path, Type type);

        [[nodiscard]] static auto parseAssetType(const std::string& type_str) -> Type;
        [[nodiscard]] static auto basename(const std::string& path) -> std::string;

        std::unordered_map<std::string, Item> file_list_;

        static std::unique_ptr<List> instance;
    };

}  // namespace Resource
