using CMSMicroservice.Protobuf.Protos.DiscountProduct;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.DiscountShopCQ.Commands.CreateDiscountProduct;
using CMSMicroservice.Application.DiscountShopCQ.Commands.UpdateDiscountProduct;
using CMSMicroservice.Application.DiscountShopCQ.Commands.DeleteDiscountProduct;
using CMSMicroservice.Application.DiscountShopCQ.Queries.GetDiscountProductById;
using CMSMicroservice.Application.DiscountShopCQ.Queries.GetDiscountProducts;

namespace CMSMicroservice.WebApi.Services;

public class DiscountProductService : DiscountProductContract.DiscountProductContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

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

    public override async Task<CreateDiscountProductResponse> CreateDiscountProduct(CreateDiscountProductRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CreateDiscountProductRequest, CreateDiscountProductCommand, CreateDiscountProductResponse>(request, context);
    }

    public override async Task<Empty> UpdateDiscountProduct(UpdateDiscountProductRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<UpdateDiscountProductRequest, UpdateDiscountProductCommand>(request, context);
    }

    public override async Task<Empty> DeleteDiscountProduct(DeleteDiscountProductRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<DeleteDiscountProductRequest, DeleteDiscountProductCommand>(request, context);
    }

    public override async Task<GetDiscountProductByIdResponse> GetDiscountProductById(GetDiscountProductByIdRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetDiscountProductByIdRequest, GetDiscountProductByIdQuery, GetDiscountProductByIdResponse>(request, context);
    }

    public override async Task<GetDiscountProductsResponse> GetDiscountProducts(GetDiscountProductsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetDiscountProductsRequest, GetDiscountProductsQuery, GetDiscountProductsResponse>(request, context);
    }
}
