import chalk from 'chalk';
import { applicationTypes, authenticationTypes, databaseTypes, monitoringTypes, searchEngineTypes, serviceDiscoveryTypes, ingressTypes, } from '../../jdl/jhipster/index.js';
const { ELASTICSEARCH } = searchEngineTypes;
const { GATEWAY, MONOLITH } = applicationTypes;
const { JWT } = authenticationTypes;
const { PROMETHEUS } = monitoringTypes;
const { CONSUL, EUREKA } = serviceDiscoveryTypes;
const { GKE } = ingressTypes;
const NO_DATABASE = databaseTypes.NO;
export default {
    writeFiles,
};
export function writeFiles() {
    const suffix = 'k8s';
    return {
        writeDeployments() {
            for (let i = 0; i < this.appConfigs.length; i++) {
                const appName = this.appConfigs[i].baseName.toLowerCase();
                const appOut = appName.concat('-', suffix);
                this.app = this.appConfigs[i];
                this.writeFile('deployment.yml.ejs', `${appOut}/${appName}-deployment.yml`);
                this.writeFile('service.yml.ejs', `${appOut}/${appName}-service.yml`);
                if (this.app.databaseType !== NO_DATABASE) {
                    const databaseType = this.app.prodDatabaseType ?? this.app.databaseType;
                    this.writeFile(`db/${databaseType}.yml.ejs`, `${appOut}/${appName}-${databaseType}.yml`);
                }
                if (this.app.searchEngine === ELASTICSEARCH) {
                    this.writeFile('db/elasticsearch.yml.ejs', `${appOut}/${appName}-elasticsearch.yml`);
                }
                if (this.app.applicationType === GATEWAY || this.app.applicationType === MONOLITH) {
                    if (this.istio) {
                        this.writeFile('istio/gateway.yml.ejs', `${appOut}/${appName}-gateway.yml`);
                    }
                    else if (this.kubernetesServiceType === 'Ingress') {
                        this.writeFile('ingress.yml.ejs', `${appOut}/${appName}-ingress.yml`);
                    }
                }
                if (!this.app.serviceDiscoveryAny && this.app.authenticationType === JWT) {
                    this.writeFile('secret/jwt-secret.yml.ejs', `${appOut}/jwt-secret.yml`);
                }
                if (this.app.databaseTypeCouchbase) {
                    this.writeFile('secret/couchbase-secret.yml.ejs', `${appOut}/templates/couchbase-secret.yml`);
                }
                if (this.monitoring === PROMETHEUS) {
                    this.writeFile('monitoring/jhipster-prometheus-sm.yml.ejs', `${appOut}/${appName}-prometheus-sm.yml`);
                }
                if (this.istio) {
                    this.writeFile('istio/destination-rule.yml.ejs', `${appOut}/${appName}-destination-rule.yml`);
                    this.writeFile('istio/virtual-service.yml.ejs', `${appOut}/${appName}-virtual-service.yml`);
                }
            }
        },
        writeReadme() {
            this.writeFile('README-KUBERNETES.md.ejs', 'K8S-README.md');
        },
        writeNamespace() {
            if (this.kubernetesNamespace !== 'default') {
                this.writeFile('namespace.yml.ejs', 'namespace.yml');
            }
        },
        writeMessagingBroker() {
            if (!this.useKafka)
                return;
            this.writeFile('messagebroker/kafka.yml.ejs', `messagebroker-${suffix}/kafka.yml`);
        },
        writeKeycloak() {
            if (!this.useKeycloak)
                return;
            const keycloakOut = 'keycloak'.concat('-', suffix);
            this.entryPort = '8080';
            this.keycloakRedirectUris = '';
            this.appConfigs.forEach(appConfig => {
                if (appConfig.applicationType === GATEWAY || appConfig.applicationType === MONOLITH) {
                    this.entryPort = appConfig.composePort;
                    if (this.ingressDomain) {
                        this.keycloakRedirectUris += `"http://${appConfig.baseName.toLowerCase()}.${this.kubernetesNamespace}.${this.ingressDomain}/*",
            "https://${appConfig.baseName.toLowerCase()}.${this.kubernetesNamespace}.${this.ingressDomain}/*", `;
                    }
                    else {
                        this.keycloakRedirectUris += `"http://${appConfig.baseName.toLowerCase()}:${appConfig.composePort}/*",
            "https://${appConfig.baseName.toLowerCase()}:${appConfig.composePort}/*", `;
                    }
                    this.keycloakRedirectUris += `"http://localhost:${appConfig.composePort}/*",
            "https://localhost:${appConfig.composePort}/*",`;
                    if (appConfig.devServerPort !== undefined) {
                        this.keycloakRedirectUris += `"http://localhost:${appConfig.devServerPort}/*", `;
                    }
                    this.debug(chalk.red.bold(`${appConfig.baseName} has redirect URIs ${this.keycloakRedirectUris}`));
                    this.debug(chalk.red.bold(`AppConfig is ${JSON.stringify(appConfig)}`));
                }
            });
            this.writeFile('keycloak/keycloak-configmap.yml.ejs', `${keycloakOut}/keycloak-configmap.yml`);
            this.writeFile('keycloak/keycloak-postgresql.yml.ejs', `${keycloakOut}/keycloak-postgresql.yml`);
            this.writeFile('keycloak/keycloak.yml.ejs', `${keycloakOut}/keycloak.yml`);
            if (this.ingressType === GKE) {
                this.writeFile('cert-manager/letsencrypt-staging-ca-secret.yml.ejs', 'cert-manager/letsencrypt-staging-ca-secret.yml');
                this.writeFile('cert-manager/letsencrypt-staging-issuer.yml.ejs', 'cert-manager/letsencrypt-staging-issuer.yml');
            }
        },
        writePrometheusGrafanaFiles() {
            const monitOut = 'monitoring'.concat('-', suffix);
            if (this.monitoring === PROMETHEUS) {
                this.writeFile('monitoring/jhipster-prometheus-crd.yml.ejs', `${monitOut}/jhipster-prometheus-crd.yml`);
                this.writeFile('monitoring/jhipster-prometheus-cr.yml.ejs', `${monitOut}/jhipster-prometheus-cr.yml`);
                this.writeFile('monitoring/jhipster-grafana.yml.ejs', `${monitOut}/jhipster-grafana.yml`);
                this.writeFile('monitoring/jhipster-grafana-dashboard.yml.ejs', `${monitOut}/jhipster-grafana-dashboard.yml`);
                if (this.istio) {
                    this.writeFile('istio/gateway/jhipster-grafana-gateway.yml.ejs', `${monitOut}/jhipster-grafana-gateway.yml`);
                }
            }
        },
        writeRegistryFiles() {
            const registryOut = 'registry'.concat('-', suffix);
            if (this.serviceDiscoveryType === EUREKA) {
                this.writeFile('registry/jhipster-registry.yml.ejs', `${registryOut}/jhipster-registry.yml`);
                this.writeFile('registry/application-configmap.yml.ejs', `${registryOut}/application-configmap.yml`);
            }
            else if (this.serviceDiscoveryType === CONSUL) {
                this.writeFile('registry/consul.yml.ejs', `${registryOut}/consul.yml`);
                this.writeFile('registry/consul-config-loader.yml.ejs', `${registryOut}/consul-config-loader.yml`);
                this.writeFile('registry/application-configmap.yml.ejs', `${registryOut}/application-configmap.yml`);
            }
        },
        writeConfigRunFile() {
            this.writeFile('kubectl-apply.sh.ejs', 'kubectl-apply.sh');
        },
        writeObservabilityGatewayFiles() {
            if (!this.istio)
                return;
            const istioOut = 'istio'.concat('-', suffix);
            this.writeFile('istio/gateway/grafana-gateway.yml.ejs', `${istioOut}/grafana-gateway.yml`);
            this.writeFile('istio/gateway/zipkin-gateway.yml.ejs', `${istioOut}/zipkin-gateway.yml`);
            this.writeFile('istio/gateway/kiali-gateway.yml.ejs', `${istioOut}/kiali-gateway.yml`);
        },
        writeKustomize() {
            const patchOut = 'patch'.concat('-', suffix);
            this.writeFile('kustomize/kustomization.yml.ejs', 'kustomization.yml');
            if (this.istio) {
                this.writeFile('kustomize/patch/istio-label.yml.ejs', `${patchOut}/istio-label.yml`);
                this.writeFile('kustomize/patch/istio-namespace.yml.ejs', `${patchOut}/istio-namespace.yml`);
            }
        },
        writeSkaffold() {
            this.writeFile('skaffold/skaffold.yml.ejs', 'skaffold.yml');
        },
    };
}
