using CMSMicroservice.Protobuf.Protos.UserOrder;
using Mapster;

namespace FrontOffice.Main.Utilities;

// public enum PaymentMethod
// {
//     Wallet = 1,
// }
//
// public enum OrderStatus
// {
//     Pending = 0,
//     Paid = 1,
// }

// public record OrderItem(long ProductId, string Title, string ImageUrl, long UnitPrice, int Quantity)
// {
//     public long LineTotal => UnitPrice * Quantity;
// }
//
// public class Order
// {
//     public long Id { get; set; }
//     public DateTime CreatedAt { get; set; } = DateTime.Now;
//     public OrderStatus Status { get; set; } = OrderStatus.Pending;
//     public PaymentMethod PaymentMethod { get; set; } = PaymentMethod.Wallet;
//     public long AddressId { get; set; }
//     public string AddressSummary { get; set; } = string.Empty;
//     public List<OrderItem> Items { get; set; } = new();
//     public long Total => Items.Sum(i => i.LineTotal);
// }

public class OrderService
{
    private long _seq = 1000;
    private readonly List<GetUserOrderResponse> _orders = new();
    private readonly CMSMicroservice.Protobuf.Protos.UserOrder.UserOrderContract.UserOrderContractClient _userOrderContractClient;

    public OrderService(CMSMicroservice.Protobuf.Protos.UserOrder.UserOrderContract.UserOrderContractClient userOrderContractClient)
    {
        _userOrderContractClient = userOrderContractClient;
    }

    public async Task<List<GetUserOrderResponse>> GetOrdersAsync()
    {
        var result = await _userOrderContractClient.GetCustomerOrdersAsync(new());
        if (result != null && result.Models.Count > 0)
        {
            foreach (var item in result.Models)
            {
                var order = new GetUserOrderResponse();
                if (_orders.All(a => a.Id != item.Id))
                {
                    TypeAdapterConfig.GlobalSettings.NewConfig<GetUserOrderResponseFactorDetail, GetUserOrderResponseFactorDetail>()
                        .Map(dest => dest.ProductThumbnailPath, src => src.ProductThumbnailPath);
                    order= item.Adapt<GetUserOrderResponse>();
                    order.FactorDetails.AddRange(item.FactorDetails.Adapt<List<GetUserOrderResponseFactorDetail>>());
                    _orders.Add(order);
                }
            }
        }

        return _orders.OrderByDescending(o => o.PaymentDate).ToList();
    }

    public async Task<GetUserOrderResponse?> GetOrderAsync(long id)
    {
        var order = _orders.FirstOrDefault(o => o.Id == id);
        if (order == null)
        {
            var result = await _userOrderContractClient.GetCustomerOrderAsync(new GetUserOrderRequest()
            {
                Id = id
            });
            _orders.Add(result);
            return result;
        }

        return order;
    }

    /// <summary>
    /// دریافت نرخ مالیات بر ارزش افزوده
    /// </summary>
    public async Task<double> GetVATRateAsync()
    {
        try
        {
            // TODO: Use Configuration service to get VAT rate
            // return await _userOrderContractClient.GetVATRateAsync(new Google.Protobuf.WellKnownTypes.Empty());
            return 0.10; // Default 10%
        }
        catch
        {
            // در صورت خطا، مقادیر پیش‌فرض
            return 0.10;
        }
    }
}