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

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

test('steward plan summary uses warm guidance copy for application flow', () => {
  const message = buildStewardPlanMessageText({
    tasks: [
      {
        task_id: 'task-1',
        task_type: 'expense_application',
        title: '费用申请 差旅',
        assigned_agent: 'application_assistant',
        confirmation_required: true
      }
    ],
    next_action: 'confirm_create_application'
  })

  assert.match(message, /我先帮您把步骤理清楚/)
  assert.match(message, /我先看了一下，您这次主要是 \*\*1 个事项\*\*/)
  assert.match(message, /为了不让步骤混在一起/)
  assert.match(message, /这步交给申请助手——先把申请单草稿拉出来给您过目/)
  assert.match(message, /您看这个顺序是否合适/)
  assert.match(message, /需要补充的信息会在具体步骤里再温和提醒/)
  assert.doesNotMatch(message, /我会这样推进/)
  assert.doesNotMatch(message, /不会一次性把所有动作都执行掉/)
  assert.doesNotMatch(message, /交给申请助手生成申请单核对结果/)
})

test('steward plan summary guides bare reimbursement intent into scene selection', () => {
  const plan = {
    tasks: [
      {
        task_id: 'task-reim-1',
        task_type: 'reimbursement',
        title: '费用报销 1',
        assigned_agent: 'reimbursement_assistant',
        confirmation_required: true,
        ontology_fields: {
          expense_type: 'other',
          reason: '我要报销'
        },
        missing_fields: ['time_range', 'reason']
      }
    ],
    confirmation_groups: [
      {
        confirmation_id: 'confirm-task-reim-1',
        action_type: 'confirm_create_reimbursement_draft',
        target_task_id: 'task-reim-1'
      }
    ],
    next_action: 'confirm_task'
  }

  const message = buildStewardPlanMessageText(plan)

  assert.match(message, /我来带您发起报销/)
  assert.match(message, /您现在只说了要报销/)
  assert.match(message, /先选报销场景/)
  assert.match(message, /差旅费、交通费、住宿费/)
  assert.doesNotMatch(message, /步骤混在一起/)
  assert.doesNotMatch(message, /核对“费用报销 1”/)

  const [action] = buildStewardSuggestedActions(plan)
  assert.equal(action.label, '确定，选择报销场景')
  assert.match(action.description, /先进入报销助手选择具体费用类型/)
  assert.equal(action.payload.carry_text, '我要报销')
})

test('steward suggested action carries server executable application action step', () => {
  const plan = {
    plan_id: 'plan-application-submit',
    tasks: [
      {
        task_id: 'task-app-1',
        task_type: 'expense_application',
        title: '上海出差申请',
        assigned_agent: 'application_assistant',
        requested_action: 'submit',
        ontology_fields: {
          expense_type: 'travel',
          time_range: '2026-02-20 至 2026-02-23',
          location: '上海',
          reason: '辅助国网仿生产服务器部署',
          transport_mode: 'train'
        },
        missing_fields: [],
        action_steps: [
          { step_id: 'task-app-1:01', action_type: 'fill_application_fields', status: 'planned' },
          { step_id: 'task-app-1:02', action_type: 'build_application_preview', status: 'planned' },
          { step_id: 'task-app-1:03', action_type: 'validate_required_fields', status: 'planned' },
          { step_id: 'task-app-1:04', action_type: 'run_duplicate_precheck', status: 'planned' },
          {
            step_id: 'task-app-1:05',
            action_type: 'submit_application',
            status: 'pending_confirmation',
            requires_confirmation: true
          }
        ]
      }
    ],
    confirmation_groups: [
      {
        confirmation_id: 'confirm-task-app-1',
        action_type: 'confirm_create_application',
        target_task_id: 'task-app-1'
      }
    ],
    next_action: 'confirm_task'
  }

  const [action] = buildStewardSuggestedActions(plan)

  assert.equal(action.payload.steward_execute_action, true)
  assert.equal(action.payload.steward_action_type, 'submit_application')
  assert.equal(action.payload.steward_action_step.step_id, 'task-app-1:05')
  assert.equal(action.payload.steward_action_requires_confirmation, true)
  assert.equal(action.payload.steward_current_task.requested_action, 'submit')
  assert.equal(action.payload.steward_current_task.action_steps.at(-1).action_type, 'submit_application')
})
