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

import {
  buildAiDocumentDetailRequest,
  parseAiApplicationDetailHref,
  parseAiDocumentDetailHref
} from '../src/utils/aiDocumentDetailReference.js'

test('AI detail request keeps business application number out of claimId for legacy links', () => {
  const detailReference = parseAiApplicationDetailHref('#ai-open-application-detail:AP-202606200001-ABCDEFGH')
  const request = buildAiDocumentDetailRequest(detailReference)

  assert.deepEqual(detailReference, {
    reference: 'AP-202606200001-ABCDEFGH',
    documentType: 'application'
  })
  assert.equal(request.id, 'AP-202606200001-ABCDEFGH')
  assert.equal(request.claimId, '')
  assert.equal(request.claimNo, 'AP-202606200001-ABCDEFGH')
  assert.equal(request.documentNo, 'AP-202606200001-ABCDEFGH')
  assert.equal(request.documentTypeCode, 'application')
  assert.equal(request.detailLookupOnly, true)
  assert.equal(request.source, 'ai-conversation')
  assert.equal(request.returnTo, 'conversation')
})

test('AI detail request uses explicit claim_id as lookup identity', () => {
  const detailReference = parseAiDocumentDetailHref(
    '#ai-open-document-detail:claim_id%3Dapproval-1%26claim_no%3DAP-APPROVAL-001'
  )
  const request = buildAiDocumentDetailRequest(detailReference)

  assert.deepEqual(detailReference, {
    reference: 'AP-APPROVAL-001',
    claimId: 'approval-1',
    claimNo: 'AP-APPROVAL-001'
  })
  assert.equal(request.id, 'approval-1')
  assert.equal(request.claimId, 'approval-1')
  assert.equal(request.claimNo, 'AP-APPROVAL-001')
  assert.equal(request.documentNo, 'AP-APPROVAL-001')
  assert.equal(request.documentTypeCode, 'application')
  assert.equal(request.returnTo, 'conversation')
})

test('AI detail request treats non-number references as internal claim ids', () => {
  const request = buildAiDocumentDetailRequest({ reference: 'approval-internal-id' })

  assert.equal(request.id, 'approval-internal-id')
  assert.equal(request.claimId, 'approval-internal-id')
  assert.equal(request.claimNo, '')
  assert.equal(request.documentNo, 'approval-internal-id')
  assert.equal(request.returnTo, 'conversation')
})
