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

import {
  compactDigitalEmployeeWorkRecords,
  extractWorkRecordToolSummary,
  isVisibleDigitalEmployeeWorkRecord,
  resolveWorkRecordModuleLabel,
  resolveWorkRecordProductKind,
  resolveWorkRecordTaskType,
  resolveWorkRecordTitle
} from '../src/views/scripts/digitalEmployeeWorkRecordsModel.js'

const runProductsComponent = readFileSync(
  fileURLToPath(new URL('../src/components/audit/DigitalEmployeeRunProducts.vue', import.meta.url)),
  'utf8'
)
const digitalEmployeeDetailComponent = readFileSync(
  fileURLToPath(new URL('../src/components/audit/AuditDigitalEmployeeDetail.vue', import.meta.url)),
  'utf8'
)

test('digital employee risk graph run resolves structured product metadata', () => {
  const run = {
    route_json: {
      task_code: 'task.hermes.global_risk_scan',
      task_name: '财务风险图谱巡检'
    },
    tool_calls: [
      {
        tool_name: 'digital_employee.financial_risk_graph.scan',
        request_json: { task_type: 'global_risk_scan' },
        response_json: {
          scanned_claim_count: 3,
          risk_observation_count: 2,
          graph_node_count: 7,
          graph_edge_count: 6
        }
      }
    ]
  }

  assert.equal(resolveWorkRecordTaskType(run), 'global_risk_scan')
  assert.equal(resolveWorkRecordProductKind(run), 'risk_graph')
  assert.equal(resolveWorkRecordModuleLabel(run), '财务风险图谱巡检')
  assert.equal(resolveWorkRecordTitle(run), '财务风险图谱巡检')
  assert.equal(extractWorkRecordToolSummary(run).risk_observation_count, 2)
})

test('digital employee profile run resolves from tool request when route is sparse', () => {
  const run = {
    route_json: { selected_agent: 'hermes' },
    tool_calls: [
      {
        tool_name: 'digital_employee.employee_behavior_profile.scan',
        request_json: { task_type: 'employee_behavior_profile_scan' },
        response_json: {
          target_employee_count: 4,
          snapshot_count: 16,
          high_attention_employee_count: 1
        }
      }
    ]
  }

  assert.equal(resolveWorkRecordTaskType(run), 'employee_behavior_profile_scan')
  assert.equal(resolveWorkRecordProductKind(run), 'employee_profile')
  assert.equal(resolveWorkRecordModuleLabel(run), '员工行为画像巡检')
  assert.equal(extractWorkRecordToolSummary(run).snapshot_count, 16)
})

test('digital employee finance snapshot and reminder runs are visible core work records', () => {
  const financeRun = {
    run_id: 'finance-1',
    started_at: '2026-06-02T02:00:00+08:00',
    route_json: { task_type: 'finance_dashboard_snapshot' },
    tool_calls: [{ tool_name: 'digital_employee.finance_dashboard.snapshot' }]
  }
  const reminderRun = {
    run_id: 'reminder-1',
    started_at: '2026-06-02T02:05:00+08:00',
    route_json: { task_type: 'digital_employee_reminder_scan' },
    tool_calls: [{ tool_name: 'digital_employee.reminder.scan' }]
  }

  assert.equal(resolveWorkRecordModuleLabel(financeRun), '财务经营快照沉淀')
  assert.equal(resolveWorkRecordProductKind(financeRun), 'finance_snapshot')
  assert.equal(isVisibleDigitalEmployeeWorkRecord(financeRun), true)
  assert.equal(resolveWorkRecordModuleLabel(reminderRun), '定时提醒与待办扫描')
  assert.equal(resolveWorkRecordProductKind(reminderRun), 'reminder_scan')
  assert.equal(isVisibleDigitalEmployeeWorkRecord(reminderRun), true)
})

test('digital employee work records hide support tasks and compact daily finance snapshots', () => {
  const rows = compactDigitalEmployeeWorkRecords([
    {
      run_id: 'finance-new',
      started_at: '2026-06-02T03:00:00+08:00',
      route_json: { task_type: 'finance_dashboard_snapshot' }
    },
    {
      run_id: 'finance-old',
      started_at: '2026-06-02T02:00:00+08:00',
      route_json: { task_type: 'finance_dashboard_snapshot' }
    },
    {
      run_id: 'profile-1',
      started_at: '2026-06-02T08:30:00+08:00',
      route_json: { task_type: 'employee_behavior_profile_scan' }
    },
    {
      run_id: 'support-1',
      started_at: '2026-06-02T10:00:00+08:00',
      route_json: { task_type: 'risk_clue_collect' }
    }
  ])

  assert.deepEqual(rows.map((run) => run.run_id), ['finance-new', 'profile-1'])
})

test('digital employee risk clue run resolves review packet metadata', () => {
  const run = {
    route_json: {
      task_code: 'task.hermes.risk_rule_discovery',
      task_name: '风险线索归集'
    },
    tool_calls: [
      {
        tool_name: 'digital_employee.risk_clue.collect',
        request_json: { task_type: 'risk_clue_collect' },
        response_json: {
          fact_count: 5,
          rule_hit_count: 3,
          risk_clue_count: 2,
          risk_clues: [{ clue_id: 'risk_clue:1', title: '待复核线索' }],
          feedback_summary: {
            total: 1,
            recent: [{ feedback_id: 'fb-1', feedback_type: 'comment', observation_key: 'risk:c1' }]
          }
        }
      }
    ]
  }

  assert.equal(resolveWorkRecordTaskType(run), 'risk_clue_collect')
  assert.equal(resolveWorkRecordProductKind(run), 'risk_clue')
  assert.equal(resolveWorkRecordModuleLabel(run), '风险线索归集')
  assert.equal(extractWorkRecordToolSummary(run).risk_clue_count, 2)
})

test('digital employee work record product supports scoped observation expansion', () => {
  assert.match(runProductsComponent, /activeObservationKey/)
  assert.match(runProductsComponent, /toggleObservation/)
  assert.match(runProductsComponent, /异常关系/)
  assert.match(runProductsComponent, /待复核线索/)
  assert.match(runProductsComponent, /反馈样本/)
  assert.match(runProductsComponent, /formatFeedbackStatus/)
  assert.doesNotMatch(runProductsComponent, /KnowledgeIngestGraphView/)
})

test('digital employee skill detail does not render knowledge graph component', () => {
  assert.doesNotMatch(digitalEmployeeDetailComponent, /KnowledgeIngestGraphView/)
  assert.doesNotMatch(digitalEmployeeDetailComponent, /LightRAG 知识图谱/)
})
