@using CMSMicroservice.Protobuf.Protos.Inventory

<MudDialog>
    <DialogContent>
        <MudForm @ref="_form" @bind-IsValid="_formValid">
            <MudStack Spacing="4">
                <MudTextField T="string" @bind-Value="_name"
                              Label="نام انبار"
                              Variant="Variant.Outlined"
                              Required="true"
                              RequiredError="نام انبار الزامی است"
                              MaxLength="100" />

                <MudTextField T="string" @bind-Value="_code"
                              Label="کد انبار"
                              Variant="Variant.Outlined"
                              Required="true"
                              RequiredError="کد انبار الزامی است"
                              MaxLength="20"
                              HelperText="کد منحصر به فرد برای شناسایی انبار (مثال: WH-001)" />

                <MudTextField T="string" @bind-Value="_address"
                              Label="آدرس"
                              Variant="Variant.Outlined"
                              Lines="2"
                              MaxLength="500" />

                <MudSwitch T="bool" @bind-Value="_isDefault"
                           Color="Color.Primary"
                           Label="انبار پیش‌فرض"
                           LabelPosition="LabelPosition.Start" />

                @if (_isDefault)
                {
                    <MudAlert Severity="Severity.Info">
                        این انبار به عنوان انبار پیش‌فرض برای ورود کالا تنظیم می‌شود.
                    </MudAlert>
                }
            </MudStack>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel">انصراف</MudButton>
        <MudButton Color="Color.Primary"
                   Variant="Variant.Filled"
                   OnClick="Submit"
                   Disabled="@(!_formValid || _isSubmitting)">
            @if (_isSubmitting)
            {
                <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" />
            }
            ایجاد انبار
        </MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] IMudDialogInstance MudDialog { get; set; } = default!;
    [Inject] public InventoryContract.InventoryContractClient InventoryContract { get; set; } = default!;

    private MudForm? _form;
    private bool _formValid;
    private bool _isSubmitting;

    private string _name = string.Empty;
    private string _code = string.Empty;
    private string _address = string.Empty;
    private bool _isDefault = false;

    private void Cancel() => MudDialog.Cancel();

    private async Task Submit()
    {
        if (_form != null)
        {
            await _form.Validate();
            if (!_formValid) return;
        }

        _isSubmitting = true;
        try
        {
            var request = new CreateWarehouseRequest
            {
                Name = _name,
                Code = _code,
                Address = _address ?? string.Empty,
                IsDefault = _isDefault
            };

            var response = await InventoryContract.CreateWarehouseAsync(request);

            if (response.Id > 0)
            {
                Snackbar.Add($"انبار '{_name}' با شناسه {response.Id} ایجاد شد", Severity.Success);
                MudDialog.Close(DialogResult.Ok(response.Id));
            }
            else
            {
                Snackbar.Add("خطا در ایجاد انبار", Severity.Error);
            }
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا: {ex.Message}", Severity.Error);
        }
        finally
        {
            _isSubmitting = false;
        }
    }
}
