import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { ACTION_REGISTRY } from '../domains/prospecting'
import { getLeadGenStageCatalog } from '../migration-helpers'

/**
 * OM Spine doc <-> code contract guard.
 *
 * `scripts/monorepo/meta-verify.mjs` only string-greps the primer for symbol
 * names, so it cannot notice when a documented "constant" has become a
 * type-only export or an empty stub. This drift actually shipped (the Wave-2
 * generic/canonical OM split) and went uncaught for that reason. These
 * assertions tie the primer text to the real code shape so the same class of
 * drift fails the unit suite. Pure file reads + pure imports — CI-safe.
 */

const repoRoot = fileURLToPath(new URL('../../../../../', import.meta.url))
const catalogTypeSrc = fileURLToPath(new URL('../catalogs/lead-gen.ts', import.meta.url))

const PRIMER_PATHS = [
  'apps/docs/content/docs/technical/development/design/om-spine.mdx',
  '.claude/skills/org-os/operations/spine.md'
]

const read = (p: string) => readFileSync(p, 'utf8')

describe('OM Spine doc/code contract', () => {
  it('keeps the generic core action registry an empty stub (bindings live in @repo/elevasis-core)', () => {
    expect(ACTION_REGISTRY).toEqual([])
  })

  it('exposes the lead-gen catalog only as a reader function, not a hand-authored constant', () => {
    expect(typeof getLeadGenStageCatalog).toBe('function')

    // catalogs/lead-gen.ts must remain type-only — no LEAD_GEN_STAGE_CATALOG value export.
    const src = read(catalogTypeSrc)
    expect(src).not.toMatch(/export\s+const\s+LEAD_GEN_STAGE_CATALOG\b/)
    expect(src).toMatch(/export\s+interface\s+LeadGenStageCatalogEntry\b/)
  })

  it.each(PRIMER_PATHS)('primer %s describes the post-split reality, not the retired constant', (relPath) => {
    const text = read(repoRoot + relPath)

    // Negative: the exact stale claims that shipped before this fix must not return.
    expect(text).not.toMatch(
      /`LEAD_GEN_STAGE_CATALOG`\s+in\s+`packages\/core\/src\/organization-model\/catalogs\/lead-gen\.ts`/
    )
    expect(text).not.toMatch(/LEAD_GEN_STAGE_CATALOG \(vocabulary, catalogs\/lead-gen\.ts\)/)

    // Positive: each primer must point at the real reader + canonical owners.
    expect(text).toContain('getLeadGenStageCatalog')
    expect(text).toContain('LEAD_GEN_ACTION_ENTRIES')
    expect(text).toContain('@repo/elevasis-core')
  })
})
