using System.Collections.Concurrent;
using Hc900Crawler.Core.Application.Feedforward;

namespace Hc900Crawler.Infrastructure.Control;

/// <summary>
/// WP0 Sim Override — 싱글톤 in-memory 입력 치환. advisor가 읽는 태그값만 대체(제어/DB 쓰기 아님).
/// thread-safety: 싱글톤·동시 HTTP → ConcurrentDictionary + volatile bool (ARM64 약한 메모리 모델 대비).
/// 주의: per-key 안전만 보장(SetMany/Snapshot 그룹 원자성 X) — 순차 테스트 사용 전제.
/// </summary>
public sealed class SimOverrideStore : ISimOverrideStore
{
    private volatile bool _enabled;
    private readonly ConcurrentDictionary<string, double> _values = new();

    public bool Enabled => _enabled;

    public void SetMany(bool enabled, IReadOnlyDictionary<string, double> values)
    {
        foreach (var kv in values) _values[kv.Key] = kv.Value;
        _enabled = enabled;
    }

    public void Clear()
    {
        _values.Clear();
        _enabled = false;
    }

    public bool TryGet(string tag, out double value) => _values.TryGetValue(tag, out value);

    public IReadOnlyDictionary<string, double> Snapshot() => new Dictionary<string, double>(_values);
}
