using CMSMicroservice.Application.Common.Authorization;
using CMSMicroservice.Protobuf.Protos.AppVersion;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.AppVersionCQ.Queries.GetAppVersion;
using CMSMicroservice.Application.AppVersionCQ.Queries.GetAllAppVersions;
using CMSMicroservice.Application.AppVersionCQ.Commands.UpdateAppVersion;

namespace CMSMicroservice.WebApi.Services;

public class AppVersionService : AppVersionContract.AppVersionContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

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

    public override async Task<GetAppVersionResponse> GetAppVersion(GetAppVersionRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAppVersionRequest, GetAppVersionQuery, GetAppVersionResponse>(request, context);
    }

    [RequiresPermission(PermissionNames.SettingsManageConfiguration)]
    public override async Task<Empty> UpdateAppVersion(UpdateAppVersionRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<UpdateAppVersionRequest, UpdateAppVersionCommand>(request, context);
    }

    [RequiresPermission(PermissionNames.SettingsView)]
    public override async Task<GetAllAppVersionsResponse> GetAllAppVersions(GetAllAppVersionsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAllAppVersionsRequest, GetAllAppVersionsQuery, GetAllAppVersionsResponse>(request, context);
    }
}
