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

import {
  buildStewardPlanMessageText,
  buildStewardSuggestedActions
} from '../src/views/scripts/stewardPlanModel.js'

test('steward pending flow confirmation renders extracted fields as table', () => {
  const message = buildStewardPlanMessageText({
    plan_id: 'steward-plan-pending-flow',
    plan_status: 'needs_flow_confirmation',
    next_action: 'confirm_flow',
    pending_flow_confirmation: {
      status: 'pending',
      reason: '已先查询可关联申请单，当前仍需确认下一步。',
      candidate_flows: [
        {
          flow_id: 'travel_application',
          label: '先发起出差申请',
          confidence: 0.82,
          ontology_fields: {
            time_range: '2026-02-20',
            location: '上海',
            expense_type: 'travel',
            reason: '辅助国网仿生产服务器部署'
          },
          missing_fields: ['transport_mode']
        }
      ]
    }
  })

  assert.match(message, /\| 字段 \| 内容 \|/)
  assert.match(message, /\| 费用类型 \| 差旅 \|/)
  assert.match(message, /\| 发生时间 \| 2026-02-20 \|/)
  assert.match(message, /\| 地点 \| 上海 \|/)
  assert.match(message, /\| 事由 \| 辅助国网仿生产服务器部署 \|/)
  assert.doesNotMatch(message, /已提取到：\*\*/)
  assert.match(message, /请先点击下方/)
})

test('steward pending flow confirmation builds candidate actions', () => {
  const actions = buildStewardSuggestedActions({
    plan_id: 'steward-plan-pending-flow',
    plan_status: 'needs_flow_confirmation',
    next_action: 'confirm_flow',
    pending_flow_confirmation: {
      status: 'pending',
      reason: '缺少申请或报销动作词。',
      candidate_flows: [
        {
          flow_id: 'travel_application',
          label: '补办出差申请',
          confidence: 0.52,
          ontology_fields: {
            time_range: '2026-02-20',
            location: '上海',
            expense_type: 'travel',
            reason: '辅助国网仿生产环境部署'
          },
          missing_fields: ['transport_mode']
        },
        {
          flow_id: 'travel_reimbursement',
          label: '发起费用报销',
          confidence: 0.48,
          ontology_fields: {
            time_range: '2026-02-20',
            location: '上海',
            expense_type: 'travel',
            reason: '辅助国网仿生产环境部署'
          },
          missing_fields: []
        }
      ]
    }
  })

  assert.equal(actions.length, 2)
  assert.deepEqual(actions.map((item) => item.label), ['补办出差申请', '发起费用报销'])
  assert.equal(actions[0].payload.steward_confirm_flow, true)
  assert.equal(actions[0].payload.flow_id, 'travel_application')
  assert.equal(actions[1].payload.flow_id, 'travel_reimbursement')
})

test('steward ready application confirmation routes workbench action to inline preview table', () => {
  const actions = buildStewardSuggestedActions({
    plan_id: 'steward-plan-ready-application',
    plan_status: 'ready',
    tasks: [
      {
        task_id: 'task-application-beijing',
        task_type: 'expense_application',
        title: '费用申请 2026-06-23 北京',
        summary: '明天前往北京出差3天，支撑客户现场实施。',
        assigned_agent: 'application_assistant',
        ontology_fields: {
          expense_type: 'travel',
          time_range: '2026-06-23 至 2026-06-25',
          location: '北京',
          days: '3天',
          reason: '支撑客户现场实施'
        },
        missing_fields: ['transport_mode']
      }
    ],
    confirmation_groups: [
      {
        confirmation_id: 'confirm-application-beijing',
        action_type: 'confirm_create_application',
        target_task_id: 'task-application-beijing'
      }
    ]
  })

  assert.equal(actions.length, 1)
  assert.equal(actions[0].label, '确定，先创建申请单')
  assert.equal(actions[0].payload.steward_confirm_flow, true)
  assert.equal(actions[0].payload.flow_id, 'travel_application')
  assert.equal(actions[0].payload.expense_type, 'travel')
  assert.equal(actions[0].payload.expense_type_label, '差旅费')
  assert.match(actions[0].payload.carry_text, /支撑客户现场实施/)
  assert.match(actions[0].payload.carry_text, /北京/)
  assert.match(actions[0].payload.carry_text, /2026-06-23 至 2026-06-25/)
})
