@if (SelectedTrack is null)
{
    <MudText Typo="Typo.body1" Color="Color.Default">Select a track from the list to edit its details.</MudText>
}
else
{
    <MudStack Spacing="4">
        @if (ShowTrackName)
        {
            <MudTextField Value="SelectedTrack.TrackName"
                          ValueChanged="@((string v) => TrackNameChanged.InvokeAsync(v))"
                          T="string"
                          Label="Track Name"
                          Required="true"
                          RequiredError="Track Name is required"
                          Variant="Variant.Outlined"
                          Disabled="Disabled" />
        }

        @if (SelectedTrack.Id.HasValue)
        {
            <MudField Label="Original File" Variant="Variant.Outlined" InnerPadding="false">
                <MudText Typo="Typo.body2">@(string.IsNullOrEmpty(SelectedTrack.OriginalFileName) ? "—" : SelectedTrack.OriginalFileName)</MudText>
                <MudText Typo="Typo.caption" Color="Color.Default">Existing track — audio is not editable.</MudText>
            </MudField>
        }
        else
        {
            <MudField Label="WAV File" Variant="Variant.Outlined" InnerPadding="false">
                @if (SelectedTrack.WavFile is { } wav)
                {
                    <MudText Typo="Typo.body2">@wav.Name (@FormatBytes(wav.Size))</MudText>
                }
                else
                {
                    <MudText Typo="Typo.body2" Color="Color.Error">No WAV file selected.</MudText>
                }
            </MudField>
        }

        @if (SelectedTrack.Status == BatchRowStatus.Failed)
        {
            <MudAlert Severity="Severity.Error">@SelectedTrack.ErrorMessage</MudAlert>
        }
    </MudStack>
}

@code {
    [Parameter] public BatchRowModel? SelectedTrack { get; set; }
    [Parameter] public bool Disabled { get; set; }
    [Parameter] public EventCallback<string> TrackNameChanged { get; set; }
    /// <summary>
    /// When false (single-track Session/Mix), the Track Name field is suppressed — the name is
    /// derived from the Release Name by the parent form and never entered independently.
    /// Defaults to true so the Cut multi-track path is unchanged.
    /// </summary>
    [Parameter] public bool ShowTrackName { get; set; } = true;

    private static string FormatBytes(long bytes)
    {
        const long KB = 1024;
        const long MB = KB * 1024;
        const long GB = MB * 1024;
        if (bytes >= GB) return $"{bytes / (double)GB:F2} GB";
        if (bytes >= MB) return $"{bytes / (double)MB:F2} MB";
        if (bytes >= KB) return $"{bytes / (double)KB:F2} KB";
        return $"{bytes} bytes";
    }
}
