import { loadVoiceAgentConfig } from "./config.ts";
import { VoiceAgentRuntime } from "./voice-agent-runtime.ts";
import { defaultAuditLogPath, JsonlVoiceAuditStore } from "./audit-store.ts";

const config = loadVoiceAgentConfig();
const auditPath = process.env.VOICE_AGENT_AUDIT_LOG ?? defaultAuditLogPath();
const auditStore = new JsonlVoiceAuditStore(auditPath);
const runtime = new VoiceAgentRuntime({
  config,
  onAudit: async (record) => {
    await auditStore.append(record);
    console.log(
      JSON.stringify({
        type: "audit",
        auditPath,
        callId: record.callId,
        disposition: record.disposition,
        voiceProvider: record.voiceProvider,
        ringCentral: record.ringCentral,
      }),
    );
  },
});

const started = await runtime.start();
console.log(JSON.stringify({ ok: true, started, auditPath }, null, 2));

const shutdown = async () => {
  await runtime.stop();
  process.exit(0);
};

process.on("SIGINT", () => void shutdown());
process.on("SIGTERM", () => void shutdown());

await new Promise(() => {});
