import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import { fileURLToPath } from 'node:url'

import { normalizeApprovalWorkbenchItem } from '../src/services/approvalWorkbench.js'

const approvalTemplate = readFileSync(
  fileURLToPath(new URL('../src/views/ApprovalCenterView.vue', import.meta.url)),
  'utf8'
)
const approvalScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/ApprovalCenterView.js', import.meta.url)),
  'utf8'
)

test('approval workbench normalizes score, evidence and advisory suggestion', () => {
  const item = normalizeApprovalWorkbenchItem({
    task_key: 'claim-1:manager',
    claim: { id: 'claim-1', claim_no: 'RE-001' },
    priority_score: 138,
    priority_tier: 'urgent',
    priority_reasons: [{ code: 'open_risk', label: '高风险待复核', weight: 30 }],
    risk_level: 'high',
    waiting_hours: 26.25,
    sla_overdue: true,
    evidence: {
      completeness: 0.5,
      present_count: 1,
      required_count: 2,
      missing_labels: ['发票'],
      historical_labels: ['历史已确认，仅供复核']
    },
    suggestion: {
      action: 'manual_review',
      label: '人工复核风险',
      reason: '存在开放风险',
      advisory_only: true
    }
  })

  assert.equal(item.priorityScore, 100)
  assert.equal(item.priorityReasons[0].label, '高风险待复核')
  assert.deepEqual(item.evidence.missingLabels, ['发票'])
  assert.deepEqual(item.evidence.historicalLabels, ['历史已确认，仅供复核'])
  assert.equal(item.suggestion.advisoryOnly, true)
})

test('approval center renders explainable priority, evidence and advisory columns', () => {
  assert.match(approvalTemplate, />优先级</)
  assert.match(approvalTemplate, />证据</)
  assert.match(approvalTemplate, />AI建议</)
  assert.match(approvalTemplate, /row\.priorityTitle/)
  assert.match(approvalTemplate, /row\.evidenceTitle/)
  assert.match(approvalTemplate, /row\.suggestionReason/)
  assert.match(approvalScript, /fetchApprovalWorkbenchItems\(\{ limit: 100 \}\)/)
  assert.match(approvalScript, /listPendingApprovalRequests/)
  assert.doesNotMatch(approvalScript, /autoApprove|batchApprove|批量通过/)
})

test('approval center keeps backend status separate from display status', () => {
  assert.match(approvalScript, /statusLabel: statusTone === 'urgent'/)
  assert.doesNotMatch(approvalScript, /status: statusTone === 'urgent'/)
  assert.match(approvalTemplate, /row\.statusLabel/)
  assert.doesNotMatch(approvalTemplate, /\{\{ row\.status \}\}/)
})
