import { existsSync, readdirSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'

const SRC = join(__dirname)
const PKG_ROOT = join(__dirname, '..')
const FORBIDDEN = [
  'StripePaymentProvider',                 // host mounts it; importing here breaks PSP-neutrality (§0.3)
  '@stripe/',                              // no direct Stripe SDK dependency
  '@platform-modules/commerce-checkout/start', // server-only deep import (db/charge)
  'startCheckout',                         // server orchestrator — never in the browser sibling
  '@platform-modules/db',                  // no db handle in the browser
]

function srcFiles(dir: string): string[] {
  return readdirSync(dir, { withFileTypes: true }).flatMap((e) => {
    const p = join(dir, e.name)
    if (e.isDirectory()) return srcFiles(p)
    return /\.(ts|tsx)$/.test(e.name) && !/\.test\.(ts|tsx)$/.test(e.name) ? [p] : []
  })
}

function distOffenders(text: string, label: string): string[] {
  const offenders: string[] = []
  for (const token of FORBIDDEN) {
    if (text.includes(token)) offenders.push(`${label} → ${token}`)
  }
  return offenders
}

function scanForForbidden(files: string[]): string[] {
  const offenders: string[] = []
  for (const f of files) {
    const text = readFileSync(f, 'utf8')
    for (const line of text.split('\n')) {
      const trimmed = line.trim()
      if (!trimmed.startsWith('import') && !/\bfrom\s+['"]/.test(trimmed)) continue
      for (const token of FORBIDDEN) {
        if (line.includes(token)) offenders.push(`${f} → ${token}`)
      }
    }
  }
  return offenders
}

describe('no server-only / non-neutral imports leak into the browser sibling (§0.3 bundle guard)', () => {
  it('no src file imports StripePaymentProvider / @stripe / server-only core', () => {
    const offenders = scanForForbidden(srcFiles(SRC))
    expect(offenders, offenders.join('\n')).toEqual([])
  })

  it('built dist bundle carries no StripePaymentProvider / @stripe / server-only core when dist is present', () => {
    const distJs = join(PKG_ROOT, 'dist', 'index.js')
    if (!existsSync(distJs)) return

    const js = readFileSync(distJs, 'utf8')
    const offenders = distOffenders(js, 'dist/index.js')
    expect(offenders, offenders.join('\n')).toEqual([])
  })
})