package cz.moneta.test.dsl.util.task.udebs.cbljournal;

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 CblJournalTasks {
    private final StoreAccessor harness;

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

    private UdebsEndpoint getEndpoint() {
        return harness.getEndpoint(UdebsEndpoint.class);
    }

    public Result<Record> selectFromCblJournal(String apiname, int daysInPast, int channelId, String serverId) {
        return getEndpoint().executeSql(
            "SELECT * FROM CBL_JOURNAL WHERE " +
                " APINAME = '" + apiname + "' " +
                " AND REQUESTTIMESTAMP > SYSDATE - " + daysInPast +
                " AND STATUS = 1" +
                " AND CHANNELID = " + channelId +
                (serverId == null ? "" : " AND SERVERID LIKE '%" + serverId + "%'") +
                " ORDER BY REQUESTTIMESTAMP DESC");
    }

    public Result<Record> selectFromCblJournalByUserId(Long userId) {
        String sql = "SELECT * FROM CBL_JOURNAL WHERE " +
            " USERID = " + userId +
            " AND REQUESTTIMESTAMP > SYSDATE - 1" +
            " ORDER BY REQUESTTIMESTAMP DESC";
        harness.log("Executing SQL: " + sql);
        return getEndpoint().executeSql(sql);
    }
}
