/**
 * Astra Mode Architecture Context Builder.
 *
 * 의도: 사용자가 *Astra 자체의 mode 디자인* (Guard vs Multi-Agent 가 별도 모드여야
 * 하는지) 을 묻는 메타 질문에 답할 때, 모델이 일반론적 답이 아니라 *현재 코드베이스
 * 의 실제 구조* 를 보고 답하게 하려고 시스템 프롬프트에 추가 컨텍스트 블록을 주입.
 *
 * 이 builder 는 *stateless* — instance state 의존 없고 외부 호출 없음. agent.ts 의
 * private 메서드로 박혀 있던 걸 별도 파일로 extract 해서 god file 무게 줄임 +
 * 향후 단위 테스트 용이성 확보.
 */

/**
 * 사용자 prompt 가 "Astra 의 Guard vs MA 모드 분리 결정" 류 메타 질문인지 판정.
 *
 * 3가지 신호가 *모두* 있어야 true — 너무 광범위하게 잡으면 평범한 질문에도
 * 잘못된 컨텍스트가 박혀서 모델이 헷갈림.
 */
export function isAstraModeArchitectureQuestion(prompt: string): boolean {
    if (!prompt) return false;
    const mentionsGuard = /\bguard\b|가드|Guard|Chronicle Guard|Project Chronicle/i.test(prompt);
    const mentionsMultiAgent = /\bMA\b|multi[-\s]?agent|멀티\s*에이전트|다중\s*에이전트|Planner|Researcher|Writer/i.test(prompt);
    const asksDecision = /(분리|통합|모드|사용|좋을까|맞을까|구조|설계|아키텍처|의견|판단|어때|어떤\s*거?\s*같|separate|combine|mode|architecture|design|opinion)/i.test(prompt);
    return asksDecision && mentionsGuard && mentionsMultiAgent;
}

/**
 * 메타 질문 감지 시 시스템 프롬프트에 prepend 할 컨텍스트 블록.
 * 감지 안 되면 빈 문자열 (호출자가 그대로 조립 가능).
 */
export function buildAstraModeArchitectureContext(prompt: string): string {
    if (!isAstraModeArchitectureQuestion(prompt)) {
        return '';
    }

    return [
        '[ASTRA MODE ARCHITECTURE DECISION CONTEXT]',
        'The user is asking about Astra itself, specifically whether Guard mode and MA/Multi-Agent mode should remain separate.',
        '',
        'Confirmed implementation facts from the current codebase:',
        '- Guard is currently exposed as a sidebar toggle, but it defaults to enabled in the webview UI.',
        '- Guard context is built by buildProjectChronicleGuardContext(activeProject) and passed into AgentExecutor as designerContext.',
        '- In the normal single-agent path, designerContext is injected into the system prompt as [PROJECT CHRONICLE GUARD].',
        '- In the Multi-Agent path, designerContext is appended as Project Chronicle Guard context for the workflow manager.',
        '- Multi-Agent is an internal execution strategy. The legacy g1nation.multiAgentEnabled setting can still force it for complex prompts, but Astra may also select it automatically for report/research/strategy style tasks.',
        '- Current guardrail: Multi-Agent is not used for local project path preflight or Astra mode-design questions, because those need richer context assembly first.',
        '',
        'Product decision guidance:',
        '- Do not treat Guard and MA as two equal user-facing modes.',
        '- Guard should be an always-on policy/context layer: project target, evidence discipline, record hygiene, tone, and decision logging.',
        '- MA should be an optional execution strategy chosen automatically for genuinely complex tasks.',
        '- Recommended UX: hide or de-emphasize the Guard toggle, show it as Auto/On by default, and let Astra route between single-agent and MA internally.',
        '- Recommended answer: give a clear verdict that separating them as peer modes is not ideal; separate them internally by responsibility instead.',
        '- Mention the concrete risk that MA can currently bypass richer context assembly, so unifying the context preparation before routing is the next engineering step.',
    ].join('\n');
}
