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

import { renderMarkdown } from '../src/utils/markdown.js'

const createViewTemplate = readFileSync(
  fileURLToPath(new URL('../src/views/TravelReimbursementCreateView.vue', import.meta.url)),
  'utf8'
)
const createViewScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/TravelReimbursementCreateView.js', import.meta.url)),
  'utf8'
)
const suggestedActionsScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/useTravelReimbursementSuggestedActions.js', import.meta.url)),
  'utf8'
)
const submitConfirmScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/useTravelReimbursementApplicationSubmitConfirm.js', import.meta.url)),
  'utf8'
)
const messageHandlersScript = readFileSync(
  fileURLToPath(new URL('../src/views/scripts/useTravelReimbursementCreateViewMessageHandlers.js', import.meta.url)),
  'utf8'
)
const messageItemStyles = readFileSync(
  fileURLToPath(new URL('../src/assets/styles/components/travel-reimbursement-message-item.css', import.meta.url)),
  'utf8'
)

test('expense application submit uses rich text link and confirm dialog', () => {
  const copy = '请确认上述的信息是否填写正确？如果准确无误，点击 [确认](#application-submit) 进入审批环节。'
  const rendered = renderMarkdown(copy)

  assert.match(copy, /请确认上述的信息是否填写正确/)
  assert.match(copy, /进入审批环节/)
  assert.match(
    rendered,
    /<a href="#application-submit" class="markdown-action-link markdown-action-link-confirm">确认<\/a>/
  )
  assert.match(messageItemStyles, /\.application-preview-footer \{[\s\S]*margin-top: 48px;/)
  assert.match(messageItemStyles, /\.message-answer-markdown :deep\(\.markdown-action-link\) \{[\s\S]*text-decoration: underline;/)
  assert.match(createViewTemplate, /:open="applicationSubmitConfirmDialog\.open"/)
  assert.match(createViewTemplate, /title="确认提交当前费用申请？"/)
  assert.match(createViewTemplate, /description="提交后申请将进入领导审核流程，请确认关键申请信息和预计费用已经核对无误。"/)
  assert.match(createViewTemplate, /@confirm="confirmApplicationSubmit"/)
  assert.match(createViewScript, /const APPLICATION_SUBMIT_HREF = '#application-submit'/)
  assert.match(
    messageHandlersScript,
    /href === APPLICATION_SUBMIT_HREF[\s\S]*openApplicationSubmitConfirm\(message\)/
  )
  assert.match(
    submitConfirmScript,
    /async function confirmApplicationSubmit\(options = \{\}\)[\s\S]*const applicationSubmitText[\s\S]*applicationPreview[\s\S]*runApplicationPreviewAction[\s\S]*AI_APPLICATION_ACTION_SUBMIT[\s\S]*submitComposer/
  )
  assert.match(
    submitConfirmScript,
    /applicationSubmitConfirmDialog\.value = \{[\s\S]*open: false,[\s\S]*message: null[\s\S]*\}[\s\S]*const payload = applicationPreview/
  )
  assert.match(
    submitConfirmScript,
    /emitDraftSaved\(\{[\s\S]*status: 'submitted'[\s\S]*documentType: 'application'/
  )
  assert.match(submitConfirmScript, /buildTravelPlanningNudgeMessage\(applicationPreview, draftPayload\)/)
  assert.match(submitConfirmScript, /buildTravelPlanningSuggestedActions\(applicationPreview, draftPayload\)/)
  assert.match(submitConfirmScript, /meta:\s*\['行程规划推荐'\]/)
  assert.match(suggestedActionsScript, /TRAVEL_PLANNING_ACTION_GENERATE/)
  assert.match(suggestedActionsScript, /buildTravelPlanningRecommendation\(sourcePreview, sourceDraftPayload\)/)
  assert.match(suggestedActionsScript, /TRAVEL_PLANNING_ACTION_SKIP/)
})
