namespace Theriapolis.Core.Data;

/// <summary>
/// Pre-loaded content lookup tables. Constructing one calls every loader
/// exactly once and indexes results by id, so subsequent
/// <c>resolver.Clades["canidae"]</c> lookups are O(1).
///
/// Used by character creation, save/load (id → def resolution), and Phase 5 M5
/// NPC instantiation. Shared across screens that need any combination of
/// these tables.
/// </summary>
public sealed class ContentResolver
{
    public IReadOnlyDictionary<string, CladeDef>      Clades      { get; }
    public IReadOnlyDictionary<string, SpeciesDef>    Species     { get; }
    public IReadOnlyDictionary<string, ClassDef>      Classes     { get; }
    public IReadOnlyDictionary<string, SubclassDef>   Subclasses  { get; }
    public IReadOnlyDictionary<string, BackgroundDef> Backgrounds { get; }
    public IReadOnlyDictionary<string, ItemDef>       Items       { get; }
    public NpcTemplateContent                         Npcs        { get; }

    public ContentResolver(ContentLoader loader)
    {
        var clades = loader.LoadClades();
        Clades      = clades.ToDictionary(c => c.Id, StringComparer.OrdinalIgnoreCase);
        Species     = loader.LoadSpecies(clades).ToDictionary(s => s.Id, StringComparer.OrdinalIgnoreCase);
        var classes = loader.LoadClasses();
        Classes     = classes.ToDictionary(c => c.Id, StringComparer.OrdinalIgnoreCase);
        Subclasses  = loader.LoadSubclasses(classes).ToDictionary(s => s.Id, StringComparer.OrdinalIgnoreCase);
        Backgrounds = loader.LoadBackgrounds().ToDictionary(b => b.Id, StringComparer.OrdinalIgnoreCase);
        var items   = loader.LoadItems();
        Items       = items.ToDictionary(i => i.Id, StringComparer.OrdinalIgnoreCase);
        Npcs        = loader.LoadNpcTemplates(items);
    }
}
