using CMSMicroservice.Protobuf.Protos.Category;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.CategoryCQ.Commands.CreateNewCategory;
using CMSMicroservice.Application.CategoryCQ.Commands.UpdateCategory;
using CMSMicroservice.Application.CategoryCQ.Commands.DeleteCategory;
using CMSMicroservice.Application.CategoryCQ.Queries.GetCategory;
using CMSMicroservice.Application.CategoryCQ.Queries.GetAllCategoryByFilter;
namespace CMSMicroservice.WebApi.Services;
public class CategoryService : CategoryContract.CategoryContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

    public CategoryService(IDispatchRequestToCQRS dispatchRequestToCQRS)
    {
        _dispatchRequestToCQRS = dispatchRequestToCQRS;
    }
    public override async Task<CreateNewCategoryResponse> CreateNewCategory(CreateNewCategoryRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<CreateNewCategoryRequest, CreateNewCategoryCommand, CreateNewCategoryResponse>(request, context);
    }
    public override async Task<Empty> UpdateCategory(UpdateCategoryRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<UpdateCategoryRequest, UpdateCategoryCommand>(request, context);
    }
    public override async Task<Empty> DeleteCategory(DeleteCategoryRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<DeleteCategoryRequest, DeleteCategoryCommand>(request, context);
    }
    public override async Task<GetCategoryResponse> GetCategory(GetCategoryRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetCategoryRequest, GetCategoryQuery, GetCategoryResponse>(request, context);
    }
    public override async Task<GetAllCategoryByFilterResponse> GetAllCategoryByFilter(GetAllCategoryByFilterRequest request, ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<GetAllCategoryByFilterRequest, GetAllCategoryByFilterQuery, GetAllCategoryByFilterResponse>(request, context);
    }
    
    // ============= Customer-specific Methods =============
    
    public override async Task<GetAllCategoriesResponse> GetAllCategories(GetAllCategoriesRequest request, ServerCallContext context)
    {
        // Reuse existing GetAllCategoryByFilter query but with customer-specific response
        return await _dispatchRequestToCQRS.Handle<GetAllCategoriesRequest, GetAllCategoryByFilterQuery, GetAllCategoriesResponse>(request, context);
    }

    public override async Task<GetAllCategoriesForCustomerResponse> GetAllCategoriesForCustomer(GetAllCategoriesForCustomerRequest request, ServerCallContext context)
    {
        // TODO: Implement using existing CMS Category Application layer
        // For now, return empty response
        return new GetAllCategoriesForCustomerResponse
        {
            MetaData = new CMSMicroservice.Protobuf.Protos.MetaData
            {
                CurrentPage = request.PageNumber,
                PageSize = request.PageSize,
                TotalCount = 0,
                TotalPage = 0,
                HasNext = false,
                HasPrevious = false
            }
        };
    }

    public override async Task<GetCategoryByIdForCustomerResponse> GetCategoryByIdForCustomer(GetCategoryByIdForCustomerRequest request, ServerCallContext context)
    {
        // TODO: Implement using existing CMS Category Application layer
        throw new RpcException(new Status(StatusCode.Unimplemented, "GetCategoryByIdForCustomer not implemented yet"));
    }
}
