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

const facilities = [
  {
    path: 'src/views/subSystem/basic/waterSource/index.vue',
    fields: ['longitude', 'latitude']
  },
  {
    path: 'src/views/subSystem/basic/waterPlant/index.vue',
    fields: ['longitude', 'latitude']
  },
  {
    path: 'src/views/subSystem/basic/pumpStation/index.vue',
    fields: ['longitude', 'latitude']
  },
  {
    path: 'src/views/subSystem/basic/pipeInfo/index.vue',
    fields: ['startLongitude', 'startLatitude', 'endLongitude', 'endLatitude']
  },
  {
    path: 'src/views/subSystem/basic/waterUserInfo/index.vue',
    fields: ['longitude', 'latitude']
  }
]

test('all T1 facility pages expose Excel import through the shared dialog', () => {
  for (const facility of facilities) {
    const source = fs.readFileSync(facility.path, 'utf8')
    assert.match(source, /WaterFacilityImportDialog/u, facility.path)
    assert.match(source, />导入<\/el-button>/u, facility.path)
    assert.match(source, /@imported="getList"/u, facility.path)
  }

  const dialog = fs.readFileSync('src/views/subSystem/waterSupply/components/WaterFacilityImportDialog.vue', 'utf8')
  assert.match(dialog, /accept="\.xlsx, \.xls"/u)
  assert.match(dialog, /:before-upload="validateFile"/u)
  assert.match(dialog, /function validateFile/u)
  assert.match(dialog, /\(xlsx\|xls\)/u)
  assert.match(dialog, /updateSupport/u)
  assert.match(dialog, /下载模板/u)
  assert.match(dialog, /VITE_APP_BASE_API/u)
  assert.match(dialog, /download\(props\.templateUrl/u)
  assert.match(dialog, /response\.code !== 200/u)
})

test('all T1 facility pages expose coordinates in list, edit drawer, and detail drawer', () => {
  for (const facility of facilities) {
    const source = fs.readFileSync(facility.path, 'utf8')
    for (const field of facility.fields) {
      const occurrences = source.match(new RegExp(field, 'gu')) || []
      assert.ok(occurrences.length >= 3, `${facility.path} must expose ${field} for list, form, and detail`)
      assert.match(source, new RegExp(`v-model="form\\.${field}"`, 'u'), `${facility.path} must edit ${field}`)
      assert.match(source, new RegExp(`detailData\\.${field}`, 'u'), `${facility.path} must show ${field} in details`)
    }
  }
})
