package org.openjproxy.grpc.server.utils;

import com.openjproxy.grpc.StatementRequest;
import org.openjproxy.constants.CommonConstants;
import org.openjproxy.grpc.ProtoConverter;

import java.sql.Statement;
import java.util.Map;

/**
 * Utility class for validating statement request properties and flags.
 * Extracted from StatementServiceImpl to improve modularity.
 */
public class StatementRequestValidator {

    /**
     * Checks if the request has auto-generated keys flag set.
     *
     * @param request The statement request
     * @return true if auto-generated keys flag is set
     */
    public static boolean hasAutoGeneratedKeysFlag(StatementRequest request) {
        if (request.getPropertiesList().isEmpty()) {
            return false;
        }
        Map<String, Object> properties = ProtoConverter.propertiesFromProto(request.getPropertiesList());
        Integer autoGeneratedKeys = (Integer) properties.get(CommonConstants.STATEMENT_AUTO_GENERATED_KEYS_KEY);
        return autoGeneratedKeys != null && autoGeneratedKeys == Statement.RETURN_GENERATED_KEYS;
    }

    /**
     * Checks if the request is an add batch operation.
     *
     * @param request The statement request
     * @return true if it's an add batch operation
     */
    public static boolean isAddBatchOperation(StatementRequest request) {
        if (request.getPropertiesList().isEmpty()) {
            return false;
        }
        Map<String, Object> properties = ProtoConverter.propertiesFromProto(request.getPropertiesList());
        Boolean batchFlag = (Boolean) properties.get(CommonConstants.PREPARED_STATEMENT_ADD_BATCH_FLAG);
        return batchFlag != null && batchFlag;
    }
}