using CMSMicroservice.Protobuf.Protos.UserCarts;
using CMSMicroservice.WebApi.Common.Services;

namespace CMSMicroservice.WebApi.Services;

public class UserCartsService : UserCartsContract.UserCartsContractBase
{
    private readonly IDispatchRequestToCQRS _dispatcher;

    public UserCartsService(IDispatchRequestToCQRS dispatcher)
    {
        _dispatcher = dispatcher;
    }

    #region Customer Methods

    public override async Task<AddNewUserCartForCustomerResponse> AddNewUserCartForCustomer(
        AddNewUserCartForCustomerRequest request, ServerCallContext context)
    {
        // TODO: Map to DiscountShop AddToCart command
        return new AddNewUserCartForCustomerResponse
        {
            Message = "AddNewUserCartForCustomer not implemented yet"
        };
    }

    public override async Task<UpdateUserCartForCustomerResponse> UpdateUserCartForCustomer(
        UpdateUserCartForCustomerRequest request, ServerCallContext context)
    {
        // TODO: Map to DiscountShop UpdateCartItemCount command
        return new UpdateUserCartForCustomerResponse
        {
            Message = "UpdateUserCartForCustomer not implemented yet"
        };
    }

    public override async Task<RemoveUserCartForCustomerResponse> RemoveUserCartForCustomer(
        RemoveUserCartForCustomerRequest request, ServerCallContext context)
    {
        // TODO: Map to DiscountShop RemoveFromCart command
        return new RemoveUserCartForCustomerResponse
        {
            Message = "RemoveUserCartForCustomer not implemented yet"
        };
    }

    public override async Task<GetUserCartForCustomerResponse> GetUserCartForCustomer(
        GetUserCartForCustomerRequest request, ServerCallContext context)
    {
        // TODO: Map to DiscountShop GetUserCart query
        return new GetUserCartForCustomerResponse();
    }

    #endregion

    #region Admin Methods

    public override async Task<AddNewUserCartResponse> AddNewUserCart(
        AddNewUserCartRequest request, ServerCallContext context)
    {
        throw new RpcException(new Status(StatusCode.Unimplemented, "AddNewUserCart not implemented yet"));
    }

    public override async Task<Empty> UpdateUserCart(
        UpdateUserCartRequest request, ServerCallContext context)
    {
        throw new RpcException(new Status(StatusCode.Unimplemented, "UpdateUserCart not implemented yet"));
    }

    public override async Task<Empty> DeleteUserCart(
        DeleteUserCartRequest request, ServerCallContext context)
    {
        throw new RpcException(new Status(StatusCode.Unimplemented, "DeleteUserCart not implemented yet"));
    }

    public override async Task<GetUserCartResponse> GetUserCart(
        GetUserCartRequest request, ServerCallContext context)
    {
        throw new RpcException(new Status(StatusCode.Unimplemented, "GetUserCart not implemented yet"));
    }

    public override async Task<GetAllUserCartsByFilterResponse> GetAllUserCartsByFilter(
        GetAllUserCartsByFilterRequest request, ServerCallContext context)
    {
        throw new RpcException(new Status(StatusCode.Unimplemented, "GetAllUserCartsByFilter not implemented yet"));
    }

    #endregion
}
using CMSMicroservice.Application.UserCartsCQ.Commands.ClearCart;
using Grpc.Core;

namespace CMSMicroservice.WebApi.Services;

public class UserCartsService : UserCartsContract.UserCartsContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

    public UserCartsService(IDispatchRequestToCQRS dispatchRequestToCQRS)
    {
        _dispatchRequestToCQRS = dispatchRequestToCQRS;
    }
    
    public override async Task<CreateNewUserCartsResponse> CreateNewUserCarts(CreateNewUserCartsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CreateNewUserCartsRequest, CreateNewUserCartsCommand, CreateNewUserCartsResponse>(request, context);
    }
    
    public override async Task<Google.Protobuf.WellKnownTypes.Empty> UpdateUserCarts(UpdateUserCartsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<UpdateUserCartsRequest, UpdateUserCartsCommand>(request, context);
    }
    
    public override async Task<Google.Protobuf.WellKnownTypes.Empty> DeleteUserCarts(DeleteUserCartsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<DeleteUserCartsRequest, DeleteUserCartsCommand>(request, context);
    }
    
    public override async Task<GetUserCartsResponse> GetUserCarts(GetUserCartsRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetUserCartsRequest, GetUserCartsQuery, GetUserCartsResponse>(request, context);
    }
    
    public override async Task<GetAllUserCartsByFilterResponse> GetAllUserCartsByFilter(GetAllUserCartsByFilterRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAllUserCartsByFilterRequest, GetAllUserCartsByFilterQuery, GetAllUserCartsByFilterResponse>(request, context);
    }
    
    public override async Task<ClearCartResponse> ClearCart(ClearCartRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<ClearCartRequest, ClearCartCommand, ClearCartResponse>(request, context);
    }
    
    public override async Task<ClearCartResponse> ClearCartForCustomer(ClearCartRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<ClearCartRequest, ClearCartCommand, ClearCartResponse>(request, context);
    }
    
    // ============= Customer-specific Methods =============
    
    public override async Task<CreateNewUserCartsResponse> AddNewUserCartForCustomer(CreateNewUserCartsRequest request, ServerCallContext context)
    {
        // Reuse the existing CreateNewUserCarts logic
        return await CreateNewUserCarts(request, context);
    }
    
    public override async Task<Google.Protobuf.WellKnownTypes.Empty> UpdateUserCartForCustomer(UpdateUserCartsRequest request, ServerCallContext context)
    {
        // Reuse the existing UpdateUserCarts logic
        return await UpdateUserCarts(request, context);
    }
    
    public override async Task<GetAllUserCartsByFilterResponse> GetUserCartForCustomer(GetAllUserCartsByFilterRequest request, ServerCallContext context)
    {
        // Reuse the existing GetAllUserCartsByFilter logic
        return await GetAllUserCartsByFilter(request, context);
    }
}
