using CMSMicroservice.Protobuf.Protos.Commission;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.CommissionCQ.Commands.CalculateWeeklyBalances;
using CMSMicroservice.Application.CommissionCQ.Commands.CalculateWeeklyCommissionPool;
using CMSMicroservice.Application.CommissionCQ.Commands.ProcessUserPayouts;
using CMSMicroservice.Application.CommissionCQ.Commands.BulkCreditPayouts;
using CMSMicroservice.Application.CommissionCQ.Commands.RequestWithdrawal;
using CMSMicroservice.Application.CommissionCQ.Commands.ProcessWithdrawal;
using CMSMicroservice.Application.CommissionCQ.Commands.ApproveWithdrawal;
using CMSMicroservice.Application.CommissionCQ.Commands.RejectWithdrawal;
using CMSMicroservice.Application.CommissionCQ.Commands.TriggerWeeklyCalculation;
using CMSMicroservice.Application.CommissionCQ.Queries.GetWeeklyCommissionPool;
using CMSMicroservice.Application.CommissionCQ.Queries.GetUserCommissionPayouts;
using CMSMicroservice.Application.CommissionCQ.Queries.GetCommissionPayoutHistory;
using CMSMicroservice.Application.CommissionCQ.Queries.GetUserWeeklyBalances;
using CMSMicroservice.Application.CommissionCQ.Queries.GetMyWeeklyBalances;
using CMSMicroservice.Application.CommissionCQ.Queries.GetAllWeeklyPools;
using CMSMicroservice.Application.CommissionCQ.Queries.GetWithdrawalRequests;
using CMSMicroservice.Application.CommissionCQ.Queries.GetWorkerStatus;
using CMSMicroservice.Application.CommissionCQ.Queries.GetWorkerExecutionLogs;
using CMSMicroservice.Application.CommissionCQ.Queries.GetWithdrawalReports;
using CMSMicroservice.Application.CommissionCQ.Queries.GetAvailableWeeks;
using CMSMicroservice.Application.CommissionCQ.Queries.GetWeekDefinitions;
using CMSMicroservice.Application.CommissionCQ.Queries.GetMyCommissionPayouts;

namespace CMSMicroservice.WebApi.Services;

public class CommissionService : CommissionContract.CommissionContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

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

    // Commands
    public override async Task<Empty> CalculateWeeklyBalances(CalculateWeeklyBalancesRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CalculateWeeklyBalancesRequest, CalculateWeeklyBalancesCommand>(request, context);
    }

    public override async Task<Empty> CalculateWeeklyCommissionPool(CalculateWeeklyCommissionPoolRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CalculateWeeklyCommissionPoolRequest, CalculateWeeklyCommissionPoolCommand>(request, context);
    }

    public override async Task<Empty> ProcessUserPayouts(ProcessUserPayoutsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<ProcessUserPayoutsRequest, ProcessUserPayoutsCommand>(request, context);
    }

    public override async Task<Empty> RequestWithdrawal(RequestWithdrawalRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<RequestWithdrawalRequest, RequestWithdrawalCommand>(request, context);
    }

    public override async Task<Empty> ProcessWithdrawal(ProcessWithdrawalRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<ProcessWithdrawalRequest, ProcessWithdrawalCommand>(request, context);
    }

    // Queries
    public override async Task<GetWeeklyCommissionPoolResponse> GetWeeklyCommissionPool(GetWeeklyCommissionPoolRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetWeeklyCommissionPoolRequest, GetWeeklyCommissionPoolQuery, GetWeeklyCommissionPoolResponse>(request, context);
    }

    public override async Task<GetUserCommissionPayoutsResponse> GetUserCommissionPayouts(GetUserCommissionPayoutsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetUserCommissionPayoutsRequest, GetUserCommissionPayoutsQuery, GetUserCommissionPayoutsResponse>(request, context);
    }

    public override async Task<GetMyCommissionPayoutsResponse> GetMyCommissionPayouts(GetMyCommissionPayoutsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetMyCommissionPayoutsRequest, GetMyCommissionPayoutsQuery, GetMyCommissionPayoutsResponse>(request, context);
    }

    public override async Task<GetCommissionPayoutHistoryResponse> GetCommissionPayoutHistory(GetCommissionPayoutHistoryRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetCommissionPayoutHistoryRequest, GetCommissionPayoutHistoryQuery, GetCommissionPayoutHistoryResponse>(request, context);
    }

    public override async Task<GetUserWeeklyBalancesResponse> GetUserWeeklyBalances(GetUserWeeklyBalancesRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetUserWeeklyBalancesRequest, GetUserWeeklyBalancesQuery, GetUserWeeklyBalancesResponse>(request, context);
    }

    public override async Task<GetAllWeeklyPoolsResponse> GetAllWeeklyPools(GetAllWeeklyPoolsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAllWeeklyPoolsRequest, GetAllWeeklyPoolsQuery, GetAllWeeklyPoolsResponse>(request, context);
    }

    public override async Task<GetWithdrawalRequestsResponse> GetWithdrawalRequests(GetWithdrawalRequestsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetWithdrawalRequestsRequest, GetWithdrawalRequestsQuery, GetWithdrawalRequestsResponse>(request, context);
    }

    public override async Task<Empty> ApproveWithdrawal(ApproveWithdrawalRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<ApproveWithdrawalRequest, ApproveWithdrawalCommand>(request, context);
    }

    public override async Task<Empty> RejectWithdrawal(RejectWithdrawalRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<RejectWithdrawalRequest, RejectWithdrawalCommand>(request, context);
    }

    // Worker Control APIs
    public override async Task<TriggerWeeklyCalculationResponse> TriggerWeeklyCalculation(TriggerWeeklyCalculationRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<TriggerWeeklyCalculationRequest, TriggerWeeklyCalculationCommand, TriggerWeeklyCalculationResponse>(request, context);
    }

    public override async Task<GetWorkerStatusResponse> GetWorkerStatus(GetWorkerStatusRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetWorkerStatusRequest, GetWorkerStatusQuery, GetWorkerStatusResponse>(request, context);
    }

    public override async Task<GetWorkerExecutionLogsResponse> GetWorkerExecutionLogs(GetWorkerExecutionLogsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetWorkerExecutionLogsRequest, GetWorkerExecutionLogsQuery, GetWorkerExecutionLogsResponse>(request, context);
    }

    public override async Task<GetAvailableWeeksResponse> GetAvailableWeeks(GetAvailableWeeksRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAvailableWeeksRequest, GetAvailableWeeksQuery, GetAvailableWeeksResponse>(request, context);
    }

    public override async Task<GetWeekDefinitionsResponse> GetWeekDefinitions(GetWeekDefinitionsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetWeekDefinitionsRequest, GetWeekDefinitionsQuery, GetWeekDefinitionsResponse>(request, context);
    }

    public override async Task<BulkCreditPayoutsResponse> BulkCreditPayouts(BulkCreditPayoutsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<BulkCreditPayoutsRequest, BulkCreditPayoutsCommand, BulkCreditPayoutsResponse>(request, context);
    }

    public override async Task<GetWithdrawalReportsResponse> GetWithdrawalReports(GetWithdrawalReportsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetWithdrawalReportsRequest, GetWithdrawalReportsQuery, GetWithdrawalReportsResponse>(request, context);
    }

    public override async Task<GetMyWeeklyBalancesResponse> GetMyWeeklyBalances(GetMyWeeklyBalancesRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetMyWeeklyBalancesRequest, GetMyWeeklyBalancesQuery, GetMyWeeklyBalancesResponse>(request, context);
    }
}
