using CMSMicroservice.Protobuf.Protos.Configuration;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.ConfigurationCQ.Commands.SetConfigurationValue;
using CMSMicroservice.Application.ConfigurationCQ.Commands.DeactivateConfiguration;
using CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationByKey;
using CMSMicroservice.Application.ConfigurationCQ.Queries.GetAllConfigurations;
using CMSMicroservice.Application.ConfigurationCQ.Queries.GetConfigurationHistory;

namespace CMSMicroservice.WebApi.Services;

public class ConfigurationService : ConfigurationContract.ConfigurationContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

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

    public override async Task<Empty> CreateOrUpdateConfiguration(CreateOrUpdateConfigurationRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CreateOrUpdateConfigurationRequest, SetConfigurationValueCommand>(request, context);
    }

    public override async Task<Empty> DeactivateConfiguration(DeactivateConfigurationRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<DeactivateConfigurationRequest, DeactivateConfigurationCommand>(request, context);
    }

    public override async Task<GetConfigurationByKeyResponse> GetConfigurationByKey(GetConfigurationByKeyRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetConfigurationByKeyRequest, GetConfigurationByKeyQuery, GetConfigurationByKeyResponse>(request, context);
    }

    public override async Task<GetAllConfigurationsResponse> GetAllConfigurations(GetAllConfigurationsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAllConfigurationsRequest, GetAllConfigurationsQuery, GetAllConfigurationsResponse>(request, context);
    }

    public override async Task<GetConfigurationHistoryResponse> GetConfigurationHistory(GetConfigurationHistoryRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetConfigurationHistoryRequest, GetConfigurationHistoryQuery, GetConfigurationHistoryResponse>(request, context);
    }
}
