import wso2/gateway;
import ballerina/http;
import ballerina/log;

{{#if containerConfig.hasDocker}}import ballerinax/docker;{{/if}}
{{#if containerConfig.kubernetes.kubernetesService.enable}}import ballerinax/kubernetes;{{/if}}

//Throttle tier data initiation

{{#if config.mutualSSL.certificateDetails}}
    json ClientCerts ={{{config.mutualSSL.certificateDetails}}};
{{else}}
    json ClientCerts=null;
{{/if}}



//Get mutualSSL filter
gateway: MutualSSLFilter  mtslFilter= new;
// Get authentication filter
gateway:AuthnFilter authnFilter = getAuthenticationFilter();
// Subscription validation filter
// Authorization filter
gateway:OAuthzFilter authorizationFilter = gateway:getDefaultAuthorizationFilter();
gateway:SubscriptionFilter subscriptionFilter = new;
// Get deployed policies
map<boolean> deployedPolicies = getDeployedPolicies();
// Throttling filter
gateway:ThrottleFilter throttleFilter = new(deployedPolicies);
// Validation filter
gateway:ValidationFilter validationFilter = new;
// Analytics filter
gateway:AnalyticsRequestFilter analyticsFilter = new;
// Extension filter
ExtensionFilter extensionFilter = new;

http:ServiceEndpointConfiguration secureServiceEndpointConfiguration = { {{>http2}},
                                                                           {{>mutualSSL}}
                                                                           filters:[mtslFilter, authnFilter, authorizationFilter, subscriptionFilter, throttleFilter, validationFilter, analyticsFilter,
                                                                           extensionFilter]
                                                                       };

{{>docker}}
{{>secureKubernetesIngress}}
{{>kubernetesService}}
listener gateway:APIGatewaySecureListener apiSecureListener = new(secureServiceEndpointConfiguration);

http:ServiceEndpointConfiguration serviceEndpointConfiguration = { {{>http2}},
                                                                     filters:[authnFilter, authorizationFilter, subscriptionFilter, throttleFilter, validationFilter, analyticsFilter,
                                                                     extensionFilter]
                                                                 };

{{>kubernetesIngress}}
{{>kubernetesService}}
listener gateway:APIGatewayListener apiListener = new(serviceEndpointConfiguration);


function getAuthenticationFilter() returns gateway:AuthnFilter {
    http:AuthHandlerRegistry registry = new;
    // Getting basic and JWT authentication providers
    http:AuthProvider[] authProviders = gateway:getAuthProviders();
    int i = 1;
    foreach var provider in authProviders {
        if ( provider.id.length() > 0) {
            registry.add(provider.id, gateway:createAuthHandler(provider));
        } else {
            registry.add(provider.scheme + "-" + i, gateway:createAuthHandler(provider));
        }
        i= i +1;
    }
    // Adding basic and JWT authentication providers to a the handler chain
    http:AuthnHandlerChain authnHandlerChain = new(registry);
    // OAuth authentication handler
    gateway:OAuthnAuthenticator oauthAuthenticator = new;
    // Register OAuth authentication handler,basic and JWT authentication providers to the authentication filter
    return new gateway:AuthnFilter(oauthAuthenticator, authnHandlerChain);
}

