using CMSMicroservice.Protobuf.Protos.DiscountShoppingCart;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.DiscountShopCQ.Commands.AddToCart;
using CMSMicroservice.Application.DiscountShopCQ.Commands.RemoveFromCart;
using CMSMicroservice.Application.DiscountShopCQ.Commands.UpdateCartItemCount;
using CMSMicroservice.Application.DiscountShopCQ.Commands.ClearCart;
using CMSMicroservice.Application.DiscountShopCQ.Queries.GetUserCart;

namespace CMSMicroservice.WebApi.Services;

public class DiscountShoppingCartService : DiscountShoppingCartContract.DiscountShoppingCartContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

    public DiscountShoppingCartService(IDispatchRequestToCQRS dispatchRequestToCQRS)
    {
        _dispatchRequestToCQRS = dispatchRequestToCQRS;
    }

    public override async Task<AddToCartResponse> AddToCart(AddToCartRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<AddToCartRequest, AddToCartCommand, AddToCartResponse>(request, context);
    }

    public override async Task<RemoveFromCartResponse> RemoveFromCart(RemoveFromCartRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<RemoveFromCartRequest, RemoveFromCartCommand, RemoveFromCartResponse>(request, context);
    }

    public override async Task<UpdateCartItemCountResponse> UpdateCartItemCount(UpdateCartItemCountRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<UpdateCartItemCountRequest, UpdateCartItemCountCommand, UpdateCartItemCountResponse>(request, context);
    }

    public override async Task<GetUserCartResponse> GetUserCart(GetUserCartRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetUserCartRequest, GetUserCartQuery, GetUserCartResponse>(request, context);
    }

    public override async Task<Empty> ClearCart(ClearCartRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<ClearCartRequest, ClearCartCommand>(request, context);
    }
}
