export interface Environment {
  production: boolean;
  applName: string;
  defaultServer: string;
  server: {
    [key: string]: string;
  };
  ssoSite: string;
  ssoSignIn: string;
  ssoSignUp: string;
  ssoSignOut: string;
  tokenStoredInPermanentStorage: boolean;
  replaceDataToRealServer?: boolean;
}

const serverSSOAddress = 'https://atria-soft.org';

const environment_back_prod: Environment = {
  production: true,
  // URL of development API
  applName: 'karideo',
  defaultServer: 'karideo',
  server: {
    karideo: `${serverSSOAddress}/karideo/api`,
    karso: `${serverSSOAddress}/karso/api`,
  },
  ssoSite: `${serverSSOAddress}/karso/`,
  ssoSignIn: `${serverSSOAddress}/karso/signin/karideo/`,
  ssoSignUp: `${serverSSOAddress}/karso/signup/karideo/`,
  ssoSignOut: `${serverSSOAddress}/karso/signout/karideo/`,
  tokenStoredInPermanentStorage: false,
};

const environment_local: Environment = {
  production: false,
  // URL of development API
  applName: 'karideo',
  defaultServer: 'karideo',
  server: {
    karideo: 'http://localhost:18080/karideo/api',
    karso: `${serverSSOAddress}/karso/api`,
  },
  ssoSite: `${serverSSOAddress}/karso/`,
  ssoSignIn: `${serverSSOAddress}/karso/signin/karideo-dev/`,
  ssoSignUp: `${serverSSOAddress}/karso/signup/karideo-dev/`,
  ssoSignOut: `${serverSSOAddress}/karso/signout/karideo-dev/`,
  tokenStoredInPermanentStorage: false,
  replaceDataToRealServer: true,
};

/**
 * Check if the current environment is for development
 * @returns true if development is active.
 */
export const isDevelopmentEnvironment = () => {
  return import.meta.env.MODE === 'development';
};

export const environment = isDevelopmentEnvironment()
  ? environment_local
  : environment_back_prod;


/**
 * get the current REST api URL. Depend on the VITE_API_BASE_URL env variable.
 * @returns The URL with http(s)://***
 */
export const getApiUrl = () => {
  const baseUrl: string | undefined = import.meta.env.VITE_API_BASE_URL;
  if (baseUrl === undefined || baseUrl === null) {
    //return `${window.location.protocol}//${window.location.host}/api`;
    return environment.server.karideo;
  }
  if (baseUrl.startsWith('http')) {
    return baseUrl;
  }
  return `${window.location.protocol}//${window.location.host}/${baseUrl}`;
};
