namespace NeonSprawl.Server.Diagnostics;

/// <summary>Optional one-line stdout traces for prototype HTTP routes (manual QA / dev until E9.M1).</summary>
/// <remarks>
/// Enable with environment variable <c>NEON_SPRAWL_API_LOG</c> set to <c>1</c>, <c>true</c>, <c>yes</c>, or <c>on</c> (case-insensitive).
/// </remarks>
public static class PrototypeApiConsoleLog
{
    /// <summary>Slice 3 hook names; matches product telemetry vocabulary in <c>AbilityCastApi</c> comments.</summary>
    public static bool IsAbilityCastStdoutEnabled()
    {
        var raw = Environment.GetEnvironmentVariable("NEON_SPRAWL_API_LOG");
        if (string.IsNullOrWhiteSpace(raw))
        {
            return false;
        }

        var v = raw.Trim();
        return v is "1" or "true" or "yes"
            || string.Equals(v, "on", StringComparison.OrdinalIgnoreCase);
    }

    /// <summary>One line per JSON ability-cast response (HTTP 200 with <c>AbilityCastResponse</c>).</summary>
    public static void WriteAbilityCastLine(
        string playerId,
        int slotIndex,
        string? abilityId,
        string? targetId,
        bool accepted,
        string? reasonCode)
    {
        if (!IsAbilityCastStdoutEnabled())
        {
            return;
        }

        if (accepted)
        {
            Console.WriteLine(
                "[ability-cast] ability_cast_requested player={0} slot={1} ability={2} target={3}",
                playerId,
                slotIndex,
                abilityId ?? "",
                targetId ?? "");
        }
        else
        {
            Console.WriteLine(
                "[ability-cast] ability_cast_denied player={0} slot={1} reason={2}",
                playerId,
                slotIndex,
                reasonCode ?? "");
        }
    }
}
