/*
 * Copyright (c) 2018 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

@App:name("IS_ANALYTICS_AUTHENTICATION_COMMON")
@App:description("Handle Event from IS and save data in a table for Common Authentication")

-- Read data from IS
@source(type = "wso2event", wso2.stream.id = "org.wso2.is.analytics.stream.OverallAuthentication:1.0.0", @map(type = 'wso2event'))
define stream SourceStreamFromIdentityServerWso2EventStream (
    meta_tenantId int,
    contextId string,
    eventId string,
    eventType string,
    authenticationSuccess bool,
    username string,
    localUsername string,
    userStoreDomain string,
    tenantDomain string,
    remoteIp string,
    region string,
    inboundAuthType string,
    serviceProvider string,
    rememberMeEnabled bool,
    forceAuthEnabled bool,
    passiveAuthEnabled bool,
    rolesCommaSeparated string,
    authenticationStep string,
    identityProvider string,
    authStepSuccess bool,
    stepAuthenticator string,
    isFirstLogin bool,
    identityProviderType string,
    _timestamp long
);

@sink(type='inMemory', topic='IsAnalytics.Authentication.Common.OutputSinkFromProcessedAuthenticationDataStream', @map(type='passThrough'))
define stream ProcessedAuthenticationDataStream (
    meta_tenantId int,
    eventId string,
    username string,
    localUsername string,
    userStoreDomain string,
    tenantDomain string,
    rolesCommaSeparated string,
    remoteIp string,
    region string,
    inboundAuthType string,
    serviceProvider string,
    rememberMeEnabled bool,
    forceAuthEnabled bool,
    passiveAuthEnabled bool,
    stepAuthenticator string,
    authenticationStep string,
    authStepSuccess bool,
    identityProvider string,
    identityProviderType string,
    isFirstLogin bool,
    successValue int,
    failureValue int,
    stepSuccessValue int,
    firstLoginValue int,
    timestamp long
);

@sink(type='inMemory', topic='IsAnalytics.Authentication.Common.OutputSinkFromOverallAuthenticationProcessedStream', @map(type='passThrough'))
define stream OverallAuthenticationProcessedStream (
    meta_tenantId int,
    contextId string,
    eventId string,
    eventType string,
    username string,
    localUsername string,
    userStoreDomain string,
    tenantDomain string,
    remoteIp string,
    region string,
    inboundAuthType string,
    serviceProvider string,
    rememberMeEnabled bool,
    forceAuthEnabled bool,
    passiveAuthEnabled bool,
    rolesCommaSeparated string,
    authenticationStep string,
    identityProvider string,
    authenticationSuccess bool,
    authStepSuccess bool,
    stepAuthenticator string,
    isFirstLogin bool,
    identityProviderType string,
    timestamp long
);

@info(name = 'Passthrough query with option to enable geo-location')
from SourceStreamFromIdentityServerWso2EventStream
select
    meta_tenantId,
    contextId,
    eventId,
    eventType,
    username,
    localUsername,
    userStoreDomain,
    tenantDomain,
    remoteIp,
    -- Uncomment following line and comment the line following ("region") to enable geo location data in siddhi app
    --ifThenElse(geo:findCountryFromIP(remoteIp)=="", "N/A", geo:findCountryFromIP(remoteIp)) as region,
    region,
    inboundAuthType,
    serviceProvider,
    rememberMeEnabled,
    forceAuthEnabled,
    passiveAuthEnabled,
    rolesCommaSeparated,
    authenticationStep,
    identityProvider,
    authenticationSuccess,
    authStepSuccess,
    stepAuthenticator,
    isFirstLogin,
    identityProviderType,
    _timestamp as timestamp
insert into OverallAuthenticationProcessedStream;

-- Adding commas to the start and the end of roles comma separated as to use 'contains' keyword on it.
@info(name = 'Basic select ops on OverallAuthenticationProcessedStream and generates ProcessedAuthenticationDataStream')
from OverallAuthenticationProcessedStream
select
    meta_tenantId,
    eventId,
    username,
    localUsername,
    userStoreDomain,
    tenantDomain,
    str:concat(',', rolesCommaSeparated, ',') as rolesCommaSeparated,
    remoteIp,
    region,
    inboundAuthType,
    serviceProvider,
    rememberMeEnabled,
    forceAuthEnabled,
    passiveAuthEnabled,
    stepAuthenticator,
    authenticationStep,
    authStepSuccess,
    identityProvider,
    ifThenElse(identityProviderType is NULL, "SSO", identityProviderType) as identityProviderType,
    isFirstLogin,
    ifThenElse(authenticationSuccess == true, 1, 0) as successValue,
    ifThenElse((authStepSuccess == false and eventType == 'step'), 1, 0) as failureValue,
    ifThenElse((authStepSuccess == true and eventType == 'step'), 1, 0) as stepSuccessValue,
    ifThenElse(authenticationSuccess == true AND isFirstLogin == true, 1, 0) as firstLoginValue,
    timestamp
insert into ProcessedAuthenticationDataStream;

