// Server-side deny paths of exportProjectCopilotDocx/Xlsx (PR #346 review
// fix). Mirrors app/wertstrom/__tests__/qaf-actions.test.ts's mocking: the
// flag gate must reject BEFORE any DB access — createClient throws if reached.

import { describe, it, expect, vi, beforeEach } from 'vitest'

const VALID_UUID = '44444444-4444-4444-8444-444444444444'

let copilotExportsEnabled = false
const createClientMock = vi.fn(() => {
  throw new Error('createClient must not be called on a denied export')
})

vi.mock('@/config/profiles', () => ({
  getProfile: vi.fn(() => ({ features: { copilotExports: copilotExportsEnabled } })),
}))
vi.mock('@/lib/supabase/server', () => ({
  createClient: () => createClientMock(),
}))
vi.mock('@/lib/agenda/server-queries', () => ({
  loadProjectAgendas: vi.fn(() => {
    throw new Error('loadProjectAgendas must not be called on a denied export')
  }),
}))

import { exportProjectCopilotDocx, exportProjectCopilotMd, exportProjectCopilotXlsx } from '../[id]/export/actions'

describe('exportProjectCopilotDocx/Xlsx — server-side deny paths', () => {
  beforeEach(() => {
    copilotExportsEnabled = false
    createClientMock.mockClear()
  })

  it('DOCX: rejects with ok:false when the copilotExports flag is disabled — no DB access, no buffer', async () => {
    const res = await exportProjectCopilotDocx(VALID_UUID, '19.07.2026')
    expect(res.ok).toBe(false)
    if (!res.ok) expect(res.error.length).toBeGreaterThan(0)
    expect(createClientMock).not.toHaveBeenCalled()
  })

  it('XLSX: rejects with ok:false when the copilotExports flag is disabled — no DB access, no buffer', async () => {
    const res = await exportProjectCopilotXlsx(VALID_UUID, '19.07.2026')
    expect(res.ok).toBe(false)
    if (!res.ok) expect(res.error.length).toBeGreaterThan(0)
    expect(createClientMock).not.toHaveBeenCalled()
  })

  it('rejects an invalid project id before flag/DB (both variants)', async () => {
    copilotExportsEnabled = true
    const docx = await exportProjectCopilotDocx('not-a-uuid', '19.07.2026')
    const xlsx = await exportProjectCopilotXlsx('not-a-uuid', '19.07.2026')
    expect(docx).toEqual({ ok: false, error: 'invalid project id' })
    expect(xlsx).toEqual({ ok: false, error: 'invalid project id' })
    expect(createClientMock).not.toHaveBeenCalled()
  })
})

describe('exportProjectCopilotMd — server-side deny paths (Demo-067 E, KAR-984)', () => {
  beforeEach(() => {
    copilotExportsEnabled = false
    createClientMock.mockClear()
  })

  it('rejects with ok:false when the copilotExports flag is disabled — no DB access, no buffer', async () => {
    const res = await exportProjectCopilotMd(VALID_UUID, '19.07.2026')
    expect(res.ok).toBe(false)
    if (!res.ok) expect(res.error.length).toBeGreaterThan(0)
    expect(createClientMock).not.toHaveBeenCalled()
  })

  it('rejects an invalid project id before flag/DB', async () => {
    copilotExportsEnabled = true
    const res = await exportProjectCopilotMd('not-a-uuid', '19.07.2026')
    expect(res).toEqual({ ok: false, error: 'invalid project id' })
    expect(createClientMock).not.toHaveBeenCalled()
  })
})
