const { Connection, Client } = require('@temporalio/client');

let temporalClient = null;

async function getTemporalClient() {
  if (temporalClient) return temporalClient;

  const temporalUrl = process.env.TEMPORAL_URL || 'temporal-frontend.ai-core.svc.cluster.local:7233';
  console.log(`[Temporal] Connecting to cluster at ${temporalUrl}`);
  
  const connection = await Connection.connect({ address: temporalUrl });
  
  temporalClient = new Client({
    connection,
    namespace: 'default', // Using the default namespace
  });
  
  console.log('[Temporal] Successfully connected to cluster!');
  return temporalClient;
}

module.exports = {
  getTemporalClient,
};
