import { z } from 'zod';

const schema = z.object({
  NODE_ENV: z.enum(['development', 'production']).default('production'),
  PORT: z.coerce.number().default(9503),
  HOST: z.string().default('100.114.205.53'),
  DATABASE_URL: z.string(),
  LOG_LEVEL: z.enum(['fatal', 'error', 'warn', 'info', 'debug', 'trace']).default('info'),
  RETENTION_RAW_HOURS: z.coerce.number().default(48),
  RETENTION_ROLLUP_DAYS: z.coerce.number().default(90),
  CAPTURE_SIZE_KB: z.coerce.number().default(256),
  CAPTURE_BUDGET_MB: z.coerce.number().default(50),
  LLAMA_PROVIDERS_PATH: z.string().optional(),
  LLAMA_SWAP_URL: z.string().default('http://localhost:8080'),
  // P9.1: path to the llama-swap config-schema.json (fork). Defaults to the
  // copy bundled under dist/data; override to point at the live fork schema.
  LLAMA_CONFIG_SCHEMA_PATH: z.string().optional(),
});

export type Config = z.infer<typeof schema>;

export function loadConfig(): Config {
  const result = schema.safeParse(process.env);
  if (!result.success) {
    console.error('Invalid env:', result.error.message);
    process.exit(1);
  }
  return result.data;
}
