#!/usr/bin/env node
/**
 * Rewrite monorepo-shaped Cursor rule bodies for consumer app layouts.
 * Used by vendor-consumer-extras.mjs (npm bundle) and sync-extras.mjs (install).
 */

const CONSUMER_CODE_GLOB = "{components,lib,src}/**/*.{tsx,ts}"

/** @param {string} line */
function rewriteGlobsLine(line) {
  if (!line.startsWith("globs:")) return line

  let globs = line.slice("globs:".length).trim()

  // Drop monorepo-only package paths — consumers don't ship packages/ui.
  globs = globs
    .replace(/,\s*packages\/ui\/(?:src\/\*\*\/\*\.\{tsx,ts\}|\*\*\/\*\.\{tsx,ts\})/g, "")
    .replace(/,?\s*packages\/ui\/[^,]*/g, "")

  // Monorepo app roots → consumer Vite layout.
  globs = globs
    .replace(/apps\/web\/\{components,lib,app\}\/\*\*\/\*\.\{tsx,ts\}/g, CONSUMER_CODE_GLOB)
    .replace(/apps\/web\/\{components,app,lib\}\/\*\*\/\*\.\{tsx,ts\}/g, CONSUMER_CODE_GLOB)
    .replace(/apps\/web\/\{components,lib\}\/\*\*\/\*\.\{tsx,ts\}/g, CONSUMER_CODE_GLOB)
    .replace(/apps\/web\/\{components,app\}\/\*\*\/\*\.\{tsx,ts\}/g, CONSUMER_CODE_GLOB)
    .replace(/apps\/web\/\{components,packages\/ui\}\/\*\*\/\*\.\{tsx,ts\}/g, CONSUMER_CODE_GLOB)
    .replace(/apps\/web\/components\/\*\*\/\*\.\{tsx,ts\}/g, CONSUMER_CODE_GLOB)
    .replace(/apps\/web\/components\/\*\*\.tsx/g, CONSUMER_CODE_GLOB)
    .replace(/apps\/web\/components\/library-\*\.tsx/g, "components/library-*.tsx")
    .replace(/apps\/web\/components\/\*\*\/\*key-metrics\*/g, "components/**/*key-metrics*")
    .replace(/apps\/web\/\*\*\/\*\.\{tsx,ts\}/g, CONSUMER_CODE_GLOB)
    .replace(/apps\/web\/\*\*\/\*\.tsx/g, CONSUMER_CODE_GLOB)

  // Docs + globals in consumer tree.
  globs = globs
    .replace(/apps\/web\/app\/globals\.css/g, "src/styles/globals.css")
    .replace(/apps\/web\/docs\//g, "docs/exxat-ds/")

  // Normalize duplicate commas / trailing commas.
  globs = globs.replace(/,\s*,/g, ",").replace(/,\s*$/g, "").trim()

  return `globs: ${globs}`
}

/** @param {string} content */
export function rewriteConsumerRule(content, { filename = "" } = {}) {
  let out = content

  // Glob rules — line-by-line so compound globs are handled.
  out = out
    .split("\n")
    .map((line) => rewriteGlobsLine(line))
    .join("\n")

  // Path references in prose + mdc links.
  out = out.replace(/apps\/web\/\.cursor\/rules\//g, ".cursor/rules/")
  out = out.replace(/apps\/web\/docs\//g, "docs/exxat-ds/")
  out = out.replace(/apps\/web\/AGENTS\.md/g, "./AGENTS.md")
  out = out.replace(/`apps\/web\/`/g, "`./`")
  out = out.replace(/\(mdc:apps\/web\//g, "(mdc:")
  out = out.replace(/apps\/web\/components\//g, "components/")
  out = out.replace(/apps\/web\/lib\//g, "lib/")
  out = out.replace(/apps\/web\/stores\//g, "stores/")
  out = out.replace(/apps\/web\/contexts\//g, "contexts/")
  out = out.replace(/apps\/web\/app\//g, "src/")
  out = out.replace(/apps\/web\/\.cursor\/rules\//g, ".cursor/rules/")

  // exxat-ds-agents: consumer apps ship product-context + patterns in sync-extras.
  if (filename === "exxat-ds-agents.mdc") {
    out = out.replace(
      /\*\*App location:\*\* `apps\/web\/` in this \*\*pnpm monorepo\*\*[\s\S]*?Shared design system lives at `packages\/ui\/` \(`@exxatdesignux\/ui`\)\./,
      "**App location:** This **consumer app** (`./`) built on `@exxatdesignux/ui`. Shared design system lives in `node_modules/@exxatdesignux/ui`.",
    )
    out = out.replace(
      /\*\*Read `AGENTS\.md`:\*\* `apps\/web\/AGENTS\.md` from the monorepo root, or `\.\/AGENTS\.md` when the workspace is the `apps\/web\/` folder only \(same file\)\./,
      "**Read `AGENTS.md`:** `./AGENTS.md` in this app folder. Pattern docs live under `docs/exxat-ds/`.",
    )
    out = out.replace(
      /\*\*Internal-only \(this monorepo, not yet vendored to consumer apps\):\*\*[\s\S]*?- `\*\*apps\/web\/docs\/agent-context\/\*\*` — `domain\.md`[\s\S]*?Source of truth for the rule and skill above\./,
      "**Consumer extras (synced by `exxat-ui sync-extras`):**\n\n- **`.cursor/rules/exxat-product-context.mdc`** — Product / Scope / Persona brief lines.\n- **`.cursor/skills/exxat-domain-context/SKILL.md`** — persona + RBAC quick reference.\n- **`docs/exxat-ds/`** — patterns, handbook, upgrade checklist.",
    )
    out = out.replace(
      /- `\*\*apps\/web\/docs\/modern-saas-patterns\.md\*\*`/,
      "- **`docs/exxat-ds/handbook/modern-saas-patterns.md`** (or GitHub source)",
    )
    out = out.replace(
      /- `\*\*apps\/web\/docs\/jobs\/\*\*`/,
      "- **`docs/exxat-ds/jobs/`**",
    )
  }

  if (filename === "exxat-product-context.mdc") {
    out = out.replace(
      /from `apps\/web\/docs\/agent-context\/personas\.md`/g,
      "from `.cursor/skills/exxat-domain-context/SKILL.md` (persona table)",
    )
    out = out.replace(
      /`apps\/web\/docs\/agent-context\//g,
      "`docs/exxat-ds/agent-context/` (GitHub: Exxat-DS-Workspace `apps/web/docs/agent-context/`)",
    )
    out = out.replace(
      /\[`ProductSwitcher`\]\(mdc:apps\/web\/components\/product-switcher\.tsx\)/,
      "[`ProductSwitcher`](mdc:components/product-switcher.tsx)",
    )
  }

  return out
}
