package cz.moneta.test.dsl.udebs;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.udebs.UdebsEndpoint;

import java.util.Optional;

public class PhoneBuilder {
    private final StoreAccessor harness;

    private static final String IS_CONTACT_PHONE_EXISTS = " SELECT" +
            "      CONTACTGSM, CONTACTPHONE, CONTACTFAX" +
            "  FROM" +
            "      users" +
            "  WHERE" +
            "   '420%s'" +
            "   IN(CONTACTGSM, CONTACTPHONE, CONTACTFAX)";

    public PhoneBuilder(StoreAccessor harness) {
        this.harness = harness;
    }

    public boolean existPhone(String phoneNumber) {
        String sqlQuery = String.format(IS_CONTACT_PHONE_EXISTS, phoneNumber);

        return Optional.of(harness.getEndpoint(UdebsEndpoint.class))
                .map(e -> e.executeSql(sqlQuery))
                .filter(rs -> rs.isNotEmpty())
                .isPresent();
    }
}
