import { readFile } from 'node:fs/promises'
import { resolveInWorkDir } from '../pathGuard.js'
import { requireCodeWorkDir } from '../hooks.js'

export async function runRead(
  path: string,
  offset?: number,
  limit?: number,
): Promise<Record<string, unknown>> {
  const full = resolveInWorkDir(requireCodeWorkDir(), path)
  const raw = await readFile(full, 'utf8')
  const lines = raw.split('\n')
  const start = Math.max(0, (offset ?? 1) - 1)
  const end = limit ? start + limit : lines.length
  const slice = lines.slice(start, end)
  return {
    path,
    offset: start + 1,
    content: slice.join('\n'),
    totalLines: lines.length,
  }
}
