package cz.moneta.test.dsl.udebs;

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.Optional;

public class UserStatusToIdentityBuilder {

    private static final String SET_USER_TO_IDENTITY =
            "DECLARE" +
                    " on_UserLoginId      number;" +
                    " on_DigitalContextId number;" +
                    " BEGIN" +
                    "   on_UserLoginId := 0;" +
                    "   on_DigitalContextId := 0;" +
                    "   UDEBSGE.EM_ADMIN_CONTEXTS.Set_UserLoginsToContext(" +
                    "        in_userloginid => %s, " +
                    "        in_digitalcontextid => %s, " +
                    "        in_entitystatusid => 345 , " +
                    "        id_validityfrom => sysdate,      " +
                    "        id_closedate => null, " +
                    "        on_UserLoginId => on_UserLoginId," +
                    "        on_DigitalContextId => on_DigitalContextId);" +
                    "   dbms_output.put_line('on_DigitalContextId: ' || on_DigitalContextId);" +
                    " END; ";

    private final StoreAccessor harness;

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

    public void setStatus(String userLoginId, String digitalContextId) {
        Optional.of(harness.getEndpoint(UdebsEndpoint.class))
                .map(e -> e.executeSql(String.format(SET_USER_TO_IDENTITY, userLoginId, digitalContextId)))
                .orElseThrow(() -> new HarnessException("Error I couldn't set user status to identity"));
    }
}
