namespace ZB.MOM.WW.Telemetry;

/// <summary>
/// Eager, fail-fast validation of <see cref="ZbTelemetryOptions"/> shared by the core
/// <c>AddZbTelemetry</c> path and the Serilog <c>AddZbSerilog</c> path, so a malformed value is
/// reported once, clearly, and with the offending option named — rather than surfacing late as a
/// bare <see cref="UriFormatException"/> deep inside exporter construction at host-build time.
/// </summary>
internal static class ZbTelemetryOptionsValidator
{
    /// <summary>
    /// Validates the OTLP configuration. When <see cref="ZbTelemetryOptions.Exporter"/> is
    /// <see cref="ZbExporter.Otlp"/>, <see cref="ZbTelemetryOptions.OtlpEndpoint"/> must be a
    /// non-empty, well-formed absolute URI. Throws an <see cref="ArgumentException"/> (naming the
    /// option) otherwise. No-op for the Prometheus exporter — a stray endpoint is ignored there.
    /// </summary>
    /// <param name="options">The populated telemetry options to validate.</param>
    /// <param name="paramName">The originating parameter name for the thrown exception.</param>
    public static void Validate(ZbTelemetryOptions options, string paramName)
    {
        ArgumentNullException.ThrowIfNull(options);

        if (options.Exporter != ZbExporter.Otlp)
        {
            return;
        }

        if (string.IsNullOrWhiteSpace(options.OtlpEndpoint))
        {
            throw new ArgumentException(
                "ZbTelemetryOptions.OtlpEndpoint is required when Exporter = ZbExporter.Otlp " +
                "(e.g. \"http://collector:4317\").",
                paramName);
        }

        if (!Uri.TryCreate(options.OtlpEndpoint, UriKind.Absolute, out _))
        {
            throw new ArgumentException(
                $"ZbTelemetryOptions.OtlpEndpoint is not a well-formed absolute URI: " +
                $"'{options.OtlpEndpoint}' (expected e.g. \"http://collector:4317\").",
                paramName);
        }
    }
}
