@page "/metrics"
@inject ILogger<Metrics> Logger
@using System.Diagnostics

<PageTitle>Metrics</PageTitle>

<h1>Metrics Demo</h1>

<p>This page demonstrates various metrics that can be tracked and visualized in Grafana.</p>

<div class="row mt-4">
    <div class="col-md-6">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Counter Metrics</h5>
                <p>Counter: @counterValue</p>
                <button class="btn btn-primary" @onclick="IncrementCounter">Increment Counter</button>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Timer Metrics</h5>
                <p>Simulate a slow operation and track its duration.</p>
                <button class="btn btn-warning" @onclick="SimulateSlowOperation" disabled="@isProcessing">
                    @(isProcessing ? "Processing..." : "Start Slow Operation")
                </button>
                @if (lastDuration > 0)
                {
                    <p class="mt-2">Last operation took: @lastDuration ms</p>
                }
            </div>
        </div>
    </div>
</div>

<div class="row mt-4">
    <div class="col-md-6">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Error Simulation</h5>
                <p>Generate different types of log entries for testing.</p>
                <button class="btn btn-danger" @onclick="SimulateError">Generate Error</button>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Custom Metrics</h5>
                <p>Page visits: @pageVisits</p>
                <small class="text-muted">This counter increments each time you visit this page.</small>
            </div>
        </div>
    </div>
</div>

@code {
    private int counterValue = 0;
    private int pageVisits = 0;
    private bool isProcessing = false;
    private long lastDuration = 0;
    private static readonly ActivitySource ActivitySource = new("BlazorApp.Metrics");

    protected override void OnInitialized()
    {
        pageVisits++;
        Logger.LogInformation("Metrics page visited. Total visits: {PageVisits}", pageVisits);
    }

    private void IncrementCounter()
    {
        counterValue++;
        Logger.LogInformation("Counter incremented to {CounterValue}", counterValue);
    }

    private async Task SimulateSlowOperation()
    {
        isProcessing = true;
        var sw = Stopwatch.StartNew();
        
        using var activity = ActivitySource.StartActivity("SlowOperation");
        activity?.SetTag("operation.type", "simulated");
        
        Logger.LogInformation("Starting slow operation");
        
        try
        {
            await Task.Delay(Random.Shared.Next(1000, 3000));
            sw.Stop();
            lastDuration = sw.ElapsedMilliseconds;
            
            Logger.LogInformation("Slow operation completed in {Duration}ms", lastDuration);
            activity?.SetTag("duration.ms", lastDuration);
        }
        finally
        {
            isProcessing = false;
        }
    }

    private void SimulateError()
    {
        var errorId = Guid.NewGuid();
        Logger.LogError("Simulated error with ID: {ErrorId}", errorId);
        Logger.LogWarning("This is a test warning associated with error {ErrorId}", errorId);
    }
}
