import assert from 'node:assert/strict'
import test from 'node:test'

import {
  applyAiApplicationAnswer,
  buildAiApplicationStepPrompt,
  buildAiApplicationSummary,
  createAiApplicationDraft,
  getAiApplicationCurrentStep,
  isAiApplicationDraftComplete
} from '../src/utils/aiApplicationDraftModel.js'

test('application draft starts at the reason step', () => {
  const draft = createAiApplicationDraft('travel', '差旅费')
  assert.equal(draft.expenseType, 'travel')
  assert.equal(draft.expenseTypeLabel, '差旅费')
  assert.equal(draft.stepKey, 'reason')
  assert.equal(getAiApplicationCurrentStep(draft).key, 'reason')
})

test('answers advance through fields in order and reach summary', () => {
  let draft = createAiApplicationDraft('travel', '差旅费')
  draft = applyAiApplicationAnswer(draft, '去上海支持项目部署', [])
  assert.equal(draft.stepKey, 'time_range')
  draft = applyAiApplicationAnswer(draft, '2026-06-20 至 2026-06-22，出差 3 天', [])
  assert.equal(draft.stepKey, 'location')
  draft = applyAiApplicationAnswer(draft, '上海', [])
  assert.equal(draft.stepKey, 'amount')
  draft = applyAiApplicationAnswer(draft, '约 2358 元', [])
  assert.ok(isAiApplicationDraftComplete(draft))
})

test('step prompt names the type and the current field', () => {
  const draft = createAiApplicationDraft('travel', '差旅费')
  const prompt = buildAiApplicationStepPrompt(draft)
  assert.match(prompt, /差旅费/)
  assert.match(prompt, /事由/)
})

test('summary lists every filled field', () => {
  let draft = createAiApplicationDraft('travel', '差旅费')
  draft = applyAiApplicationAnswer(draft, '去上海支持项目部署', [])
  draft = applyAiApplicationAnswer(draft, '2026-06-20 至 2026-06-22，出差 3 天', [])
  draft = applyAiApplicationAnswer(draft, '上海', [])
  draft = applyAiApplicationAnswer(draft, '约 2358 元', [])
  const summary = buildAiApplicationSummary(draft)
  assert.match(summary, /差旅费/)
  assert.match(summary, /去上海支持项目部署/)
  assert.match(summary, /2026-06-20 至 2026-06-22，出差 3 天/)
  assert.match(summary, /约 2358 元/)
})
