using System.Collections.Frozen;

namespace ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;

/// <summary>
/// Bidirectional name registry for management command records. The registry contains
/// exactly the non-abstract <c>*Command</c> types declared in the
/// <c>ZB.MOM.WW.ScadaBridge.Commons.Messages.Management</c> namespace; these are the commands that
/// travel over the HTTP management boundary.
/// </summary>
/// <remarks>
/// <see cref="Resolve"/> and <see cref="GetCommandName"/> are symmetric:
/// <c>Resolve(GetCommandName(t))</c> returns <c>t</c> for every type
/// <see cref="GetCommandName"/> accepts. <see cref="GetCommandName"/> rejects any type
/// the registry does not contain rather than computing an unresolvable name.
/// </remarks>
public static class ManagementCommandRegistry
{
    private static readonly FrozenDictionary<string, Type> Commands = BuildRegistry();

    /// <summary>
    /// Names keyed by command type, for the reverse lookup. Keeps
    /// <see cref="GetCommandName"/> in lock-step with the forward registry.
    /// </summary>
    private static readonly FrozenDictionary<Type, string> NamesByType =
        Commands.ToFrozenDictionary(kv => kv.Value, kv => kv.Key);

    /// <summary>
    /// Resolves a management command wire name to its CLR type, or null if not registered.
    /// </summary>
    /// <param name="commandName">The wire name of the management command (without the "Command" suffix).</param>
    /// <returns>The CLR <see cref="Type"/> for the command, or <c>null</c> if not registered.</returns>
    public static Type? Resolve(string commandName)
    {
        return Commands.GetValueOrDefault(commandName);
    }

    /// <summary>
    /// Returns the registered wire name for a management command type.
    /// </summary>
    /// <param name="commandType">The CLR type of the management command to look up.</param>
    /// <exception cref="ArgumentException">
    /// Thrown when <paramref name="commandType"/> is not a registered management
    /// command — i.e. not a non-abstract <c>*Command</c> type in the
    /// <c>ZB.MOM.WW.ScadaBridge.Commons.Messages.Management</c> namespace. This keeps the method
    /// symmetric with <see cref="Resolve"/>: it never yields a name that
    /// <see cref="Resolve"/> cannot turn back into the same type.
    /// </exception>
    /// <returns>The registered wire name for <paramref name="commandType"/>.</returns>
    public static string GetCommandName(Type commandType)
    {
        ArgumentNullException.ThrowIfNull(commandType);

        if (NamesByType.TryGetValue(commandType, out var name))
            return name;

        throw new ArgumentException(
            $"'{commandType.FullName}' is not a registered management command. " +
            $"Management commands must be non-abstract '*Command' records declared in " +
            $"the '{typeof(ManagementEnvelope).Namespace}' namespace.",
            nameof(commandType));
    }

    private static FrozenDictionary<string, Type> BuildRegistry()
    {
        var ns = typeof(ManagementEnvelope).Namespace!;
        var assembly = typeof(ManagementEnvelope).Assembly;

        return assembly.GetTypes()
            .Where(t => t.Namespace == ns
                && t.Name.EndsWith("Command", StringComparison.Ordinal)
                && !t.IsAbstract)
            .ToFrozenDictionary(
                t => t.Name[..^"Command".Length],
                t => t,
                StringComparer.OrdinalIgnoreCase);
    }
}
