using System.Collections.ObjectModel;

namespace NeonSprawl.Server.Game.Gathering;

/// <summary>In-memory resource-node catalog loaded at startup (NEO-58). Game callers should use <see cref="IResourceNodeDefinitionRegistry"/>.</summary>
public sealed class ResourceNodeCatalog(
    string resourceNodesDirectory,
    IReadOnlyDictionary<string, ResourceNodeDefRow> nodesById,
    IReadOnlyDictionary<string, ResourceYieldRow> yieldsByNodeDefId,
    int nodeCatalogJsonFileCount,
    int yieldCatalogJsonFileCount)
{
    /// <summary>Absolute path to the directory enumerated for <c>*_resource_nodes.json</c> / <c>*_resource_yields.json</c>.</summary>
    public string ResourceNodesDirectory { get; } = resourceNodesDirectory;

    public IReadOnlyDictionary<string, ResourceNodeDefRow> NodesById { get; } =
        new ReadOnlyDictionary<string, ResourceNodeDefRow>(new Dictionary<string, ResourceNodeDefRow>(nodesById, StringComparer.Ordinal));

    public IReadOnlyDictionary<string, ResourceYieldRow> YieldsByNodeDefId { get; } =
        new ReadOnlyDictionary<string, ResourceYieldRow>(new Dictionary<string, ResourceYieldRow>(yieldsByNodeDefId, StringComparer.Ordinal));

    public int DistinctNodeCount => NodesById.Count;

    public int DistinctYieldCount => YieldsByNodeDefId.Count;

    /// <summary>Number of <c>*_resource_nodes.json</c> files under <see cref="ResourceNodesDirectory"/>.</summary>
    public int NodeCatalogJsonFileCount { get; } = nodeCatalogJsonFileCount;

    /// <summary>Number of <c>*_resource_yields.json</c> files under <see cref="ResourceNodesDirectory"/>.</summary>
    public int YieldCatalogJsonFileCount { get; } = yieldCatalogJsonFileCount;

    /// <summary>Resolves a node def by stable <paramref name="nodeDefId"/>.</summary>
    public bool TryGetNode(string nodeDefId, out ResourceNodeDefRow? row) =>
        NodesById.TryGetValue(nodeDefId, out row);

    /// <summary>Resolves the fixed yield row for <paramref name="nodeDefId"/>.</summary>
    public bool TryGetYield(string nodeDefId, out ResourceYieldRow? row) =>
        YieldsByNodeDefId.TryGetValue(nodeDefId, out row);
}
