import { access, stat } from 'node:fs/promises'
import { join } from 'node:path'
import { hasRootCodeMd } from '../memory/loadCodeMd.js'
import { requireCodeWorkDir } from '../hooks.js'

export async function runPrepare(): Promise<Record<string, unknown>> {
  const codeWorkDir = requireCodeWorkDir()
  await access(codeWorkDir)
  const st = await stat(codeWorkDir)
  if (!st.isDirectory()) {
    throw new Error(`codeWorkDir 不是目录: ${codeWorkDir}`)
  }
  let isGit = false
  try {
    await access(join(codeWorkDir, '.git'))
    isGit = true
  } catch {
    isGit = false
  }
  const hasCodeMd = await hasRootCodeMd(codeWorkDir)
  return { codeWorkDir, isGit, hasCodeMd }
}
