/**
 * 将 server/docs/flowcharts 同步到 saiadmin-artd/public/docs/flowcharts
 * 构建前执行，保证部署包内含最新流程图 HTML/MMD
 */
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const artdRoot = path.resolve(__dirname, '..')
const repoRoot = path.resolve(artdRoot, '..')
const srcDir = path.join(repoRoot, 'server', 'docs', 'flowcharts')
const destDir = path.join(artdRoot, 'public', 'docs', 'flowcharts')

if (!fs.existsSync(srcDir)) {
  console.warn('[sync-dice-flowcharts] 源目录不存在，跳过:', srcDir)
  process.exit(0)
}

fs.mkdirSync(destDir, { recursive: true })
const names = fs.readdirSync(srcDir).filter((n) => /\.(html|mmd)$/i.test(n))
for (const name of names) {
  fs.copyFileSync(path.join(srcDir, name), path.join(destDir, name))
  console.log('[sync-dice-flowcharts] copied', name)
}
console.log('[sync-dice-flowcharts] done,', names.length, 'file(s)')
