/**
 * `wrnexus profiles` — list the config profiles defined in `wrnexus.config.ts`,
 * mark the active one, and show which `.env` files exist for each.
 */

import { existsSync } from "node:fs";
import { join, resolve } from "node:path";
import { loadRawConfig, resolveProfile } from "@wrnexus/styles";

export async function listProfiles(appRoot: string): Promise<void> {
  const root = resolve(appRoot);
  const config = await loadRawConfig(root);
  const active = resolveProfile();
  const defined = Object.keys(config.profiles ?? {});
  // Always show the two conventional profiles plus any custom ones.
  const names = Array.from(new Set(["development", "production", ...defined]));

  console.log("Profiles (select with --profile=<name> or WRNEXUS_PROFILE):\n");
  for (const name of names) {
    const marker = name === active ? "●" : "○";
    const hasConfig = defined.includes(name) ? "config" : "";
    const envFiles = [`.env.${name}`, `.env.${name}.local`].filter((f) =>
      existsSync(join(root, f)),
    );
    const bits = [hasConfig, ...envFiles].filter(Boolean).join(", ");
    console.log(`  ${marker} ${name.padEnd(14)}${bits ? "  (" + bits + ")" : ""}`);
  }
  const baseEnv = [".env", ".env.local"].filter((f) => existsSync(join(root, f)));
  if (baseEnv.length) console.log(`\n  base env: ${baseEnv.join(", ")} (loaded for every profile)`);
  console.log(`\n  active: ${active}`);
}
