using CMSMicroservice.Protobuf.Protos.UserAddress;
using CMSMicroservice.Protobuf.Protos.UserOrder;
using FrontOffice.Main.Utilities;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using Messages = CMSMicroservice.Protobuf.Protos;

namespace FrontOffice.Main.Pages.Store;

public partial class CheckoutSummary : ComponentBase
{
    [Inject] private CartService Cart { get; set; } = default!;
    [Inject] private OrderService OrderService { get; set; } = default!;
    [Inject] private VATService VAT { get; set; } = default!;
    [Inject] private UserAddressContract.UserAddressContractClient UserAddressContract { get; set; } = default!;
    [Inject] private UserOrderContract.UserOrderContractClient UserOrderContract { get; set; } = default!;
    [Inject] private AuthDialogService AuthDialogService { get; set; } = default!;
    [Inject] private AuthService AuthService { get; set; } = default!;
    // Snackbar and Navigation are injected via _Imports.razor

    private List<CustomerAddressModel> _addresses = new();
    private CustomerAddressModel? _selectedAddress;
    private bool _loadingAddresses;
    private long walletBalance;

    private Messages.PaymentMethod _payment = Messages.PaymentMethod.Wallet;

    private bool CanPlaceOrder => Cart.Items.Count > 0 && _selectedAddress != null;

    protected override async Task OnInitializedAsync()
    {
        if (!await AuthService.IsAuthenticatedAsync())
        {
            await AuthDialogService.ShowAuthDialogAsync();
        }
        // لود سبد خرید (فقط اگر کاربر لاگین کرده باشد)
        await Cart.EnsureInitializedAsync();
        await LoadAddresses();
        await LoadWalletBalance();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            // بارگذاری نرخ VAT
            await VAT.LoadAsync();
        }
        await base.OnAfterRenderAsync(firstRender);
    }

    private async Task LoadWalletBalance()
    {
        var walletResult = await WalletService.GetBalancesAsync();
        walletBalance = walletResult.CreditBalance
        //  + walletResult.NetworkBalance
         ;
    }

    private async Task LoadAddresses()
    {
        _loadingAddresses = true;
        try
        {
            var response = await UserAddressContract.GetCustomerAddressesAsync(new());
            if (response?.Models?.Any() == true)
            {
                _addresses = response.Models.ToList();
                _selectedAddress = _addresses.FirstOrDefault(a => a.IsDefault) ?? _addresses.First();
            }
            else
            {
                _addresses = new();
            }
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا در بارگذاری آدرس‌ها: {ex.Message}", Severity.Error);
            _addresses = new();
        }
        finally
        {
            _loadingAddresses = false;
            await InvokeAsync(StateHasChanged);
        }
    }

    private async Task PlaceOrder()
    {
        if (!CanPlaceOrder || _selectedAddress is null)
        {
            Snackbar.Add("لطفاً آدرس را انتخاب کنید و سبد خرید را بررسی کنید.", Severity.Warning);
            return;
        }

        try
        {
            var request = new SubmitShopBuyOrderRequest
            {
                TotalAmount = VAT.AddVAT(Cart.Total)
            };

            var response = await UserOrderContract.SubmitShopBuyOrderAsync(request);
            await Cart.Clear();
            Snackbar.Add("سفارش با موفقیت ثبت شد.", Severity.Success);
            Navigation.NavigateTo($"{RouteConstants.Store.OrderDetail}{response.Id}");
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا در ثبت سفارش: {ex.Message}", Severity.Error);
        }
    }

    private static string FormatPrice(long price) => string.Format("{0:N0} تومان", price);

    /// <summary>
    /// محاسبه مالیات بر ارزش افزوده
    /// </summary>
    private long CalculateVAT() => VAT.CalculateVAT(Cart.Total);

    private static string GetProductImageUrl(string? imageUrl)
        => string.IsNullOrWhiteSpace(imageUrl) ? "/images/product-placeholder.svg" : imageUrl.TrimStart('/');
}