import 'dotenv/config';
import pg from 'pg';

const { Pool } = pg;
const isMemoryDb = !process.env.DATABASE_URL || process.env.DATABASE_URL.startsWith('memory://');
const pool = isMemoryDb ? null : new Pool({ connectionString: process.env.DATABASE_URL });
const tickMs = parseInt(process.env.WORKER_TICK_MS || '10000', 10);
const runOnce = process.env.WORKER_RUN_ONCE === '1';

// Placeholder worker loop
async function main() {
  if (pool) {
    await pool.query('select 1');
  }
  console.log(`Mastermind worker started (${isMemoryDb ? 'memory' : 'postgres'})`);
  while (true) {
    await new Promise((r) => setTimeout(r, tickMs));
    if (runOnce) break;
    // future: ingest inbox, OCR docs, classify
  }
}

main().catch((e) => {
  console.error(e);
  process.exit(1);
});
