package cz.moneta.test.dsl.udebs.ib;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.udebs.UdebsEndpoint;
import org.jooq.Record;
import org.jooq.Result;

public class AccountNumbers {

    private final StoreAccessor harness;

    private String CURRENT_ACCOOUNTS_QUERY = "select CLIENTACCOUNTS.ACCOUNTID, ACCOUNTS.ACCTYPEID, gettext(ACCOUNTTYPES.TEXTID,'cs'), users.rc_number from users\n" +
            "join CLIENTACCOUNTS on users.clientid = CLIENTACCOUNTS.CLIENTID\n" +
            "join ACCOUNTS on CLIENTACCOUNTS.ACCOUNTID = ACCOUNTS.ACCOUNTID\n" +
            "join ACCOUNTTYPES on ACCOUNTS.ACCTYPEID = ACCOUNTTYPES.ACCTYPEID\n" +
            "where RC_NUMBER='%s' and gettext(ACCOUNTTYPES.TEXTID,'cs') in ('Běžný účet')";

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

    public Result<Record> getCurrentAccounts(String birthNumber) {
        return harness.getEndpoint(UdebsEndpoint.class)
                .executeSql(String.format(CURRENT_ACCOOUNTS_QUERY, birthNumber));
    }

    public String getTopCurrentAccount(String birthNumber) {
        return getCurrentAccounts(birthNumber).get(0)
                .getValue("ACCOUNTID")
                .toString();
    }
}