// @vitest-environment jsdom
//
// FB-41 (P0 compliance, composition profile — ADR 013): the "Berater-
// Auslastung (letzte 90 Tage)" section must be hideable via the
// `consultantLoadReport` profile flag. Before this flag existed the section
// always rendered unconditionally, so "hidden when false" below is a
// genuine regression test — against the pre-flag component it fails (the
// heading would be found).
//
// FIXTURE-DATEN-REGEL: all props below are empty collections — this test
// only exercises the flag-gated section, not the data-driven rendering
// paths (those are covered by lib/reporting/__tests__/aggregations.test.ts).

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import ReportingDashboard from '@/components/reporting/reporting-dashboard'

// FB-41: handleExport() dynamically imports lib/export/export-service —
// mock it so the export-payload test below never touches jsPDF/PptxGenJS
// or triggers a real browser download.
const exportToPdfMock = vi.fn().mockResolvedValue(new Blob())
const exportToPptxMock = vi.fn().mockResolvedValue(new Blob())
const downloadBlobMock = vi.fn().mockResolvedValue(undefined)

vi.mock('@/lib/export/export-service', () => ({
  exportToPdf: exportToPdfMock,
  exportToPptx: exportToPptxMock,
  downloadBlob: downloadBlobMock,
}))

afterEach(() => {
  cleanup()
})

beforeEach(() => {
  vi.clearAllMocks()
})

function renderDashboard(consultantLoadReport: boolean) {
  return render(
    <ReportingDashboard
      projects={[]}
      oeeRecords={[]}
      assessments={[]}
      assessmentResponses={[]}
      assessmentQuestions={[]}
      mainCategories={[]}
      consultants={[]}
      assignments={[]}
      workshopActions={[]}
      processSteps={[]}
      lscMeasures={[]}
      oeeCapacity={[]}
      auditLogEntries={[]}
      consultantLoadReport={consultantLoadReport}
    />,
  )
}

describe('ReportingDashboard — consultantLoadReport flag (FB-41)', () => {
  it('hides the Berater-Auslastung section when the flag is false', () => {
    renderDashboard(false)
    expect(screen.queryByText('Berater-Auslastung (letzte 90 Tage)')).toBeNull()
  })

  it('shows the Berater-Auslastung section when the flag is true', () => {
    renderDashboard(true)
    expect(screen.getByText('Berater-Auslastung (letzte 90 Tage)')).toBeTruthy()
  })
})

// FB-41 export-level regression: the JSX-gate tests above only prove the
// on-screen section is hidden. The PDF/PPTX export builds its own
// `sections` array (reporting-dashboard.tsx, handleExport, ~line 604-620)
// gated by the SAME `consultantLoadReport` flag but as a *separate* code
// path — this is the "second, separately-testable spot" the task calls
// out. Without that gate, the first test below fails (section present).
describe('ReportingDashboard — export payload (FB-41)', () => {
  it('omits the Berater-Auslastung section from the export config when the flag is false', async () => {
    renderDashboard(false)
    fireEvent.click(screen.getByText('PDF Gesamtbericht'))
    await waitFor(() => expect(exportToPdfMock).toHaveBeenCalledTimes(1))
    const config = exportToPdfMock.mock.calls[0][0] as { sections: Array<{ title: string }> }
    expect(config.sections.some((s) => s.title === 'Berater-Auslastung (letzte 90 Tage)')).toBe(false)
  })

  it('includes the Berater-Auslastung section in the export config when the flag is true', async () => {
    renderDashboard(true)
    fireEvent.click(screen.getByText('PDF Gesamtbericht'))
    await waitFor(() => expect(exportToPdfMock).toHaveBeenCalledTimes(1))
    const config = exportToPdfMock.mock.calls[0][0] as { sections: Array<{ title: string }> }
    expect(config.sections.some((s) => s.title === 'Berater-Auslastung (letzte 90 Tage)')).toBe(true)
  })
})
