namespace ClaudeDo.Data.Models;

public static class ModelRegistry
{
    public static readonly IReadOnlyList<string> Aliases = new[] { "sonnet", "opus", "haiku", "fable" };

    /// <summary>Model aliases ordered cheapest → most capable. Single source for prompt cost guidance.</summary>
    public static readonly IReadOnlyList<string> ByCostAscending = new[] { "haiku", "sonnet", "opus", "fable" };

    public const string DefaultAlias = "sonnet";
    public const string PlanningAlias = "opus";
    public const string HandlerTriageAlias = "opus";
    public const string HandlerWaitAlias = "sonnet";
    public const string HandlerMergeAlias = "opus";

    public const string ListDefaultSentinel = "(default)";
    public const string TaskInheritSentinel = "(inherit)";

    /// <summary>
    /// Validate a model alias from external input. Null/blank → null (inherit).
    /// Returns the canonical lowercase alias; throws on an unknown value.
    /// </summary>
    public static string? NormalizeAlias(string? model)
    {
        var m = model?.Trim();
        if (string.IsNullOrEmpty(m)) return null;
        foreach (var alias in Aliases)
            if (string.Equals(alias, m, StringComparison.OrdinalIgnoreCase))
                return alias;
        throw new ArgumentException($"Unknown model '{model}'. Allowed: {string.Join(", ", Aliases)}.");
    }

    /// <summary>
    /// Best-effort alias lookup for run-path model resolution, where a full CLI model id
    /// (e.g. "claude-sonnet-4-6") is just as likely as a bare alias and a run must never be
    /// blocked by an unrecognized one. Null/blank/unrecognized → null; never throws.
    /// </summary>
    public static string? TryNormalizeAlias(string? model)
    {
        var m = model?.Trim();
        if (string.IsNullOrEmpty(m)) return null;
        foreach (var alias in Aliases)
            if (string.Equals(alias, m, StringComparison.OrdinalIgnoreCase))
                return alias;
        foreach (var alias in Aliases)
            if (m.Contains(alias, StringComparison.OrdinalIgnoreCase))
                return alias;
        return null;
    }
}
