package dev.sonpx.loyalty.mcp.config;

import org.springframework.core.MethodParameter;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.service.invoker.HttpRequestValues;
import org.springframework.web.service.invoker.HttpServiceArgumentResolver;

public class PageableArgumentResolver implements HttpServiceArgumentResolver {
    @Override
    public boolean resolve(Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) {
        if (!Pageable.class.isAssignableFrom(parameter.getParameterType())) {
            return false;
        }

        if (argument instanceof Pageable pageable) {
            if (pageable.isPaged()) {
                requestValues.addRequestParameter("page", String.valueOf(pageable.getPageNumber()));
                requestValues.addRequestParameter("size", String.valueOf(pageable.getPageSize()));
            }

            Sort sort = pageable.getSort();
            if (sort.isSorted()) {
                for (Sort.Order order : sort) {
                    requestValues.addRequestParameter("sort", order.getProperty() + "," + order.getDirection().name());
                }
            }
        }
        return true;
    }
}
