import { OpenPanelComponent } from "@openpanel/nextjs";

import { opApiUrl, opClientId } from "@/lib/env";

/**
 * OpenPanel analytics — the simple, first-party setup (same as our acieb-v2
 * site, which runs this in production).
 *
 * - `scriptUrl="/op1.js"`: the recorder is served as a static file from
 *   `public/op1.js`, so it's first-party and adblockers can't block it. The
 *   i18n middleware ignores it (the matcher excludes any path with a dot).
 * - `apiUrl`: events POST straight to the self-hosted instance. CORS on the
 *   instance allows our origins (seoinsight.app, www, localhost). No Next
 *   route in the path, so the locale middleware never rewrites `/track`.
 *
 * (The earlier `/api/op` proxy was a mistake: the i18n middleware rewrote
 * `POST /api/op/track` to `/en/api/op/track` → 405, and it 404'd on
 * `op1-replay.js`. Going direct avoids both.)
 *
 * `clientId`/`apiUrl` are public (OpenPanel's docs) and come from
 * `NEXT_PUBLIC_*`. Renders nothing when unconfigured.
 *
 * Session replay is intentionally OFF: with the static-script setup it would
 * fetch a separate `/op1-replay.js` recorder we don't yet self-host (it 404'd
 * and, under dev StrictMode, fired twice — the "doublon"). To re-enable later,
 * drop a first-party `public/op1-replay.js` and add
 * `sessionReplay={{ enabled: true }}` back.
 */
export function OpenPanelAnalytics() {
  if (!opClientId) return null;

  return (
    <OpenPanelComponent
      clientId={opClientId}
      apiUrl={opApiUrl}
      scriptUrl="/op1.js"
      trackScreenViews
      trackOutgoingLinks
      trackAttributes
    />
  );
}
