using Hc900Crawler.Core.Application.Feedforward;

namespace Hc900Crawler.Infrastructure.Control;

public sealed class FrontPositionIndicator
{
    private readonly double _bandwidth;
    private readonly FirstOrderLag _baseline = new();

    public FrontPositionIndicator(double bandwidth) => _bandwidth = Math.Max(1e-9, bandwidth);

    public (string state, string? trimAdvice, Confidence grade) Update(
        double frontMetric, double tsSec, double refTauSec, bool strongSignal)
    {
        double bl = _baseline.Step(frontMetric, refTauSec, tsSec);
        double dev = frontMetric - bl;
        Confidence grade = strongSignal ? Confidence.B : Confidence.C;

        if (Math.Abs(dev) <= _bandwidth)
            return ("정상(프론트 안정)", null, grade);
        if (dev > 0)
            return ("프론트 상승(경비물 혼입 위험)", "환류↑ 권장", grade);
        return ("프론트 하강", "boilup↑·환류↓ 권장", grade);
    }
}
