using System.Text.Json.Serialization;

namespace Hc900Crawler.Core.Application.Feedforward;

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum FeedRampState { Ramping, Hold, Reached, Canceled }

/// <summary>
/// 작업 B: FEED 램프 실행 작업 상태. 컬럼당 1개. FeedRampExecutorService가 단계마다 갱신.
/// LastWrittenSp = 마지막으로 컨트롤러에 쓴(또는 DryRun에서 쓸 예정이던) FEED SP.
/// </summary>
public sealed record FeedRampJob
{
    public int           ColumnId      { get; init; }
    public double        TargetFeed    { get; init; }
    public double        LastWrittenSp { get; init; } = double.NaN;  // NaN=아직 첫 단계 전(앵커 미설정)
    public FeedRampState State         { get; init; } = FeedRampState.Ramping;
    public string?       Hold          { get; init; }                // Hold 사유(피드 불량 등)
    public double?       CurrentFeed   { get; init; }
    public double?       Ceiling       { get; init; }
    public double?       RampRate      { get; init; }                // kg/hr·min
    public DateTime      StartedAt     { get; init; }
    public DateTime?     LastStepAt    { get; init; }
    public string        Operator      { get; init; } = "manual";
    public bool          DryRun        { get; init; }
    public IReadOnlyList<string> Warnings { get; init; } = Array.Empty<string>();
}

public interface IFeedRampJobStore
{
    FeedRampJob Start(int columnId, double targetFeed, string op, bool dryRun);
    FeedRampJob? Get(int columnId);
    IReadOnlyCollection<FeedRampJob> GetAll();
    void Update(FeedRampJob job);
    bool Cancel(int columnId);
}
