using CMSMicroservice.Protobuf.Protos.User;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace FrontOffice.Main.Pages.Profile;

public partial class Settings : ComponentBase
{
    [Inject] private UserContract.UserContractClient UserContract { get; set; } = default!;

    private UpdateUserRequest _request = new();
    private bool _saving;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            var user = await UserContract.GetUserForCustomerAsync(new());
            _request = new UpdateUserRequest
            {
                Id = user.Id,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Email = user.Email,
                NationalCode = user.NationalCode,
                EmailNotifications = user.EmailNotifications,
                SmsNotifications = user.SmsNotifications,
                PushNotifications = user.PushNotifications,
            };
        }
        catch
        {
            _request = new();
        }
    }

    private async Task SaveSettings()
    {
        _saving = true;
        try
        {
            await UserContract.UpdateUserAsync(_request);
            Snackbar.Add("تنظیمات ذخیره شد.", Severity.Success);
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا در ذخیره تنظیمات: {ex.Message}", Severity.Error);
        }
        finally
        {
            _saving = false;
        }
    }
}

