import assert from 'node:assert/strict'

import {
  buildKnowledgeIngestLogModel,
  isKnowledgeIngestRun
} from '../src/utils/knowledgeIngestLogModel.js'

function buildRun() {
  return {
    status: 'running',
    route_json: {
      job_type: 'knowledge_index_sync',
      folder: '制度文件',
      phase: 'indexing',
      progress: {
        total_documents: 2,
        completed_documents: 1,
        failed_documents: 0,
        percent: 55
      },
      knowledge_ingest: {
        status: 'running',
        phase: 'indexing',
        current_document_id: 'doc-2',
        graph: {
          chunk_count: 5,
          entity_count: 3,
          relation_count: 2,
          entities: [
            {
              name: '远光软件',
              type: 'ORGANIZATION',
              description: '远光软件是支出管理制度的公司主体。',
              descriptions: ['远光软件是支出管理制度的公司主体。'],
              properties: { created_at: '2026-05-23' }
            },
            '支出管理'
          ],
          relations: [
            {
              source: '远光软件',
              target: '支出管理',
              type: '约束',
              description: '通过制度约束支出审批。',
              keywords: ['制度', '审批'],
              weight: 2.5
            }
          ]
        },
        documents: [
          {
            document_id: 'doc-1',
            name: '公司支出管理办法.pdf',
            folder: '制度文件',
            extension: 'pdf',
            status: 'succeeded',
            phase: 'indexed',
            chunk_count: 3,
            entity_count: 2,
            relation_count: 1,
            chunks: [{ id: 'chunk-1', order: 0, tokens: 21, summary: '支出管理范围', excerpt: '支出管理范围正文' }],
            entity_chunks: [{ entity: '支出管理', chunk_ids: ['chunk-1'] }],
            sections: [{ title: '第一章 总则', excerpt: '适用于公司支出。' }],
            events: [{ at: '2026-05-22T08:00:00Z', level: 'info', message: '完成' }]
          },
          {
            document_id: 'doc-2',
            name: '费用审批台账.xlsx',
            folder: '制度文件',
            extension: 'xlsx',
            status: 'running',
            phase: 'indexing',
            chunk_count: 2,
            entity_count: 1,
            relation_count: 1
          }
        ]
      }
    }
  }
}

function testDetectsKnowledgeIngestRun() {
  assert.equal(isKnowledgeIngestRun(buildRun()), true)
  assert.equal(isKnowledgeIngestRun({ route_json: { job_type: 'daily_check' } }), false)
}

function testBuildsInteractiveModel() {
  const model = buildKnowledgeIngestLogModel(buildRun())

  assert.equal(model.available, true)
  assert.equal(model.folder, '制度文件')
  assert.equal(model.selectedDocumentId, 'doc-2')
  assert.equal(model.documents.length, 2)
  assert.equal(model.documents[0].statusLabel, '已完成')
  assert.equal(model.documents[0].chunks[0].summary, '支出管理范围')
  assert.equal(model.documents[0].chunks[0].excerpt, '支出管理范围正文')
  assert.deepEqual(model.documents[0].entityChunks, [{ entity: '支出管理', chunkIds: ['chunk-1'] }])
  assert.deepEqual(model.documents[1].chunks, [])
  assert.deepEqual(model.documents[1].sections, [])
  assert.deepEqual(model.documents[1].events, [])
  assert.equal(model.graph.entityCount, 3)
  assert.equal(model.graph.entities[0].name, '远光软件')
  assert.equal(model.graph.entities[0].type, 'ORGANIZATION')
  assert.equal(model.graph.entities[0].descriptions[0], '远光软件是支出管理制度的公司主体。')
  assert.equal(model.graph.relations[0].source, '远光软件')
  assert.equal(model.graph.relations[0].description, '通过制度约束支出审批。')
  assert.deepEqual(model.graph.relations[0].keywords, ['制度', '审批'])
  assert.equal(model.metrics[1].value, '5')
}

function testFallsBackToToolCallDocuments() {
  const model = buildKnowledgeIngestLogModel({
    status: 'succeeded',
    route_json: {
      job_type: 'knowledge_index_sync',
      requested_document_ids: ['doc-1']
    },
    tool_calls: [
      {
        response_json: {
          documents: [
            {
              document_id: 'doc-1',
              name: '归集结果.docx',
              status: 'succeeded',
              chunk_count: 1
            }
          ]
        }
      }
    ]
  })

  assert.equal(model.documents[0].name, '归集结果.docx')
  assert.equal(model.graph.chunkCount, 1)
}

function run() {
  testDetectsKnowledgeIngestRun()
  testBuildsInteractiveModel()
  testFallsBackToToolCallDocuments()
  console.log('knowledge ingest log model tests passed')
}

run()
