import { writeFileSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const outPath = resolve(__dirname, "../packages/shared/src/analytics/baked.ts");

const mode = process.argv[2] || "on";

const enabled = mode === "on";
const posthogApiKey = enabled ? "phc_REPLACE_WITH_REAL_KEY" : "";
const posthogHost = enabled ? "https://us.i.posthog.com" : "";
const sentryDsn = enabled
  ? "https://REPLACE_WITH_REAL_DSN@o0.ingest.us.sentry.io/0"
  : "";
const sampleRate = enabled ? 1 : 0;

const content = `// AUTO-GENERATED by scripts/bake-analytics.mjs -- do not edit manually
export const ANALYTICS_BAKED = {
  enabled: ${enabled},
  posthogApiKey: "${posthogApiKey}",
  posthogHost: "${posthogHost}",
  sentryDsn: "${sentryDsn}",
  sampleRate: ${sampleRate},
} as const;
`;

writeFileSync(outPath, content, "utf-8");
console.log(`bake-analytics: wrote ${outPath} (mode=${mode}, enabled=${enabled})`);
