@namespace Lumeo

@{
    var context = new FormFieldContext(Name, EffectiveError);
}

<CascadingValue Value="context" IsFixed="false">
    <div class="@CssClass" @attributes="AdditionalAttributes">
        @if (Orientation == FormFieldOrientation.Vertical)
        {
            @if (Label is not null)
            {
                <Label>
                    @Label
                    @if (Required)
                    {
                        <span class="text-destructive ml-0.5">*</span>
                    }
                </Label>
            }
            @ChildContent
            @if (HelpText is not null && EffectiveError is null)
            {
                <p class="text-xs text-muted-foreground">@HelpText</p>
            }
            @if (EffectiveError is not null)
            {
                <p class="text-xs text-destructive">@EffectiveError</p>
            }
        }
        else
        {
            <div class="grid grid-cols-[auto_1fr] items-center gap-x-4 gap-y-1">
                @if (Label is not null)
                {
                    <Label Class="@(string.Join(" ", new[] { "text-right", LabelWidth is not null ? $"w-[{LabelWidth}]" : null }.Where(s => !string.IsNullOrWhiteSpace(s))))">
                        @Label
                        @if (Required)
                        {
                            <span class="text-destructive ml-0.5">*</span>
                        }
                    </Label>
                }
                <div>
                    @ChildContent
                    @if (HelpText is not null && EffectiveError is null)
                    {
                        <p class="text-xs text-muted-foreground mt-1">@HelpText</p>
                    }
                    @if (EffectiveError is not null)
                    {
                        <p class="text-xs text-destructive mt-1">@EffectiveError</p>
                    }
                </div>
            </div>
        }
    </div>
</CascadingValue>

@code {
    [Parameter] public string? Label { get; set; }
    [Parameter] public string? HelpText { get; set; }
    [Parameter] public string? Error { get; set; }
    [Parameter] public bool Required { get; set; }
    [Parameter] public FormFieldOrientation Orientation { get; set; } = FormFieldOrientation.Vertical;
    [Parameter] public string? LabelWidth { get; set; }
    [Parameter] public string? Name { get; set; }
    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }

    // Bug D fix: receive the parent Form's context so Reset() clears cascade to child fields.
    [CascadingParameter] private FormContext? ParentFormContext { get; set; }

    // Use live errors from FormContext when available; fall back to the manually-set Error param.
    private string? EffectiveError =>
        ParentFormContext is { } pc && !string.IsNullOrEmpty(Name)
            ? pc.GetFieldErrors(Name).FirstOrDefault() ?? Error
            : Error;

    private string CssClass => Orientation == FormFieldOrientation.Vertical
        ? $"space-y-2 {Class}".Trim()
        : $"{Class}".Trim();

    public enum FormFieldOrientation
    {
        Vertical,
        Horizontal
    }

    public record FormFieldContext(string? Name, string? Error)
    {
        public bool HasError => !string.IsNullOrEmpty(Error);
    }
}
