using System.Collections.Concurrent;
using System.Security.Cryptography;
using System.Text;

namespace ZB.MOM.WW.OtOpcUa.Core.Scripting;

/// <summary>
///     Source-hash-keyed compile cache for user scripts. Roslyn compilation is the most
///     expensive step in the evaluator pipeline (5-20ms per script depending on size);
///     re-compiling on every value-change event would starve the virtual-tag engine.
///     The cache is generic on the <see cref="ScriptContext"/> subclass + result type so
///     different engines (virtual-tag / alarm-predicate / future alarm-action) each get
///     their own cache instance — there's no cross-type pollution.
/// </summary>
/// <remarks>
///     <para>
///         Concurrent-safe: <see cref="ConcurrentDictionary{TKey, TValue}"/> of
///         <see cref="Lazy{T}"/> means a miss on two threads compiles exactly once.
///         <see cref="LazyThreadSafetyMode.ExecutionAndPublication"/> guarantees other
///         threads block on the in-flight compile rather than racing to duplicate work.
///     </para>
///     <para>
///         Cache is keyed on SHA-256 of the UTF-8 bytes of the source — collision-free in
///         practice. Whitespace changes therefore miss the cache on purpose; operators
///         see re-compile time on their first evaluation after a format-only edit which
///         is rare and benign.
///     </para>
///     <para>
///         No capacity bound. Virtual-tag + alarm scripts are operator-authored and
///         bounded by config DB (typically low thousands). If that changes in v3, add an
///         LRU eviction policy — the API stays the same.
///     </para>
/// </remarks>
public sealed class CompiledScriptCache<TContext, TResult>
    where TContext : ScriptContext
{
    private readonly ConcurrentDictionary<string, Lazy<ScriptEvaluator<TContext, TResult>>> _cache = new();

    /// <summary>
    ///     Return the compiled evaluator for <paramref name="scriptSource"/>, compiling
    ///     on first sight + reusing thereafter. If the source fails to compile, the
    ///     original Roslyn / sandbox exception propagates; the cache entry is removed so
    ///     the next call retries (useful during Admin UI authoring when the operator is
    ///     still fixing syntax).
    /// </summary>
    public ScriptEvaluator<TContext, TResult> GetOrCompile(string scriptSource)
    {
        if (scriptSource is null) throw new ArgumentNullException(nameof(scriptSource));

        var key = HashSource(scriptSource);
        var lazy = _cache.GetOrAdd(key, _ => new Lazy<ScriptEvaluator<TContext, TResult>>(
            () => ScriptEvaluator<TContext, TResult>.Compile(scriptSource),
            LazyThreadSafetyMode.ExecutionAndPublication));

        try
        {
            return lazy.Value;
        }
        catch
        {
            // Failed compile — evict so a retry with corrected source can succeed.
            _cache.TryRemove(key, out _);
            throw;
        }
    }

    /// <summary>Current entry count. Exposed for Admin UI diagnostics / tests.</summary>
    public int Count => _cache.Count;

    /// <summary>Drop every cached compile. Used on config generation publish + tests.</summary>
    public void Clear() => _cache.Clear();

    /// <summary>True when the exact source has been compiled at least once + is still cached.</summary>
    public bool Contains(string scriptSource)
        => _cache.ContainsKey(HashSource(scriptSource));

    private static string HashSource(string source)
    {
        var bytes = Encoding.UTF8.GetBytes(source);
        var hash = SHA256.HashData(bytes);
        return Convert.ToHexString(hash);
    }
}
