#pragma once

#include <shared_mutex>
#include <unordered_map>
#include <optional>
#include <vector>
#include "../../../../utils/string.hpp"
#include "../types.hpp"

namespace lsp::service::symbol::repository
{
    class Workspace
    {
    public:
        Workspace() = default;
        ~Workspace() = default;

        Workspace(const Workspace&) = delete;
        Workspace& operator=(const Workspace&) = delete;

        void Add(const FileSymbolIndex& index);
        void Remove(const std::string& file_path);
        void Clear();
        size_t Count() const;
        bool Contains(const std::string& file_path) const;

        std::optional<FileSymbolIndex> FindUnit(const std::string& name) const;
        std::optional<FileSymbolIndex> FindClass(const std::string& name) const;
        std::optional<FileSymbolIndex> FindFunction(const std::string& name) const;
        std::optional<FileSymbolIndex> FindByPath(const std::string& file_path) const;

        std::vector<FileSymbolIndex> Units() const;
        std::vector<FileSymbolIndex> Classes() const;
        std::vector<FileSymbolIndex> Functions() const;
        std::vector<FileSymbolIndex> AllFiles() const;

    private:
        mutable std::shared_mutex mutex_;

        std::unordered_map<std::string, FileSymbolIndex, lsp::utils::IHasher, lsp::utils::IEqualTo> units_;
        std::unordered_map<std::string, FileSymbolIndex, lsp::utils::IHasher, lsp::utils::IEqualTo> classes_;
        std::unordered_map<std::string, FileSymbolIndex, lsp::utils::IHasher, lsp::utils::IEqualTo> functions_;

        // 路径索引（用于快速按路径查找）
        std::unordered_map<std::string, std::string> path_to_symbol_;
    };
}
