import { describe, it, expect } from 'vitest'
import { buildExportModel, buildExportFilename, formatExportDate } from '../export-model'
import { fixtureAgenda, fixtureCtx } from './fixtures'
import type { AgendaProjectContext } from '../types'

describe('buildExportModel', () => {
  it('splits participants into host and supplier/external groups', () => {
    const agenda = fixtureAgenda({
      participants: [
        ...fixtureAgenda().participants,
        { id: 'pa3', agenda_id: 'a1', person_id: null, name: 'Ext Consultant', company: 'X', department: null, department_id: null, position: null, role: null, participant_type: 'external', email: null, comment: null, is_auto_imported_from_project: false, sort_order: 2 },
      ],
    })
    const m = buildExportModel(agenda, fixtureCtx())
    expect(m.hostParticipants.map((p) => p.name)).toEqual(['Anna Host'])
    expect(m.supplierParticipants.map((p) => p.name)).toEqual(['Pedro Supplier', 'Ext Consultant'])
  })

  it('builds localised column headers', () => {
    const m = buildExportModel(fixtureAgenda(), fixtureCtx())
    expect(m.columns.time).toBe('Uhrzeit')
    expect(m.columns.info).toContain('Kommentare')
  })

  it('uses the host org label for the host participant heading when provided', () => {
    const m = buildExportModel(fixtureAgenda(), fixtureCtx(), { label: 'Acme Motors', logoUrl: null })
    expect(m.labels.hostParticipants).toBe('Acme Motors')
  })

  it('falls back to a generic host heading without a host org', () => {
    const m = buildExportModel(fixtureAgenda(), fixtureCtx())
    expect(m.labels.hostParticipants).toBe('Gastgeber-Teilnehmer')
  })

  it('renders each item as a row with a time span and duration', () => {
    const m = buildExportModel(fixtureAgenda(), fixtureCtx())
    const row = m.days[0].rows[0]
    expect(row.time).toBe('08:30 – 09:30')
    expect(row.duration).toBe('60 min')
    expect(row.subject).toBe('Meet and Greet')
    expect(row.responsible).toBe('Anna')
    expect(row.info).toContain('bring badges')
  })

  it('keeps every day, including empty ones', () => {
    const m = buildExportModel(fixtureAgenda(), fixtureCtx())
    expect(m.days).toHaveLength(2)
    expect(m.days[1].rows).toHaveLength(0)
  })

  it('carries supplier + location from project context', () => {
    const m = buildExportModel(fixtureAgenda(), fixtureCtx())
    expect(m.supplierName).toBe('ACME GmbH')
    expect(m.location).toBe('München')
  })

  it('handles an agenda with no participants', () => {
    const m = buildExportModel(fixtureAgenda({ participants: [] }), fixtureCtx())
    expect(m.hostParticipants).toEqual([])
    expect(m.supplierParticipants).toEqual([])
  })
})

describe('buildExportFilename', () => {
  it('follows Agenda_[Supplier]_[Location]_[YYYYMMDD].<ext>', () => {
    expect(buildExportFilename(fixtureCtx(), fixtureAgenda(), 'pdf')).toBe('Agenda_ACME_GmbH_Muenchen_20260601.pdf')
  })

  it('sanitises unsafe characters and falls back when fields missing', () => {
    const bareCtx: AgendaProjectContext = { projectId: 'p', projectCode: null, supplierName: 'Bär/Süd AG', plantLocation: null, supplierLogoKeys: [] }
    const name = buildExportFilename(bareCtx, fixtureAgenda({ start_date: null }), 'xlsx')
    expect(name.startsWith('Agenda_Baer_Sued_AG_')).toBe(true)
    expect(name.endsWith('.xlsx')).toBe(true)
    expect(name).not.toMatch(/[/\\]/)
  })
})

describe('formatExportDate', () => {
  it('formats per language', () => {
    expect(formatExportDate('2026-06-01', 'de')).toBe('01.06.2026')
    expect(formatExportDate('2026-06-01', 'en')).toBe('06/01/2026')
    expect(formatExportDate('2026-06-01', 'zh')).toBe('2026-06-01')
  })

  it('returns empty string for null', () => {
    expect(formatExportDate(null, 'de')).toBe('')
  })
})
