using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System.Reflection;

namespace MyNewProjectName.Infrastructure.Extensions;

/// <summary>
/// Extension methods for configuring OpenTelemetry
/// </summary>
public static class OpenTelemetryExtensions
{
    /// <summary>
    /// Add OpenTelemetry distributed tracing
    /// </summary>
    public static IServiceCollection AddOpenTelemetryTracing(
        this IServiceCollection services,
        string serviceName = "MyNewProjectName")
    {
        services.AddOpenTelemetry()
            .WithTracing(builder =>
            {
                builder
                    .SetResourceBuilder(ResourceBuilder
                        .CreateDefault()
                        .AddService(serviceName)
                        .AddAttributes(new Dictionary<string, object>
                        {
                            ["service.version"] = Assembly.GetExecutingAssembly()
                                .GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
                                .InformationalVersion ?? "1.0.0"
                        }))
                    .AddAspNetCoreInstrumentation(options =>
                    {
                        // Capture HTTP request/response details
                        options.RecordException = true;
                        options.EnrichWithHttpRequest = (activity, request) =>
                        {
                            activity.SetTag("http.request.method", request.Method);
                            activity.SetTag("http.request.path", request.Path);
                            activity.SetTag("http.request.query_string", request.QueryString);
                        };
                        options.EnrichWithHttpResponse = (activity, response) =>
                        {
                            activity.SetTag("http.response.status_code", response.StatusCode);
                        };
                    })
                    .AddHttpClientInstrumentation(options =>
                    {
                        options.RecordException = true;
                        options.EnrichWithHttpRequestMessage = (activity, request) =>
                        {
                            activity.SetTag("http.client.request.method", request.Method?.ToString());
                            activity.SetTag("http.client.request.uri", request.RequestUri?.ToString());
                        };
                    })
                    .AddEntityFrameworkCoreInstrumentation(options =>
                    {
                        options.SetDbStatementForText = true;
                        options.EnrichWithIDbCommand = (activity, command) =>
                        {
                            activity.SetTag("db.command.text", command.CommandText);
                        };
                    })
                    .AddSource(serviceName)
                    // Export to console (for development)
                    .AddConsoleExporter()
                    // Export to OTLP (for production - requires OTLP collector)
                    .AddOtlpExporter(options =>
                    {
                        // Configure OTLP endpoint if needed
                        // options.Endpoint = new Uri("http://localhost:4317");
                    });
            });

        return services;
    }
}
