import fs from 'node:fs'
import path from 'node:path'
import { describe, expect, it } from 'vitest'

const ROOT = '/Users/nancy/Documents/AICoding/VS_Code/tvu-design-system'
const TARGET_DIRS = [
  path.join(ROOT, 'src/components'),
  path.join(ROOT, 'src/canonical'),
]

const ALLOWED_VHTML_FILES = new Set([
  path.join(ROOT, 'src/components/Icon/Icon.vue'),
  path.join(ROOT, 'src/components/Logo/Logo.vue'),
])

function collectVueFiles(dir: string, files: string[] = []) {
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
    const fullPath = path.join(dir, entry.name)
    if (entry.isDirectory()) {
      collectVueFiles(fullPath, files)
      continue
    }
    if (entry.isFile() && fullPath.endsWith('.vue')) {
      files.push(fullPath)
    }
  }
  return files
}

describe('icon usage conformance', () => {
  it('keeps runtime components on the shared Icon component instead of raw svg maps', () => {
    const vueFiles = TARGET_DIRS.flatMap((dir) => collectVueFiles(dir))

    for (const file of vueFiles) {
      const source = fs.readFileSync(file, 'utf8')

      if (!ALLOWED_VHTML_FILES.has(file)) {
        expect(source, `${file} should not render raw svg markup with v-html`).not.toMatch(/v-html=/)
      }

      expect(source, `${file} should not import generated icon maps directly`).not.toMatch(
        /from ['"].*icons\/catalog\/generated\//,
      )
      expect(source, `${file} should not import raw icon constants directly`).not.toMatch(
        /from ['"].*icons\/raw['"]/,
      )
    }
  })
})
