package com.realnet.utils;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class Port_Constant {

	public static String BACKEND_PORTAL_DOMAIN;
	public static String FRONTEND_PORTAL_DOMAIN;
	public static String SUREOPS_DOMAIN;
	public static String JOBPRO_DOMAIN;
	public static String APP_BUILD_DOMAIN;
	public static String SURE_SERVE_DOMAIN;
	public static String SURE_SETU_DOMAIN;
	public static String FARM_SCRIPT_RUNNER_DOMAIN;
	public static String DEPLOYMENT_TYPE;
	public static String FARM_IP_ADDRESS_online;
	public static String CONTROL_CENTRE_PORT;
	public static String CONTROL_CENTRE_MASTER_PORT;
	public static String CONTROL_CENTRE_MASTER_IP;
	public static String LOCALHOST;
	public static String SUREOPS_LOCALHOST;
	public static String APPBUILD_LOCALHOST;
	public static String SURE_VAULT_DOMAIN;
	public static String GITEA_DB_NAME;
	public static String GITEA_DOMAIN;
	public static String GITEA_USERNAME;
	public static String GITEA_URL;
	public static String PROTOCOL;
	public static String SONAR_QUBE_DOMAIN;
	public static String SONAR_QUBE_Username;

	public static String SONAR_QUBE_password;
	public static String DOMAIN;

//	SETTER

	private final RestTemplate restTemplate = new RestTemplate();

	// Type ('dev', 'prod')
	private final String type = "dev";

	@Value("${BACKEND_PORTAL_DOMAIN}")
	private String backendPortalDomain;

	// @PostConstruct is used to make API calls and initialize the constants for the
	// entire application during startup.
	@PostConstruct
	public void initializeConstantsFromApi() {

//        BACKEND_PORTAL_DOMAIN = getUrlFromApi("Backend_portal_domain");
//    	String backendPortalDomain = getBackendPortalUrl();
//        System.out.println("Fetched BACKEND_PORTAL_DOMAIN: " + backendPortalDomain);

		if (backendPortalDomain != null) {
			BACKEND_PORTAL_DOMAIN = backendPortalDomain;
			System.out.println("Fetched BACKEND_PORTAL_DOMAIN from properties file: " + backendPortalDomain);

			FRONTEND_PORTAL_DOMAIN = getUrlFromApi(backendPortalDomain, "Frontend_portal_domain");
			System.out.println("FRONTEND_PORTAL_DOMAIN: " + FRONTEND_PORTAL_DOMAIN);
			SUREOPS_DOMAIN = getUrlFromApi(backendPortalDomain, "SUREOPS_DOMAIN");
			JOBPRO_DOMAIN = getUrlFromApi(backendPortalDomain, "JOBPRO_DOMAIN");
			APP_BUILD_DOMAIN = getUrlFromApi(backendPortalDomain, "APP_BUILD_DOMAIN");
			SURE_SERVE_DOMAIN = getUrlFromApi(backendPortalDomain, "SURE_SERVE_DOMAIN");
			SURE_SETU_DOMAIN = getUrlFromApi(backendPortalDomain, "SURE_SETU_DOMAIN");
			FARM_SCRIPT_RUNNER_DOMAIN = getUrlFromApi(backendPortalDomain, "FARM_SCRIPT_RUNNER_DOMAIN");
			DEPLOYMENT_TYPE = getUrlFromApi(backendPortalDomain, "DEPLOYMENT_TYPE");
			FARM_IP_ADDRESS_online = getUrlFromApi(backendPortalDomain, "FARM_IP_ADDRESS_online");
			CONTROL_CENTRE_PORT = getUrlFromApi(backendPortalDomain, "CONTROL_CENTRE_PORT");
			CONTROL_CENTRE_MASTER_PORT = getUrlFromApi(backendPortalDomain, "CONTROL_CENTRE_MASTER_PORT");
			CONTROL_CENTRE_MASTER_IP = getUrlFromApi(backendPortalDomain, "CONTROL_CENTRE_MASTER_IP");
			LOCALHOST = getUrlFromApi(backendPortalDomain, "Localhost");
			SUREOPS_LOCALHOST = getUrlFromApi(backendPortalDomain, "SUREOPS_LOCALHOST");
			APPBUILD_LOCALHOST = getUrlFromApi(backendPortalDomain, "APPBUILD_LOCALHOST");
			SURE_VAULT_DOMAIN = getUrlFromApi(backendPortalDomain, "SURE_VAULT_DOMAIN");
			GITEA_DOMAIN = getUrlFromApi(backendPortalDomain, "GITEA_DOMAIN");
			GITEA_USERNAME = getUrlFromApi(backendPortalDomain, "GITEA_USERNAME");

			GITEA_URL = getUrlFromApi(backendPortalDomain, "GITEA_URL");

			GITEA_DB_NAME = getUrlFromApi(backendPortalDomain, "GITEA_DB_NAME");

			PROTOCOL = getUrlFromApi(backendPortalDomain, "PROTOCOL");
			SONAR_QUBE_DOMAIN = getUrlFromApi(backendPortalDomain, "SONAR_QUBE_DOMAIN");
			SONAR_QUBE_Username = getUrlFromApi(backendPortalDomain, "SONAR_QUBE_Username");

			SONAR_QUBE_password = getUrlFromApi(backendPortalDomain, "SONAR_QUBE_password");
			DOMAIN = getUrlFromApi(backendPortalDomain, "DOMAIN");

		} else {
			System.out.println("Error: BACKEND_PORTAL_DOMAIN could not be fetched.");
		}
	}

	private String getUrlFromApi(String backendPortalDomain, String serviceName) {
		try {
			// Use the provided backendPortalDomain to construct the full URL
			String baseUrl = backendPortalDomain + "/token/HealthCheckup/DeploymentUrl/Deployment_url/";
			String url = baseUrl + type + "/" + serviceName;
			String Object = restTemplate.getForObject(url, String.class); // Fetch URL from API

			System.out.println(serviceName + " : " + Object);
			return Object;
		} catch (Exception e) {
			System.out.println("Error fetching URL for " + serviceName + ": " + e.getMessage());
			return null;
		}
	}
}
