using CMSMicroservice.Protobuf.Protos.ManualPayment;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.ManualPaymentCQ.Commands.CreateManualPayment;
using CMSMicroservice.Application.ManualPaymentCQ.Commands.ApproveManualPayment;
using CMSMicroservice.Application.ManualPaymentCQ.Commands.RejectManualPayment;
using CMSMicroservice.Application.ManualPaymentCQ.Queries.GetAllManualPayments;
using Grpc.Core;
using Mapster;
using MediatR;

namespace CMSMicroservice.WebApi.Services;

public class ManualPaymentService : ManualPaymentContract.ManualPaymentContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;
    private readonly ISender _sender;

    public ManualPaymentService(
        IDispatchRequestToCQRS dispatchRequestToCQRS,
        ISender sender)
    {
        _dispatchRequestToCQRS = dispatchRequestToCQRS;
        _sender = sender;
    }

    public override async Task<CreateManualPaymentResponse> CreateManualPayment(
        CreateManualPaymentRequest request,
        ServerCallContext context)
    {
        var command = request.Adapt<CreateManualPaymentCommand>();
        var id = await _sender.Send(command, context.CancellationToken);

        return new CreateManualPaymentResponse
        {
            Id = id
        };
    }

    public override async Task<Google.Protobuf.WellKnownTypes.Empty> ApproveManualPayment(
        ApproveManualPaymentRequest request,
        ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<ApproveManualPaymentRequest, ApproveManualPaymentCommand>(request, context);
    }

    public override async Task<Google.Protobuf.WellKnownTypes.Empty> RejectManualPayment(
        RejectManualPaymentRequest request,
        ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<RejectManualPaymentRequest, RejectManualPaymentCommand>(request, context);
    }

    public override async Task<GetAllManualPaymentsResponse> GetAllManualPayments(
        GetAllManualPaymentsRequest request,
        ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAllManualPaymentsRequest, GetAllManualPaymentsQuery, GetAllManualPaymentsResponse>(request, context);
    }
}
