@page "/logs"
@inject ILogger<Logs> Logger

<PageTitle>Logs</PageTitle>

<h1>Logs Demo</h1>

<p>This page helps you generate different types of log entries for testing your Grafana setup.</p>

<div class="row mt-4">
    <div class="col-md-4">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Information</h5>
                <p>Generate informational log entries.</p>
                <button class="btn btn-info" @onclick="@(() => LogMessage(LogLevel.Information))">
                    Log Information
                </button>
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Warning</h5>
                <p>Generate warning log entries.</p>
                <button class="btn btn-warning" @onclick="@(() => LogMessage(LogLevel.Warning))">
                    Log Warning
                </button>
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Error</h5>
                <p>Generate error log entries.</p>
                <button class="btn btn-danger" @onclick="@(() => LogMessage(LogLevel.Error))">
                    Log Error
                </button>
            </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">Structured Logging</h5>
                <p>Generate structured log with custom properties.</p>
                <button class="btn btn-primary" @onclick="LogStructuredMessage">
                    Log Structured Data
                </button>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Bulk Logs</h5>
                <p>Generate multiple log entries at once.</p>
                <button class="btn btn-secondary" @onclick="GenerateBulkLogs">
                    Generate 10 Logs
                </button>
            </div>
        </div>
    </div>
</div>

<div class="alert alert-success mt-4">
    <strong>Total logs generated this session:</strong> @logCount
</div>

@code {
    private int logCount = 0;

    private void LogMessage(LogLevel level)
    {
        var message = $"Test {level} message generated at {DateTime.UtcNow:O}";
        
        switch (level)
        {
            case LogLevel.Information:
                Logger.LogInformation("{Message}", message);
                break;
            case LogLevel.Warning:
                Logger.LogWarning("{Message}", message);
                break;
            case LogLevel.Error:
                Logger.LogError("{Message}", message);
                break;
        }
        
        logCount++;
    }

    private void LogStructuredMessage()
    {
        var userId = Guid.NewGuid();
        var operationId = Random.Shared.Next(1000, 9999);
        
        Logger.LogInformation(
            "User {UserId} performed operation {OperationId} with status {Status} at {Timestamp}",
            userId,
            operationId,
            "Success",
            DateTime.UtcNow
        );
        
        logCount++;
    }

    private void GenerateBulkLogs()
    {
        for (int i = 0; i < 10; i++)
        {
            var logLevel = i % 3 switch
            {
                0 => LogLevel.Information,
                1 => LogLevel.Warning,
                _ => LogLevel.Error
            };
            
            LogMessage(logLevel);
        }
    }
}
