@namespace Lumeo

<div class="@RootClasses" @attributes="AdditionalAttributes">
    <div class="flex items-baseline justify-between gap-3">
        <div class="min-w-0">
            @if (!string.IsNullOrEmpty(Label))
            {
                <div class="@LabelClass">@Label</div>
            }
            @if (!string.IsNullOrEmpty(Value))
            {
                <div class="@ValueClass">@Value</div>
            }
        </div>
    </div>

    <div class="@SparklineHeightClass w-full">
        @if (ChildContent is not null)
        {
            @ChildContent
        }
        else if (Data is not null && _points is not null)
        {
            <svg viewBox="0 0 100 30" preserveAspectRatio="none" class="h-full w-full overflow-visible" aria-hidden="true">
                <polyline points="@_points"
                          fill="none"
                          stroke="var(--color-primary, currentColor)"
                          stroke-width="1.5"
                          stroke-linecap="round"
                          stroke-linejoin="round"
                          vector-effect="non-scaling-stroke" />
            </svg>
        }
    </div>
</div>

@code {
    [Parameter] public string? Label { get; set; }
    [Parameter] public string? Value { get; set; }
    [Parameter] public IEnumerable<double>? Data { get; set; }
    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public SparkCardSize Size { get; set; } = SparkCardSize.Medium;
    [Parameter] public SparkCardVariant Variant { get; set; } = SparkCardVariant.Default;
    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }

    private string? _points;

    protected override void OnParametersSet()
    {
        _points = ComputePoints(Data);
    }

    private static string? ComputePoints(IEnumerable<double>? data)
    {
        if (data is null) return null;
        var list = data.ToList();
        if (list.Count < 2) return null;

        var min = list.Min();
        var max = list.Max();
        var range = max - min;
        if (range == 0) range = 1;

        var sb = new System.Text.StringBuilder();
        var n = list.Count;
        for (int i = 0; i < n; i++)
        {
            var x = (double)i / (n - 1) * 100.0;
            // Invert Y so higher values are visually higher (SVG y goes down)
            var yNorm = (list[i] - min) / range;
            var y = 28.0 - yNorm * 26.0 + 1.0; // padding 1 top/bottom, area height 26
            if (i > 0) sb.Append(' ');
            sb.Append(x.ToString("0.##", System.Globalization.CultureInfo.InvariantCulture));
            sb.Append(',');
            sb.Append(y.ToString("0.##", System.Globalization.CultureInfo.InvariantCulture));
        }
        return sb.ToString();
    }

    private string SizePadding => Size switch
    {
        SparkCardSize.Small => "p-3",
        SparkCardSize.Large => "p-6",
        _ => "p-5"
    };

    private string SparklineHeightClass => Size switch
    {
        SparkCardSize.Small => "mt-2 h-8",
        SparkCardSize.Large => "mt-4 h-14",
        _ => "mt-3 h-10"
    };

    private string LabelClass => Size switch
    {
        SparkCardSize.Small => "text-xs text-muted-foreground truncate",
        SparkCardSize.Large => "text-base text-muted-foreground truncate",
        _ => "text-sm text-muted-foreground truncate"
    };

    private string ValueClass => Size switch
    {
        SparkCardSize.Small => "mt-0.5 text-lg font-semibold tracking-tight text-foreground",
        SparkCardSize.Large => "mt-1.5 text-3xl font-semibold tracking-tight text-foreground",
        _ => "mt-1 text-2xl font-semibold tracking-tight text-foreground"
    };

    private string VariantClasses => Variant switch
    {
        SparkCardVariant.Bordered => "rounded-xl border-2 border-border bg-card text-card-foreground",
        SparkCardVariant.Filled => "rounded-xl border border-border/40 bg-muted text-card-foreground",
        _ => "rounded-xl border border-border bg-card text-card-foreground"
    };

    private string RootClasses =>
        $"{VariantClasses} {SizePadding} {Class}".Trim();

    public enum SparkCardSize { Small, Medium, Large }
    /// <summary>
    /// Default = standard card border. Bordered = thicker 2px border. Filled = muted background.
    /// </summary>
    public enum SparkCardVariant { Default, Bordered, Filled }
}
