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

import { useWorkbenchComposerDate } from '../src/composables/useWorkbenchComposerDate.js'
import {
  buildWorkbenchDateLabel,
  canApplyWorkbenchDateSelection,
  getTodayDateValue,
  mergeWorkbenchDateLabelIntoDraft,
  stripWorkbenchDateLabelFromDraft
} from '../src/utils/workbenchComposerDate.js'

const workbench = readFileSync(
  fileURLToPath(new URL('../src/components/business/PersonalWorkbench.vue', import.meta.url)),
  'utf8'
)
const workbenchDateComposable = readFileSync(
  fileURLToPath(new URL('../src/composables/useWorkbenchComposerDate.js', import.meta.url)),
  'utf8'
)
const workbenchDateStyles = readFileSync(
  fileURLToPath(new URL('../src/assets/styles/components/personal-workbench-composer-date.css', import.meta.url)),
  'utf8'
)

test('traditional workbench no longer renders the old composer date picker', () => {
  assert.doesNotMatch(workbench, /aria-label="上传附件"[\s\S]*class="workbench-date-anchor"/)
  assert.doesNotMatch(workbench, /aria-label="选择日期"/)
  assert.doesNotMatch(workbench, /class="workbench-date-chip"/)
  assert.doesNotMatch(workbench, /removeWorkbenchDateTag/)
  assert.doesNotMatch(workbench, /composer-date-popover/)
  assert.doesNotMatch(workbench, /setWorkbenchDateMode\('single'\)/)
  assert.doesNotMatch(workbench, /useWorkbenchComposerDate/)
  assert.match(workbenchDateComposable, /const workbenchSingleDate = ref\(getTodayDateValue\(\)\)/)
  assert.match(workbenchDateComposable, /const workbenchDateTagLabel = ref\(''\)/)
  assert.match(workbenchDateComposable, /const today = getTodayDateValue\(\)[\s\S]*workbenchSingleDate\.value = today/)
  assert.match(workbenchDateComposable, /function handleWorkbenchDateInputChange/)
  assert.match(workbenchDateStyles, /\.workbench-date-anchor/)
  assert.match(workbenchDateStyles, /\.workbench-date-chip/)
  assert.match(workbenchDateStyles, /\.composer-date-popover/)
})

test('workbench date helper builds labels and inserts them into draft text', () => {
  assert.equal(getTodayDateValue(new Date(2026, 4, 9, 12)), '2026-05-09')
  assert.equal(
    buildWorkbenchDateLabel({
      mode: 'single',
      singleDate: '2026-05-29'
    }),
    '2026-05-29'
  )
  assert.equal(
    buildWorkbenchDateLabel({
      mode: 'range',
      rangeStartDate: '2026-05-29',
      rangeEndDate: '2026-05-31'
    }),
    '2026-05-29 至 2026-05-31'
  )
  assert.equal(
    mergeWorkbenchDateLabelIntoDraft('申请出差', '2026-05-29'),
    '2026-05-29，申请出差'
  )
  assert.equal(
    mergeWorkbenchDateLabelIntoDraft('发生时间：2026-05-28，申请出差', '2026-05-29'),
    '2026-05-29，申请出差'
  )
  assert.equal(
    mergeWorkbenchDateLabelIntoDraft('2026-05-28，申请出差', '2026-05-29'),
    '2026-05-29，申请出差'
  )
  assert.equal(
    stripWorkbenchDateLabelFromDraft('2026-05-28 至 2026-05-30，申请出差'),
    '申请出差'
  )
  assert.equal(canApplyWorkbenchDateSelection({ mode: 'range', rangeStartDate: '2026-06-01', rangeEndDate: '2026-05-31' }), false)
})

test('workbench range end date changes keep the picker open until the user confirms', () => {
  const draft = ref('')
  let focusCount = 0
  const dateRuntime = useWorkbenchComposerDate({
    draft,
    focusInput: () => {
      focusCount += 1
    }
  })

  dateRuntime.workbenchDatePickerOpen.value = true
  dateRuntime.workbenchDateMode.value = 'range'
  dateRuntime.workbenchRangeStartDate.value = '2026-02-20'
  dateRuntime.workbenchRangeEndDate.value = '2026-03-23'

  dateRuntime.handleWorkbenchDateInputChange('range-end')

  assert.equal(dateRuntime.workbenchDatePickerOpen.value, true)
  assert.equal(dateRuntime.workbenchDateTagLabel.value, '')
  assert.equal(draft.value, '')
  assert.equal(focusCount, 0)

  dateRuntime.applyWorkbenchDateSelection()

  assert.equal(dateRuntime.workbenchDatePickerOpen.value, false)
  assert.equal(dateRuntime.workbenchDateTagLabel.value, '2026-02-20 至 2026-03-23')
  assert.equal(draft.value, '')
  assert.equal(dateRuntime.buildWorkbenchPromptText(), '2026-02-20 至 2026-03-23')
  assert.equal(focusCount, 1)
})
