using FrontOffice.Main.Utilities;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace FrontOffice.Main.Pages.Profile;

public partial class Wallet : ComponentBase
{
    private (string Credit, string Discount, string Network) _balances = ("-", "-", "-");
    private List<WalletTransaction> _txs = new();
    private string? _filterReferenceId;
    private string _filterType = "all";

    protected override async Task OnInitializedAsync()
    {
        var b = await WalletService.GetBalancesAsync();
        _balances = (FormatPrice(b.CreditBalance), FormatPrice(b.DiscountBalance), FormatPrice(b.NetworkBalance));
        _txs = await WalletService.GetTransactionsAsync();
    }

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

    private async Task ApplyFilters()
    {
        long? refId = null;
        if (long.TryParse(_filterReferenceId, out var parsed) && parsed > 0)
        {
            refId = parsed;
        }
        bool? isIncrease = _filterType switch
        {
            "in" => true,
            "out" => false,
            _ => null
        };
        _txs = await WalletService.GetTransactionsAsync(refId, isIncrease);
    }
}
