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

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

import java.util.Date;
import java.util.Optional;

public class ClientWithToIdentityBannerBuilder {

    private final StoreAccessor harness;

    private static final String GET_CLIENT_WITH_TO_IDENTITY_BANNER =
            "select distinct cdet.iconumber, enstat.userloginid, contex.digitalcontextid, detuser.idcardvalidity_prim, cdet.companyname, enstat.entitystatusid " +
                    " from udebsge.em_userloginstocontexts enstat" +
                    " join udebsge.em_digitalcontexts contex" +
                    " on enstat.digitalcontextid = contex.digitalcontextid" +
                    " join udebsge.em_digitaluserdetails detuser" +
                    " on contex.digitaluserid = detuser.digitaluserid" +
                    " join udebsge.em_digitalclientdetails cdet" +
                    " on contex.digitalclientid = cdet.digitalclientid" +
                    " join udebsge.em_entitystatuses statpop" +
                    " on enstat.entitystatusid = statpop.entitystatusid" +
                    " join em_usercontextcontacts contacts" +
                    " on contex.digitalcontextid = contacts.digitalcontextid" +
                    " where rownum = 1 and ((enstat.entitystatusid = '346' and enstat.identfirstaccessdate > sysdate - interval '5' day or enstat.entitystatusid = '345')" +
                    " and enstat.closedate = TO_DATE('31.12.2999','DD.MM.YYYY') " +
                    " and cdet.closedate = TO_DATE('31.12.2999','DD.MM.YYYY') " +
                    " and contacts.closedate = TO_DATE('31.12.2999','DD.MM.YYYY'))";

    private static final String GET_CLIENT_WITH_TO_POPUP_IDENTITY_BANNER =
            "select distinct cdet.iconumber, enstat.userloginid, contex.digitalcontextid,  detuser.idcardvalidity_prim, cdet.companyname, enstat.entitystatusid " +
                    " from udebsge.em_userloginstocontexts enstat" +
                    " join udebsge.em_digitalcontexts contex" +
                    " on enstat.digitalcontextid = contex.digitalcontextid" +
                    " join udebsge.em_digitaluserdetails detuser" +
                    " on contex.digitaluserid = detuser.digitaluserid" +
                    " join udebsge.em_digitalclientdetails cdet" +
                    " on contex.digitalclientid = cdet.digitalclientid" +
                    " join udebsge.em_entitystatuses statpop" +
                    " on enstat.entitystatusid = statpop.entitystatusid" +
                    " join em_usercontextcontacts contacts" +
                    " on contex.digitalcontextid = contacts.digitalcontextid" +
                    " where rownum = 1 and ((enstat.entitystatusid = '346' or enstat.entitystatusid = '345')" +
                    " and enstat.identfirstaccessdate < sysdate - interval '5' day" +
                    " and enstat.closedate = TO_DATE('31.12.2999','DD.MM.YYYY') " +
                    " and cdet.closedate = TO_DATE('31.12.2999','DD.MM.YYYY') " +
                    " and contacts.closedate = TO_DATE('31.12.2999','DD.MM.YYYY'))";

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

    public IbCredentials getClientWithToIdentityBanner() {
        return Optional.of(getEndpoint())
                .map(e -> e.executeSql(GET_CLIENT_WITH_TO_IDENTITY_BANNER))
                .flatMap(rs -> rs.stream().findFirst())
                .map(r -> new IbCredentials(
                                r.getValue(0, String.class),
                                r.getValue(1, String.class),
                                r.getValue(2, String.class),
                                r.getValue(3, Date.class)
                        )
                )
                .orElseThrow(() -> new HarnessException(" Error getting GET_CLIENT_WITH_TO_IDENTITY_BANNER from UDEBS."));
    }

    public IbCredentials getClientWithPopupToIdentityBanner() {
        return Optional.of(getEndpoint())
                .map(e -> e.executeSql(GET_CLIENT_WITH_TO_POPUP_IDENTITY_BANNER))
                .flatMap(rs -> rs.stream().findFirst())
                .map(r -> new IbCredentials(
                                r.getValue(0, String.class),
                                r.getValue(1, String.class),
                                r.getValue(2, String.class),
                                r.getValue(3, Date.class)
                        )
                )
                .orElseThrow(() -> new HarnessException(" Error getting GET_CLIENT_WITH_POPUP_TO_IDENTITY_BANNER from UDEBS."));
    }

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