using System.Text.Json;
using System.Text.Json.Serialization;
namespace Chronos.Core.Project;
///
/// A parameter the advisor can decide (BRIEF ยง10.4). In Auto the advisor's applies and
/// / explain it; touching the control stores a manual
/// value; reset returns to Auto.
///
public sealed record Auto
{
public bool IsAuto { get; init; } = true;
/// The advisor's value; meaningful only when .
public T? AutoValue { get; init; }
/// True once the advisor stored a value. Needed because a value type's is never
/// null: without it "no advice" and an advised 0 or false look the same.
public bool HasAdvice { get; init; }
/// The user's value when is false.
public T? Manual { get; init; }
/// Localization key of the reason shown in the Auto pill tooltip.
public string? ReasonKey { get; init; }
public IReadOnlyList? ReasonArgs { get; init; }
///
/// Members this Chronos does not know (written by a newer one, such as the advisor's confidence): kept as they are, so
/// they survive a round trip and every edit of the parameter (owner review 2, R8).
///
[JsonExtensionData]
public Dictionary? Extra { get; set; }
/// The value in force: manual when set by the user, otherwise the advisor's, otherwise .
public T Resolve(T fallback) => !IsAuto && Manual is not null ? Manual : HasAdvice && AutoValue is not null ? AutoValue : fallback;
public Auto WithManual(T value) => this with { IsAuto = false, Manual = value };
public Auto Reset() => this with { IsAuto = true, Manual = default };
public Auto WithAdvice(T value, string? reasonKey, params string[] args) => this with { AutoValue = value, HasAdvice = true, ReasonKey = reasonKey, ReasonArgs = args };
}