@namespace QuantEngine.Web.Client.Components

<MudStack Spacing="2" Class="form-field">
    <label class="form-label">
        @Label
        @if (Required)
        {
            <span class="text-error">*</span>
        }
    </label>

    @switch (Type)
    {
        case "text":
        case "email":
        case "password":
        case "number":
            <MudTextField T="string"
                         Value="@Value"
                         ValueChanged="@((string v) => ValueChanged.InvokeAsync(v))"
                         Variant="Variant.Outlined"
                         FullWidth="true"
                         Placeholder="@Placeholder"
                         Type="@Type"
                         Required="@Required"
                         ErrorText="@ErrorMessage" />
            break;

        case "textarea":
            <MudTextField T="string"
                         Value="@Value"
                         ValueChanged="@((string v) => ValueChanged.InvokeAsync(v))"
                         Variant="Variant.Outlined"
                         FullWidth="true"
                         Placeholder="@Placeholder"
                         Lines="5"
                         Required="@Required"
                         ErrorText="@ErrorMessage" />
            break;

        case "select":
            <MudSelect T="string"
                      Value="@Value"
                      ValueChanged="@((string v) => ValueChanged.InvokeAsync(v))"
                      Variant="Variant.Outlined"
                      FullWidth="true"
                      Required="@Required">
                @foreach (var option in Options)
                {
                    <MudSelectItem T="string" Value="@option">@option</MudSelectItem>
                }
            </MudSelect>
            break;

        case "checkbox":
            <MudCheckBox T="bool"
                        Checked="@(Value == "true")"
                        CheckedChanged="@((bool v) => ValueChanged.InvokeAsync(v ? "true" : "false"))">
                @Label
            </MudCheckBox>
            break;

        case "date":
            <MudTextField T="string"
                         Value="@Value"
                         ValueChanged="@((string v) => ValueChanged.InvokeAsync(v))"
                         Variant="Variant.Outlined"
                         FullWidth="true"
                         Type="date"
                         Required="@Required" />
            break;
    }

    @if (!string.IsNullOrEmpty(HelpText))
    {
        <MudText Typo="Typo.caption" Class="text-muted">@HelpText</MudText>
    }
</MudStack>

@code {
    [Parameter]
    public string Label { get; set; } = "";

    [Parameter]
    public string Type { get; set; } = "text";

    [Parameter]
    public string Value { get; set; } = "";

    [Parameter]
    public EventCallback<string> ValueChanged { get; set; }

    [Parameter]
    public string Placeholder { get; set; } = "";

    [Parameter]
    public bool Required { get; set; } = false;

    [Parameter]
    public string ErrorMessage { get; set; } = "";

    [Parameter]
    public string HelpText { get; set; } = "";

    [Parameter]
    public List<string> Options { get; set; } = new();
}

<style>
    .form-field {
        margin-bottom: 1rem;
    }

    .form-label {
        display: block;
        font-weight: 500;
        font-size: 0.875rem;
        color: var(--mud-palette-text-primary);
        margin-bottom: 0.5rem;
    }

    .form-label .text-error {
        color: var(--mud-palette-error);
    }
</style>
