import 'dotenv/config';

import { config } from '../config.js';

function resolveWorkerSlots(): number {
  if (!Number.isFinite(config.workerSlots) || config.workerSlots <= 0) return 4;
  return config.workerSlots;
}

async function main() {
  if (!process.env.HATCHET_CLIENT_TOKEN) {
    throw new Error('HATCHET_CLIENT_TOKEN is required for hatchet worker');
  }

  const { hatchet } = await import('./hatchet-client.js');
  const { processVoiceExperienceWorkflow } = await import(
    './workflows/process-voice-experience.js'
  );
  const worker = await hatchet.worker(config.workerName, {
    workflows: [processVoiceExperienceWorkflow],
    slots: resolveWorkerSlots(),
  });

  console.log(`[hatchet-worker] Starting worker "${config.workerName}"`);
  await worker.start();
}

main().catch((error) => {
  console.error('[hatchet-worker] Fatal error:', error);
  process.exit(1);
});
