using Theriapolis.Core.Util;

namespace Theriapolis.Core.World.Generation.Stages;

/// <summary>
/// Stage 1 — SeedInit
/// Builds the named RNG sub-stream table from the world seed.
/// All downstream stages consume their sub-stream from ctx.Rngs["name"].
/// </summary>
public sealed class SeedInitStage : IWorldGenStage
{
    public string Name => "SeedInit";

    public void Run(WorldGenContext ctx)
    {
        ulong seed = ctx.World.WorldSeed;

        ctx.Rngs["terrain"]     = SeededRng.ForSubsystem(seed, C.RNG_TERRAIN);
        ctx.Rngs["moisture"]    = SeededRng.ForSubsystem(seed, C.RNG_MOISTURE);
        ctx.Rngs["temp"]        = SeededRng.ForSubsystem(seed, C.RNG_TEMP);
        ctx.Rngs["border"]      = SeededRng.ForSubsystem(seed, C.RNG_BORDER);
        ctx.Rngs["coast"]       = SeededRng.ForSubsystem(seed, C.RNG_COAST);
        ctx.Rngs["hydro"]       = SeededRng.ForSubsystem(seed, C.RNG_HYDRO);
        ctx.Rngs["settle"]      = SeededRng.ForSubsystem(seed, C.RNG_SETTLE);
        ctx.Rngs["road"]        = SeededRng.ForSubsystem(seed, C.RNG_ROAD);
        ctx.Rngs["rail"]        = SeededRng.ForSubsystem(seed, C.RNG_RAIL);
        ctx.Rngs["faction"]     = SeededRng.ForSubsystem(seed, C.RNG_FACTION);
        ctx.Rngs["poi"]         = SeededRng.ForSubsystem(seed, C.RNG_POI);
        ctx.Rngs["weather"]     = SeededRng.ForSubsystem(seed, C.RNG_WEATHER);
        ctx.Rngs["tactical"]    = SeededRng.ForSubsystem(seed, C.RNG_TACTICAL);
        ctx.Rngs["lake"]        = SeededRng.ForSubsystem(seed, C.RNG_LAKE);
        ctx.Rngs["meander"]     = SeededRng.ForSubsystem(seed, C.RNG_MEANDER);
        ctx.Rngs["habitat"]     = SeededRng.ForSubsystem(seed, C.RNG_HABITAT);
        ctx.Rngs["anchor"]      = SeededRng.ForSubsystem(seed, C.RNG_ANCHOR);
        ctx.Rngs["settle_attr"] = SeededRng.ForSubsystem(seed, C.RNG_SETTLE_ATTR);
        ctx.Rngs["trade"]       = SeededRng.ForSubsystem(seed, C.RNG_TRADE);
        ctx.Rngs["encounter"]   = SeededRng.ForSubsystem(seed, C.RNG_ENCOUNTER);

        ctx.World.StageHashes["SeedInit"] = seed;
        ctx.LogMessage($"[SeedInit] RNG streams initialised. World seed: 0x{seed:X16}");
    }
}
