
package dev.sonpx.loyalty.mcp.reward.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.Data;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

@ToString
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class RuleCriteria {

    private String field;

    private String fieldType;

    private String operator;

    private Object[] filterValue;

    private String filterType;

    private String combinator;

    private List<RuleCriteria> rules = new ArrayList<>();

    public boolean isSimple() {
        return !isGroup();
    }

    public boolean isGroup() {
        return !(rules == null || rules.isEmpty());
    }

    public boolean isEmpty() {
        return StringUtils.isEmpty(field) && StringUtils.isEmpty(fieldType) && (rules == null || rules.isEmpty());
    }

    public void validateSimple(RuleCriteria rule, List<String> errors) {
        if (StringUtils.isEmpty(rule.getField())) {
            errors.add("Field is required. " + rule.devLog());
        }
        if (StringUtils.isEmpty(rule.getOperator())) {
            errors.add("Operator is required. " + rule.devLog());
        }
        if (StringUtils.isEmpty(rule.getFieldType())) {
            errors.add("Field type is required. " + rule.devLog());
        }
    }

    private String devLog() {
        return String.format(
                "field: [%s], fieldType: [%s], operator: [%s], filterValue: [%s], filterType: [%s]",
                field, fieldType, operator, Arrays.toString(filterValue), filterType);
    }
}
