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

describe('time approval schemas', () => {
  it('accepts bulk approve/reject payloads up to 200 entry ids', async () => {
    const { bulkReviewTimeEntriesSchema } = await import('../src/queries/time-approval')
    const entryIds = Array.from({ length: 200 }, (_, i) =>
      `00000000-0000-4000-8000-${String(i).padStart(12, '0')}`,
    )

    expect(
      bulkReviewTimeEntriesSchema.safeParse({
        action: 'approve',
        entry_ids: entryIds,
      }).success,
    ).toBe(true)

    expect(
      bulkReviewTimeEntriesSchema.safeParse({
        action: 'reject',
        entry_ids: entryIds,
        note: 'Missing project code',
      }).success,
    ).toBe(true)
  })

  it('rejects bulk reject without a note', async () => {
    const { bulkReviewTimeEntriesSchema } = await import('../src/queries/time-approval')

    expect(
      bulkReviewTimeEntriesSchema.safeParse({
        action: 'reject',
        entry_ids: ['00000000-0000-4000-8000-000000000001'],
      }).success,
    ).toBe(false)
  })

  it('rejects reject notes longer than 500 characters', async () => {
    const { rejectTimeEntrySchema } = await import('../src/queries/time-approval')

    expect(
      rejectTimeEntrySchema.safeParse({ note: 'x'.repeat(501) }).success,
    ).toBe(false)
  })
})

describe('time approval CSV export', () => {
  beforeEach(() => {
    vi.restoreAllMocks()
  })

  it('includes only approved rows and emits the payroll header order from the spec', async () => {
    const { buildPayrollExportCsv } = await import('../src/queries/time-approval')

    const csv = buildPayrollExportCsv([
      {
        id: 'entry-1',
        entryDate: '2026-05-29',
        personName: 'Dana Levi',
        personEmail: 'dana@zync.is',
        projectName: 'Website Redesign',
        taskTitle: 'Implement auth',
        hours: 6,
        rate: 150,
        amount: 900,
        source: 'manual',
      },
    ])

    expect(csv.split('\n')[0]).toBe(
      'date,person_name,person_email,project,task,hours,rate,amount,source,entry_id',
    )
    expect(csv).toContain('2026-05-29,Dana Levi,dana@zync.is,Website Redesign,Implement auth,6,150,900,manual,entry-1')
  })
})
