import { registerAs } from "@nestjs/config";

const numberFromEnv = (key: string, fallback: number): number => {
  const value = Number(process.env[key]);
  return Number.isFinite(value) && value > 0 ? value : fallback;
};

export default registerAs("app", () => ({
  env: process.env.NODE_ENV ?? "development",
  port: parseInt(process.env.PORT ?? "3001", 10),
  apiPrefix: "api",
  trainScheduling: {
    maxTrainWeightTons: numberFromEnv("TRAIN_SCHEDULING_MAX_WEIGHT_TONS", 3500),
    maxTrainLengthMeters: numberFromEnv("TRAIN_SCHEDULING_MAX_LENGTH_METERS", 760),
    maxWagonsPerTrain: numberFromEnv("TRAIN_SCHEDULING_MAX_WAGONS_PER_TRAIN", 53),
  },
  // Consumed by @edr/api-common ExchangeModule.forRootAsync (see bookings.module.ts).
  cbeExchange: {
    /** ethio.forex CBET page — scraped for USD buying/selling rates. */
    scrapeUrl:
      process.env.CBE_EXCHANGE_SCRAPE_URL ??
      process.env.CBE_EXCHANGE_API_URL ??
      "https://ethio.forex/bank/CBET",
    fallbackRate: numberFromEnv("CBE_EXCHANGE_FALLBACK_RATE", 130),
    cacheTtlMs: numberFromEnv("CBE_EXCHANGE_CACHE_TTL_MS", 3_600_000),
  },
}));
