// Session 65 (§A4) — preflight BEFORE anything else prints, so the container's
// state (content present? critical env present? cron armed?) is the first
// thing in the logs, not something inferred after a silent rollback.
require('./preflight').runPreflight();

const app = require('./app');
// Session 20 — surface which providers are actually configured at
// boot. A silently-missing key (e.g. ODDSPAPI_KEY unset in prod)
// otherwise only manifests when the gateway tries to fall over and
// finds no chain.
const { getConfiguredProviders, listProviderIds } = require('./config/providers');
// Session 24 — warm the Tank01 cache after boot so streaks / hot lists /
// game lines have data on the first page load. Non-blocking; see module.
const { scheduleStartupPrefetch } = require('./startupPrefetch');
// Session 45 — in-process snapshot cron (gated on SNAPSHOT_CRON=1).
const { startSnapshotScheduler } = require('./snapshotScheduler');
const { startBackupScheduler } = require('./backupScheduler');

// Default 3001 — Next.js owns 3000 locally and in production. The poller,
// internal cron, and BASE_URL conventions all assume 3001 for the Express
// backend. PORT env still overrides for special-case deploys.
const PORT = process.env.PORT || 3001;

app.listen(PORT, () => {
  console.log(`[VYNDR] Server running on port ${PORT}`);
  const configured = getConfiguredProviders();
  const missing = listProviderIds().filter((id) => !configured.find((c) => c.id === id));
  console.log(`[VYNDR] providers configured (${configured.length}): ${configured.map((c) => c.id).join(', ') || 'none'}`);
  if (missing.length) {
    console.warn(`[VYNDR] providers missing keys: ${missing.join(', ')}`);
  }

  // Session 24 — fire-and-forget cache warm. 5s delay so Redis is ready.
  // Skips itself when RAPID_API_KEY is unset; never blocks or crashes boot.
  scheduleStartupPrefetch();

  // Session 45 — arm the snapshot cron (no-op unless SNAPSHOT_CRON=1).
  startSnapshotScheduler();

  // Session 64 — arm the NIGHTLY BACKUP. Opt-OUT (armed whenever
  // SUPABASE_DB_URL exists; BACKUP_CRON=0 kills it). The host cron was written
  // in S62 and never installed, so the database went unbacked every night;
  // shipping it as code means deploy == installed.
  startBackupScheduler();
});
