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

import {
  isOrphanInlineApplicationPreviewMessage,
  isReimbursementCreationIntent,
  resolveInlineTravelApplicationRequest,
  resolveInlineApplicationPreviewTextAction,
  resolveLatestApplicationPreviewMessage,
  resolveLatestOrphanApplicationPreviewMessage
} from '../src/composables/workbenchAiMode/workbenchAiApplicationGateModel.js'
import {
  AI_APPLICATION_ACTION_SAVE_DRAFT,
  AI_APPLICATION_ACTION_SUBMIT
} from '../src/services/aiApplicationPreviewActions.js'

test('workbench application gate detects reimbursement creation without catching policy questions', () => {
  assert.equal(isReimbursementCreationIntent('我要报销'), true)
  assert.equal(isReimbursementCreationIntent('帮我新建一笔报账'), true)
  assert.equal(isReimbursementCreationIntent('报销一下'), true)

  assert.equal(isReimbursementCreationIntent('报销制度是什么'), false)
  assert.equal(isReimbursementCreationIntent('帮我查询报销进度'), false)
  assert.equal(isReimbursementCreationIntent('这张票能不能报销'), false)
})

test('workbench application gate resolves save and submit text actions consistently', () => {
  assert.equal(resolveInlineApplicationPreviewTextAction('保存草稿'), AI_APPLICATION_ACTION_SAVE_DRAFT)
  assert.equal(resolveInlineApplicationPreviewTextAction(' 先保存 '), AI_APPLICATION_ACTION_SAVE_DRAFT)
  assert.equal(resolveInlineApplicationPreviewTextAction('确认提交'), AI_APPLICATION_ACTION_SUBMIT)
  assert.equal(resolveInlineApplicationPreviewTextAction('直接提交'), AI_APPLICATION_ACTION_SUBMIT)
  assert.equal(resolveInlineApplicationPreviewTextAction('提交这个单据'), AI_APPLICATION_ACTION_SUBMIT)
  assert.equal(resolveInlineApplicationPreviewTextAction('提交这个申请单'), AI_APPLICATION_ACTION_SUBMIT)
  assert.equal(resolveInlineApplicationPreviewTextAction('继续修改'), '')
})

test('workbench application gate detects compact travel application direct submit intent', () => {
  const request = resolveInlineTravelApplicationRequest('去上海出差，辅助国网仿生产服务器部署，交通火车，直接提交')

  assert.deepEqual(request, {
    expenseType: 'travel',
    expenseTypeLabel: '差旅费',
    sourceText: '去上海出差，辅助国网仿生产服务器部署，交通火车，直接提交',
    autoSubmit: false,
    requestedSubmit: true
  })
  assert.equal(resolveInlineTravelApplicationRequest('去上海出差，辅助国网仿生产服务器部署，交通火车')?.autoSubmit, false)
  assert.equal(resolveInlineTravelApplicationRequest('去上海出差，辅助国网仿生产服务器部署，交通火车')?.requestedSubmit, false)
  assert.equal(resolveInlineTravelApplicationRequest('帮我查询上海差旅标准'), null)
})

test('workbench application gate resolves latest live or orphan preview message', () => {
  const messages = [
    { id: 'user-1', role: 'user', content: '2月去上海出差' },
    { id: 'assistant-orphan', role: 'assistant', content: '这是申请核对表，下方表格点击对应行即可直接编辑。' },
    { id: 'assistant-other', role: 'assistant', content: '普通回复' }
  ]

  assert.equal(isOrphanInlineApplicationPreviewMessage(messages[1]), true)
  assert.equal(resolveLatestApplicationPreviewMessage(messages), null)
  assert.equal(resolveLatestOrphanApplicationPreviewMessage(messages)?.id, 'assistant-orphan')

  messages.push({
    id: 'assistant-preview',
    role: 'assistant',
    content: '申请核对表',
    applicationPreview: { fields: { location: '上海' } }
  })
  assert.equal(resolveLatestApplicationPreviewMessage(messages)?.id, 'assistant-preview')
})
