using CMSMicroservice.Protobuf.Protos.City;
using CMSMicroservice.WebApi.Common.Services;
using CMSMicroservice.Application.CityCQ.Queries.GetAllCitiesByFilter;

namespace CMSMicroservice.WebApi.Services;

public class CityService : CityContract.CityContractBase
{
    private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;

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

    public override async Task<GetAllCitiesByFilterResponse> GetAllCitiesByFilter(
        GetAllCitiesByFilterRequest request, 
        ServerCallContext context)
    {
        return await _dispatchRequestToCQRS.Handle<
            GetAllCitiesByFilterRequest, 
            GetAllCitiesByFilterQuery, 
            GetAllCitiesByFilterResponse>(request, context);
    }

    #region Customer Methods

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

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

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

    // Admin Methods placeholder for future expansion
    public override async Task<CreateCityResponse> CreateCity(
        CreateCityRequest request, ServerCallContext context)
    {
        throw new RpcException(new Status(StatusCode.Unimplemented, "CreateCity not implemented yet"));
    }

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

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

    #endregion
}
