/**
 * Feature Context Builder
 *
 * Builds the system prompt context for the interactive agent session.
 * Includes Shep identity, CLI reference, feature context, and behavioral guidelines.
 * The output string is suitable for use as systemPrompt content in SDK sessions.
 */
import { execFileSync } from 'node:child_process';
import { homedir } from 'node:os';
import { join } from 'node:path';
/** Maximum number of plan tasks to include in the context. */
const MAX_TASKS = 30;
/** Cached CLI help text — generated once per process lifetime. */
let cachedCliHelp = null;
/**
 * Generates the full recursive CLI help text by running `shep --help` and
 * subcommand help for all registered commands. Cached after first call.
 */
function getCliHelpText() {
    if (cachedCliHelp)
        return cachedCliHelp;
    try {
        const mainHelp = execFileSync('shep', ['--help'], {
            timeout: 5000,
            encoding: 'utf8',
            stdio: ['pipe', 'pipe', 'pipe'],
        }).trim();
        // Extract subcommand names from help output
        const cmdLines = mainHelp.split('\n').filter((l) => /^\s{2}\w/.test(l));
        const subCommands = cmdLines
            .map((l) => l.trim().split(/\s+/)[0])
            .filter((cmd) => !!cmd && cmd !== 'help');
        const parts = [mainHelp];
        for (const cmd of subCommands) {
            try {
                const subHelp = execFileSync('shep', [cmd, '--help'], {
                    timeout: 3000,
                    encoding: 'utf8',
                    stdio: ['pipe', 'pipe', 'pipe'],
                }).trim();
                if (subHelp) {
                    parts.push(`\n--- shep ${cmd} ---\n${subHelp}`);
                }
            }
            catch {
                // Some commands may not have --help or may fail
            }
        }
        cachedCliHelp = parts.join('\n');
    }
    catch {
        cachedCliHelp = '(CLI help unavailable — shep binary not found in PATH)';
    }
    return cachedCliHelp;
}
/**
 * Builds concise feature context strings for agent bootstrap prompts.
 * Stateless — safe to reuse across sessions.
 */
export class FeatureContextBuilder {
    /**
     * Build the full system prompt for the interactive agent.
     *
     * @param feature - The feature domain object
     * @param worktreePath - Absolute CWD of the agent process
     * @param openPRs - List of open PR URLs to include (may be empty)
     * @returns A formatted context string suitable for systemPrompt or boot prompt injection
     */
    buildContext(feature, worktreePath, openPRs) {
        const shepHome = process.env.SHEP_HOME ?? join(homedir(), '.shep');
        let version = 'unknown';
        try {
            version = execFileSync('shep', ['--version'], {
                timeout: 3000,
                encoding: 'utf8',
                stdio: ['pipe', 'pipe', 'pipe'],
            }).trim();
        }
        catch {
            // fallback
        }
        const sections = [];
        // ── Identity ──────────────────────────────────────────────────────────
        sections.push(`# Shep Interactive Agent
You are **Shep** — the interactive AI assistant for the Shep SDLC platform.
Shep is an Autonomous AI Native SDLC Platform that automates the development
cycle from idea to deploy.

Version: ${version}
SHEP_HOME: ${shepHome}
Platform: ${process.platform} (${process.arch})`);
        // ── Behavioral guidelines ─────────────────────────────────────────────
        sections.push(`## Behavior
- You are smart, adaptive, and match the user's communication style.
- Be concise by default. If the user writes short messages, respond short.
  If they write detailed messages, respond with more detail.
- Don't jump to action immediately. First understand what the user needs.
  Ask clarifying questions when the request is ambiguous.
- When you DO act, be thorough and explain what you did.
- You have full access to the worktree via your tools (git, gh, filesystem,
  bash, read, write, edit, grep, glob). Use them proactively.
- You know the Shep CLI inside out. Help users with any shep command.
- When working on this feature, stay focused on the feature context below.
- If the user asks about Shep itself, answer from your knowledge of the CLI.`);
        // ── CLI Reference ─────────────────────────────────────────────────────
        sections.push(`## Shep CLI Reference
\`\`\`
${getCliHelpText()}
\`\`\``);
        // ── Feature context ───────────────────────────────────────────────────
        sections.push(`## Current Feature
Name: ${feature.name}
Description: ${feature.description}
Phase: ${feature.lifecycle}
Branch: ${feature.branch}
Worktree: ${worktreePath}
${this.buildTasksSection(feature).join('\n')}
${this.buildPRsSection(openPRs).join('\n')}`);
        return sections.join('\n\n');
    }
    buildTasksSection(feature) {
        const tasks = feature.plan?.tasks;
        if (!tasks || tasks.length === 0) {
            return ['Tasks: none'];
        }
        const displayed = tasks.slice(0, MAX_TASKS);
        const truncated = tasks.length > MAX_TASKS ? ` (showing first ${MAX_TASKS})` : '';
        const header = `Tasks (${tasks.length}${truncated}):`;
        const taskLines = displayed.map((t) => {
            const title = t.title ?? t.id;
            return `  [${t.state}] ${title}`;
        });
        return [header, ...taskLines];
    }
    buildPRsSection(openPRs) {
        if (openPRs.length === 0)
            return [];
        return ['Open PRs:', ...openPRs.map((url) => `  - ${url}`)];
    }
}
