/**
 * Dispatch to the right `drizzle-kit generate` based on DB_DRIVER.
 *
 * `npm run db:generate` reads the env-var and runs:
 *   - DB_DRIVER=sqlite → drizzle-kit generate --config=drizzle.sqlite.config.ts
 *   - DB_DRIVER=mysql  → drizzle-kit generate --config=drizzle.mysql.config.ts
 *
 * The explicit `db:generate:sqlite` / `db:generate:mysql` scripts call those
 * config files directly without the env-check — useful for CI that always
 * generates both.
 */

import { spawnSync } from 'node:child_process';

const driver = (process.env.DB_DRIVER ?? 'sqlite').toLowerCase();

if (driver !== 'sqlite' && driver !== 'mysql') {
  console.error(`Invalid DB_DRIVER="${driver}". Must be "sqlite" or "mysql".`);
  process.exit(1);
}

const configFile = driver === 'mysql' ? 'drizzle.mysql.config.ts' : 'drizzle.sqlite.config.ts';
console.log(`[db-generate] Using ${configFile} (DB_DRIVER=${driver})`);

const result = spawnSync('npx', ['drizzle-kit', 'generate', `--config=${configFile}`], {
  stdio: 'inherit',
  env: process.env,
});

process.exit(result.status ?? 1);
