/**
 * Workspace layout section describing the agent's working directory.
 */

import { readdirSync } from 'node:fs';
import { join } from 'node:path';

export function buildWorkspaceLayout(agentCwd: string): string {
  let entries: string[];
  try {
    entries = readdirSync(agentCwd, { withFileTypes: true })
      .filter(d => d.isDirectory() && d.name !== '.cw')
      .map(d => d.name);
  } catch {
    return '';
  }

  if (entries.length === 0) {
    return `\n\n## Workspace Layout\n\nYour working directory is: ${agentCwd}`;
  }

  const lines = entries.map(
    name => `- \`${name}/\` — ${join(agentCwd, name)}`
  );

  return `\n\n## Workspace Layout

Your working directory is: ${agentCwd}
The following project directories contain the source code (git worktrees):

${lines.join('\n')}`;
}
