using System.Text.Json;

namespace ScadaLink.SiteRuntime.Scripts;

/// <summary>
/// Read-only globals a trigger expression is compiled against. Exposes only
/// attribute reads, backed by an in-memory snapshot — no I/O, no actor Ask,
/// no side-effecting APIs. A missing attribute key reads as <c>null</c> and
/// never throws.
///
/// Canonical attribute keys are dotted (e.g. "TempSensor.Reading"); the prefix
/// logic here mirrors <see cref="AttributeAccessor.Resolve"/>.
/// </summary>
public sealed class TriggerExpressionGlobals
{
    /// <summary>
    /// Extracts the <c>"expression"</c> field from an Expression-trigger config
    /// JSON document. Returns <c>null</c> for a missing, blank, or malformed
    /// config — the single parsing idiom shared by InstanceActor, ScriptActor,
    /// and AlarmActor.
    /// </summary>
    public static string? ExtractExpression(string? triggerConfigJson)
    {
        if (string.IsNullOrEmpty(triggerConfigJson)) return null;
        try
        {
            using var doc = JsonDocument.Parse(triggerConfigJson);
            var expr = doc.RootElement.TryGetProperty("expression", out var e)
                ? e.GetString()
                : null;
            return string.IsNullOrWhiteSpace(expr) ? null : expr;
        }
        catch (JsonException)
        {
            return null;
        }
    }

    private readonly IReadOnlyDictionary<string, object?> _snapshot;

    public TriggerExpressionGlobals(IReadOnlyDictionary<string, object?> snapshot)
        => _snapshot = snapshot;

    /// <summary>Attributes in the expression's own scope (root prefix).</summary>
    public ReadOnlyAttributes Attributes => new(_snapshot, "");

    /// <summary>Indexed access to child compositions' attributes.</summary>
    public ReadOnlyChildren Children => new(_snapshot);

    /// <summary>
    /// Parent composition (null at root). Set by the caller for derived/composed
    /// scopes; the runtime actors evaluate at root scope, so this stays null.
    /// </summary>
    public ReadOnlyComposition? Parent { get; init; }

    /// <summary>
    /// Read-only attribute view anchored at a canonical-name prefix. Indexing
    /// resolves to the canonical key ("" → key, "TempSensor" → "TempSensor.key").
    /// </summary>
    public sealed class ReadOnlyAttributes
    {
        private readonly IReadOnlyDictionary<string, object?> _s;
        private readonly string _prefix;

        public ReadOnlyAttributes(IReadOnlyDictionary<string, object?> s, string prefix)
        {
            _s = s;
            _prefix = prefix;
        }

        public object? this[string key] =>
            _s.TryGetValue(_prefix.Length == 0 ? key : _prefix + "." + key, out var v) ? v : null;
    }

    /// <summary>A read-only view of one composition at a canonical-name path.</summary>
    public sealed class ReadOnlyComposition
    {
        private readonly IReadOnlyDictionary<string, object?> _s;
        private readonly string _path;

        public ReadOnlyComposition(IReadOnlyDictionary<string, object?> s, string path)
        {
            _s = s;
            _path = path;
        }

        public ReadOnlyAttributes Attributes => new(_s, _path);
    }

    /// <summary>Dictionary-style accessor for child compositions.</summary>
    public sealed class ReadOnlyChildren
    {
        private readonly IReadOnlyDictionary<string, object?> _s;

        public ReadOnlyChildren(IReadOnlyDictionary<string, object?> s) => _s = s;

        public ReadOnlyComposition this[string compositionName] => new(_s, compositionName);
    }
}
