import { describe, expect, it } from 'vitest'
import { buildProjectCopilotMd } from '../internal/project-md'
import type { ProjectExportInput } from '../internal/project-types'

function input(overrides: Partial<ProjectExportInput> = {}): ProjectExportInput {
  return {
    stammdaten: {
      supplierName: 'Musterzulieferer GmbH',
      plantLocation: 'Musterstadt',
      productName: 'Achsträger',
      projectCode: 'PRJ-001',
      vehicleProject: 'G99',
      productLine: 'Fahrwerk',
      visitDate: '2026-07-01',
      orderReceivedDate: '2026-06-01',
      expectedEndDate: '2026-12-01',
      customerTaktTimeSec: 60,
      targetCycleTimeSec: 55,
      plannedOee: 85,
      notes: 'Testnotiz',
      isDemo: false,
    },
    agendas: [
      {
        title: 'Werksbesuch Juli',
        language: 'de',
        status: 'final',
        days: [
          {
            title: 'Tag 1',
            date: '2026-07-01',
            items: [{ startTime: '09:00', endTime: '10:00', title: 'Begrüßung & Rundgang', responsible: 'Team Lead', location: 'Halle 1' }],
          },
        ],
      },
    ],
    workshopActions: [
      {
        actionNumber: 1,
        area: 'Montage',
        description: 'Werkzeugwechselzeit reduzieren',
        effort: 'middle',
        benefit: 'high',
        status: 'open',
        responsible: 'M. Muster',
        targetDate: '2026-08-01',
      },
    ],
    moduleSummaries: [
      { label: 'Fabrikanalyse', countLabel: '1 Assessment', items: ['Werksbesuch Musterwerk — Ø 2,80 (In Bearbeitung)'] },
      { label: 'Wertstrom', countLabel: '1 Wertstrom', items: ['Wertstrom Linie 3 — 5 Prozessschritte'] },
    ],
    generatedAtLabel: '19.07.2026, 10:00',
    ...overrides,
  }
}

describe('buildProjectCopilotMd', () => {
  it('produces Markdown with Stammdaten, Agenda, Maßnahmen, and Modul-Kurzfassungen', () => {
    const md = buildProjectCopilotMd(input())
    expect(typeof md).toBe('string')

    expect(md).toContain('# Gesamtprojekt-Export')
    expect(md).toContain('## Hinweis für die KI-gestützte Weiterverarbeitung — Gesamtprojekt-Export')
    expect(md).toContain('Musterzulieferer GmbH')
    expect(md).toContain('Begrüßung')
    expect(md).toContain('Werkzeugwechselzeit reduzieren')
    expect(md).toContain('Wertstrom Linie 3')
    // Steckbrief (kvTable equivalent) pipe-table header.
    expect(md).toContain('| Feld | Wert |')
    expect(md).toContain('| --- | --- |')
    expect(md).not.toContain('Demo-Hinweis')
  })

  it('renders the fictional-data disclaimer only when the project is_demo is true', () => {
    const withDemo = buildProjectCopilotMd(input({ stammdaten: { ...input().stammdaten, isDemo: true } }))
    const withoutDemo = buildProjectCopilotMd(input())
    expect(withDemo).toContain('Demo-Hinweis')
    expect(withoutDemo).not.toContain('Demo-Hinweis')
  })

  it('handles a project with no agenda/actions/module data without throwing', () => {
    const md = buildProjectCopilotMd(input({ agendas: [], workshopActions: [], moduleSummaries: [] }))
    expect(md).toContain('Keine Agenda angelegt.')
    expect(md).toContain('Keine Maßnahmen erfasst.')
    expect(md).toContain('Keine Modul-Daten für dieses Projekt.')
  })

  it('escapes pipes/newlines in free-text action descriptions on the real production path (PR #348 review fix)', () => {
    const md = buildProjectCopilotMd(
      input({
        workshopActions: [
          {
            actionNumber: 1,
            area: 'Montage',
            description: 'Rüstzeit reduzieren | Werkzeugwechsel\nsiehe Protokoll',
            effort: 'middle',
            benefit: 'high',
            status: 'open',
            responsible: 'M. Muster',
            targetDate: '2026-08-01',
          },
        ],
      }),
    )
    const actionLine = md.split('\n').find((l) => l.includes('Rüstzeit reduzieren'))
    expect(actionLine).toBeDefined()
    expect(actionLine).toContain('Rüstzeit reduzieren \\| Werkzeugwechsel siehe Protokoll')
  })
})
