package dev.sonpx.loyalty.mcp.tool;

import dev.sonpx.loyalty.mcp.model.Result;
import dev.sonpx.loyalty.mcp.reward.model.PoolDefinition;
import dev.sonpx.loyalty.mcp.service.PoolDefinitionService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.mcp.annotation.McpTool;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PoolDefinitionTools {

    private final PoolDefinitionService poolDefinitionService;

    @McpTool(description = "Lấy thông tin chi tiết một Point Pool theo ID.")
    public Result<PoolDefinition> getPoolDefinitionById(String poolId) {
        return poolDefinitionService.findById(poolId);
    }

    @McpTool(description = "Lấy thông tin nhiều Point Pool theo danh sách ID, phân tách bằng dấu phẩy.")
    public Result<List<PoolDefinition>> getPoolDefinitionsByIds(String ids) {
        return poolDefinitionService.findActiveByIds(ids);
    }

    @McpTool(description = "TÌM KIẾM Point Pool. Trả về thông tin của pool. Param `request`: đối tượng chứa các điều kiện lọc (có thể để null nếu không cần lọc).")
    public Result<List<PoolDefinition>> searchPools(dev.sonpx.loyalty.mcp.tool.request.PoolDefinitionSearchRequest request) {
        dev.sonpx.loyalty.mcp.reward.criteria.PoolDefinitionCriteria criteria = new dev.sonpx.loyalty.mcp.reward.criteria.PoolDefinitionCriteria();
        if (request != null) {
            if (request.poolId() != null) {
                dev.sonpx.loyalty.mcp.filter.StringFilter filter = new dev.sonpx.loyalty.mcp.filter.StringFilter();
                filter.setEquals(request.poolId());
                criteria.setPoolId(filter);
            }
            if (request.poolName() != null) {
                dev.sonpx.loyalty.mcp.filter.StringFilter filter = new dev.sonpx.loyalty.mcp.filter.StringFilter();
                filter.setContains(request.poolName());
                criteria.setPoolName(filter);
            }
            if (request.poolType() != null) {
                dev.sonpx.loyalty.mcp.filter.StringFilter filter = new dev.sonpx.loyalty.mcp.filter.StringFilter();
                filter.setEquals(request.poolType());
                criteria.setPoolType(filter);
            }
            if (request.entityLevel() != null) {
                dev.sonpx.loyalty.mcp.filter.StringFilter filter = new dev.sonpx.loyalty.mcp.filter.StringFilter();
                filter.setEquals(request.entityLevel());
                criteria.setEntityLevel(filter);
            }
            if (request.expiryPolicy() != null) {
                dev.sonpx.loyalty.mcp.filter.StringFilter filter = new dev.sonpx.loyalty.mcp.filter.StringFilter();
                filter.setEquals(request.expiryPolicy());
                criteria.setExpiryPolicy(filter);
            }
            if (request.atgGroupId() != null) {
                dev.sonpx.loyalty.mcp.filter.StringFilter filter = new dev.sonpx.loyalty.mcp.filter.StringFilter();
                filter.setEquals(request.atgGroupId());
                criteria.setAtgGroupId(filter);
            }
        }
        return poolDefinitionService.getAll(criteria);
    }
}
