using System.Reflection;

namespace Mbproxy.Admin;

/// <summary>
/// Reads <see cref="AssemblyInformationalVersionAttribute"/> once at startup and caches the
/// result as a string. Used for the <c>service.version</c> field on the status page.
///
/// <para>Note: <see cref="Assembly.Location"/> is unreliable under single-file publish.
/// We use <c>Assembly.GetExecutingAssembly().GetCustomAttribute&lt;&gt;()</c> which works
/// correctly regardless of publish mode.</para>
/// </summary>
internal sealed class AssemblyVersionAccessor
{
    /// <summary>
    /// The cached informational version string, e.g. <c>"1.2.3+gitsha"</c>.
    /// Falls back to <c>"0.0.0"</c> when the attribute is absent (e.g., unit-test host).
    /// </summary>
    public string Version { get; } =
        Assembly.GetExecutingAssembly()
            .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
            ?.InformationalVersion
        ?? "0.0.0";
}
