const getSupabaseUrl = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].SUPABASE_URL.value;
};

const getSupabaseKey = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].SUPABASE_KEY.value;
};

const getSupabaseServiceKey = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].SUPABASE_SERVICE_KEY.value;
};

const getGigaAuth = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].GIGA_AUTH.value;
};

const getLangsmithApiKey = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].LANGSMITH_API_KEY.value;
};

const getLangsmithEndpoint = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].LANGSMITH_ENDPOINT.value;
};

const getLangsmithTracing = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].LANGSMITH_TRACING.value;
};

const getLangsmithProject = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].LANGSMITH_PROJECT.value;
};

const getTavilyApiKey = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].TAVILY_API_KEY.value;
};

const getRagSupabaseServiceRoleKey = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].RAG_SUPABASE_SERVICE_ROLE_KEY.value;
};

const getRagSupabaseUrl = async () => {
  const response = await fetch('https://admin.bro-js.ru/api/config/v1/dev');
  const data = await response.json();
  return data.features['sber_mobile'].RAG_SUPABASE_URL.value;
};

module.exports = {
  getSupabaseUrl,
  getSupabaseKey,
  getSupabaseServiceKey
};

// IIFE для установки переменных окружения
(async () => {
  try {
    process.env.GIGA_AUTH = await getGigaAuth();
    process.env.LANGSMITH_API_KEY = await getLangsmithApiKey();
    process.env.LANGSMITH_ENDPOINT = await getLangsmithEndpoint();
    process.env.LANGSMITH_TRACING = await getLangsmithTracing();
    process.env.LANGSMITH_PROJECT = await getLangsmithProject();
    process.env.TAVILY_API_KEY = await getTavilyApiKey();
    process.env.RAG_SUPABASE_SERVICE_ROLE_KEY = await getRagSupabaseServiceRoleKey();
    process.env.RAG_SUPABASE_URL = await getRagSupabaseUrl();
    
    console.log('Environment variables loaded successfully');
  } catch (error) {
    console.error('Error loading environment variables:', error);
  }
})(); 