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

import {
  buildLeaderApprovalEvents,
  buildLeaderApprovalInfo,
  resolveGeneratedDraftClaimNo
} from '../src/utils/applicationApproval.js'

test('buildLeaderApprovalInfo extracts leader opinion and generated reimbursement draft', () => {
  const info = buildLeaderApprovalInfo({
    profileManager: '王经理',
    riskFlags: [
      {
        source: 'manual_return',
        opinion: '需要补充预算口径',
        created_at: '2026-05-24T09:00:00'
      },
      {
        source: 'manual_approval',
        event_type: 'expense_application_approval',
        operator: 'li.manager@example.com',
        operator_name: '李经理',
        opinion: '业务必要，同意申请。',
        previous_approval_stage: '直属领导审批',
        next_approval_stage: '审批完成',
        generated_draft_claim_no: 'EXP-202605-0007',
        created_at: '2026-05-25T10:15:00'
      }
    ]
  })

  assert.deepEqual(info, {
    opinion: '业务必要，同意申请。',
    operator: '李经理',
    time: '2026-05-25 10:15',
    generatedDraftClaimNo: 'EXP-202605-0007'
  })
})

test('resolveGeneratedDraftClaimNo reads approval response payload', () => {
  assert.equal(
    resolveGeneratedDraftClaimNo({
      risk_flags_json: [
        {
          source: 'manual_approval',
          event_type: 'expense_application_approval',
          generated_draft_claim_no: 'EXP-202605-0012',
          created_at: '2026-05-25T11:00:00'
        }
      ]
    }),
    'EXP-202605-0012'
  )
})

test('buildLeaderApprovalEvents returns leader return and approval timeline in event order', () => {
  const events = buildLeaderApprovalEvents({
    profileManager: 'Fallback Manager',
    riskFlags: [
      {
        source: 'manual_approval',
        event_type: 'expense_application_approval',
        operator: 'Leader Li',
        opinion: 'Approved after supplement.',
        approval_event_id: 'approval-1',
        created_at: '2026-05-25T11:00:00'
      },
      {
        source: 'manual_return',
        event_type: 'expense_application_return',
        operator: 'manager@example.com',
        operator_name: 'Leader Li',
        reason: 'Need clearer budget explanation.',
        return_count: 1,
        return_event_id: 'return-1',
        created_at: '2026-05-25T09:00:00'
      }
    ]
  })

  assert.deepEqual(events.map((event) => event.id), ['return-1', 'approval-1'])
  assert.deepEqual(events.map((event) => event.type), ['returned', 'approved'])
  assert.deepEqual(events.map((event) => event.tone), ['danger', 'success'])
  assert.equal(events[0].operator, 'Leader Li')
  assert.equal(events[0].role, '直属领导审批节点')
  assert.equal(events[0].opinion, 'Need clearer budget explanation.')
  assert.equal(events[0].returnCount, 1)
  assert.equal(events[0].time, '2026-05-25 09:00')
  assert.equal(events[0].dateLabel, '2026-05-25')
  assert.equal(events[0].timeLabel, '09:00')
  assert.equal(Object.hasOwn(events[0], 'sortAt'), false)
})

test('buildLeaderApprovalEvents hides empty or unrelated return state', () => {
  assert.deepEqual(buildLeaderApprovalEvents({ riskFlags: [] }), [])
  assert.deepEqual(
    buildLeaderApprovalEvents({
      riskFlags: [
        {
          source: 'manual_return',
          event_type: 'expense_claim_return',
          return_stage_key: 'finance',
          reason: 'Finance return should not render as leader application opinion.',
          created_at: '2026-05-25T09:00:00'
        }
      ]
    }),
    []
  )
})
