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

namespace Hc900Crawler.Infrastructure.Control;

/// <summary>
/// WP5 3단계: 조성 분율 수동입력 store (랩/원료분석값). 싱글톤 in-memory.
/// key = "{columnId}:{streamKey}" → 중비물/경비물 분율. 미설정 시 호출측이 config K로 폴백.
/// thread-safe(ConcurrentDictionary).
/// </summary>
public sealed class CompositionStore : ICompositionStore
{
    private readonly ConcurrentDictionary<string, double> _frac = new();

    private static string K(int columnId, string streamKey) => $"{columnId}:{streamKey.ToUpperInvariant()}";

    public void Set(int columnId, string streamKey, double fraction) => _frac[K(columnId, streamKey)] = fraction;
    public void Clear(int columnId, string streamKey) => _frac.TryRemove(K(columnId, streamKey), out _);
    public bool TryGet(int columnId, string streamKey, out double fraction) => _frac.TryGetValue(K(columnId, streamKey), out fraction);
    public IReadOnlyDictionary<string, double> Snapshot() => new Dictionary<string, double>(_frac);
}
