import { describe, expect, it } from 'vitest'
import JSZip from 'jszip'
import { buildProjectCopilotDocx } from '../internal/project-docx'
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,
  }
}

async function documentXml(buf: Buffer): Promise<string> {
  const zip = await JSZip.loadAsync(buf)
  const entry = zip.file('word/document.xml')
  if (!entry) throw new Error('word/document.xml missing from generated DOCX')
  return entry.async('string')
}

describe('buildProjectCopilotDocx', () => {
  it('produces a real DOCX with Stammdaten, Agenda, Maßnahmen, and Modul-Kurzfassungen', async () => {
    const buf = await buildProjectCopilotDocx(input())
    expect(Buffer.isBuffer(buf)).toBe(true)

    const xml = await documentXml(buf)
    expect(xml).toContain('Gesamtprojekt-Export')
    expect(xml).toContain('Musterzulieferer GmbH')
    expect(xml).toContain('Begrüßung')
    expect(xml).toContain('Werkzeugwechselzeit reduzieren')
    expect(xml).toContain('Wertstrom Linie 3')
    expect(xml).not.toContain('Demo-Hinweis')
  })

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

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