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 PortConstant {

	public static String LOCAL_HOST;
	public final static String FRONTEND_PORT_9191 = "30165";

	public final static String GITEA_IP_ADDRESS = "try.gitea";
	public final static String GITEA_PORT = "io";
	public final static String SUREVAULT_DEPLOYMENT_TYPE = "32";

	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_DOMAIN;
	public static String GITEA_USERNAME;
	public static String SITE_BUILDER;

	public static String PROTOCOL;

//	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() {

		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");
			PROTOCOL = getUrlFromApi(backendPortalDomain, "PROTOCOL");
			LOCAL_HOST = getUrlFromApi(backendPortalDomain, "LOCAL_HOST");
			SITE_BUILDER = getUrlFromApi(backendPortalDomain, "SITE_BUILDER");

		} 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;
		}
	}

}
