import { resolve } from "node:path";
import type { ConfigModuleOptions } from "@nestjs/config";
import { validate } from "./env.validation.js";

const nodeEnv = process.env["NODE_ENV"] || "development";

// pnpm sets cwd to the package directory (apps/bff) when running scripts
const bffRoot = process.cwd();

// Load env files in order of priority (first found wins)
// 1. .env.{NODE_ENV}.local - local overrides for specific environment
// 2. .env.local - local overrides (not committed)
// 3. .env.{NODE_ENV} - environment-specific
// 4. .env - default
export const appConfig: ConfigModuleOptions = {
  isGlobal: true,
  expandVariables: true,
  validate,
  envFilePath: [
    resolve(bffRoot, `.env.${nodeEnv}.local`),
    resolve(bffRoot, ".env.local"),
    resolve(bffRoot, `.env.${nodeEnv}`),
    resolve(bffRoot, ".env"),
  ],
};
