import { describe, it, expect } from 'vitest'
import { findForbidden } from '../extract.js'

describe('extract-doc / findForbidden — i18n JSON', () => {
  it('flags benefits / beforeAfter section-label keys', () => {
    const json = `{
  "sections": {
    "objective": "Objectif",
    "benefits": "Benefices concrets",
    "beforeAfter": "Avant / Apres",
    "faq": "Questions frequentes"
  }
}`
    const hits = findForbidden(json, 'src/i18n/locales/fr/docs-support-sla.json')
    expect(hits.map((h) => h.token).sort()).toEqual(['beforeAfter', 'benefits'])
    // line numbers are 1-based and point at the offending key
    expect(hits.every((h) => h.line > 0)).toBe(true)
  })

  it('flags overview.problem / overview.solution keys', () => {
    const json = `{
  "overview": {
    "objective": "Gestion des SLA",
    "problem": "Les engagements ne sont pas suivis",
    "solution": "Monitoring automatique"
  }
}`
    const hits = findForbidden(json, 'fr/docs-support-sla.json')
    expect(hits.map((h) => h.token).sort()).toEqual(['problem', 'solution'])
  })

  it('does NOT flag a FAQ answer that merely contains the word "solution"', () => {
    const json = `{
  "faq": {
    "1": {
      "question": "Quel est le probleme resolu ?",
      "answer": "La solution couvre le suivi automatique des SLA."
    }
  }
}`
    expect(findForbidden(json, 'fr/docs-support-sla.json')).toEqual([])
  })

  it('reports EVERY forbidden key on a single compact line', () => {
    const json = `{ "overview": { "objective": "x", "problem": "p", "solution": "s" } }`
    const hits = findForbidden(json, 'fr/docs-support-sla.json')
    expect(hits.map((h) => h.token).sort()).toEqual(['problem', 'solution'])
    expect(hits.every((h) => h.line === 1)).toBe(true)
  })

  it('returns nothing for a clean i18n file', () => {
    const json = `{
  "title": "SLA",
  "objective": "Description factuelle",
  "access": { "navigation": "Navigation" },
  "features": { "1": { "title": "Config", "description": "..." } }
}`
    expect(findForbidden(json, 'fr/docs-support-sla.json')).toEqual([])
  })
})

describe('extract-doc / findForbidden — doc-data.ts', () => {
  it('flags benefits / beforeAfter object keys', () => {
    const ts = `export const docData: DocData = {
  overview: { objective: 'docsSupportSla.overview.objective' },
  benefits: [
    { icon: 'time', value: '-70%' },
  ],
  beforeAfter: {
    beforeItems: ['Manual tracking'],
    afterItems: ['Automated'],
  },
}`
    const hits = findForbidden(ts, 'src/pages/docs/business/platform/support/sla/doc-data.ts')
    expect(hits.map((h) => h.token).sort()).toEqual(['beforeAfter', 'benefits'])
  })

  it('flags overview.problem / overview.solution object keys', () => {
    const ts = `export const docData: DocData = {
  overview: {
    objective: 'x',
    problem: 'x',
    solution: 'x',
  },
}`
    const hits = findForbidden(ts, 'sla/doc-data.ts')
    expect(hits.map((h) => h.token).sort()).toEqual(['problem', 'solution'])
  })

  it('returns nothing for a clean doc-data.ts', () => {
    const ts = `export const docData: DocData = {
  overview: { objective: 'x', stats: { useCases: 4 } },
  useCases: [{ id: 'UC-001', name: 'Create' }],
  features: [{ id: 'F-001', title: 'Config' }],
}`
    expect(findForbidden(ts, 'sla/doc-data.ts')).toEqual([])
  })
})

describe('extract-doc / findForbidden — user TSX', () => {
  it('flags a t() reference to a benefits/beforeAfter/problem/solution key', () => {
    const tsx = `export default function SlaDocPage() {
  const { t } = useTranslation('docsSupportSla')
  return (
    <section>
      <h2>{t('sections.benefits')}</h2>
      <p>{t('overview.solution')}</p>
    </section>
  )
}`
    const hits = findForbidden(tsx, 'src/pages/docs/business/platform/support/sla/index.tsx')
    expect(hits.map((h) => h.token).sort()).toEqual(['benefits', 'solution'])
  })

  it('flags a hardcoded marketing section title (Avant / Après)', () => {
    const tsx = `<h2 className="text-lg">Avant / Après</h2>`
    const hits = findForbidden(tsx, 'sla/index.tsx')
    expect(hits).toHaveLength(1)
    expect(hits[0].token).toBe('literal-title')
  })

  it('does NOT flag a normal annotated Mock UI page', () => {
    const tsx = `export default function SlaDocPage() {
  const { t } = useTranslation('docsSupportSla')
  return (
    <div className="space-y-8 max-w-5xl mx-auto pb-12">
      <h1>{t('title')}</h1>
      <h2>{t('sections.objective')}</h2>
      <p>{t('objective')}</p>
      <Annotation>{t('interface.annotations.element1')}</Annotation>
    </div>
  )
}`
    expect(findForbidden(tsx, 'sla/index.tsx')).toEqual([])
  })
})
