"""Source-aware dynamic summary prompts for LLM-based context summarization.

Provides two source-specific prompt constants (USER_SUMMARY_PROMPT and
AGENT_SUMMARY_PROMPT) and a resolve_summary_prompt() function that returns
the appropriate prompt for a given source type.

The model sees ONLY instructions for the current source type -- it never
sees instructions for the other source type.  This is achieved by calling
resolve_summary_prompt(source) per-summarization-call (not at init time).

Prompt engineering design decisions:
1. /no_think prefix disables Qwen3 reasoning mode (saves tokens + time)
2. Zero-shot (no examples) - maximizes input token budget for small models
3. Output token limit controlled via SUMMARY_MAX_TOKENS at LLM API level
4. "Output ONLY the summary" prevents preamble/labels from small models
5. Focuses on entities/topics/decisions - what agents need for relevance judgment
6. "Do not add" negation constraints prevent common small-model failure modes
7. Single paragraph format - easiest for small models to follow
8. Temperature=0 set at model level for deterministic output (not in prompt)
9. "Always write the summary in English" ensures cross-language consistency

The content to summarize is passed separately as a user/human message
at runtime (not part of the prompt constants).  HumanMessage receives
the text AS-IS, without any prefix.
"""

from __future__ import annotations

# Shared base requirements (DRY: used in both user and agent prompts).
# Private constant -- not exported, used only within this module.
_SUMMARY_BASE_REQUIREMENTS: str = '''Requirements:
- Include key topics, named entities, decisions, conclusions, and action items
- Prioritize information that helps determine relevance without reading the full text
- Use specific terms from the original text (do not generalize or abstract away details)
- Always write the summary in English regardless of the input language
- Write exactly one paragraph with no line breaks
- Target approximately 100-250 words
- Do not add any labels, prefixes, headers, or explanations
- Do not start with "This text" or "The author" or similar meta-references
- Output ONLY the summary text, nothing else'''

USER_SUMMARY_PROMPT: str = (f'''
/no_think
You are an expert summarizer for a context storage system used by AI agents.
The following text is a message from a human user. Your task is to produce a single, dense paragraph that captures the essential meaning of the user's message.

Focus on what the user is requesting, asking about, or reporting. Capture intent, specific requirements, constraints, and any decisions or directives the user communicates.

{_SUMMARY_BASE_REQUIREMENTS}
''').strip()

AGENT_SUMMARY_PROMPT: str = (f'''
/no_think
You are an expert summarizer for a context storage system used by AI agents.
The following text is a work report generated by an AI agent. Your task is to produce a single, dense paragraph that captures the essential meaning of the agent's report.

Focus on key findings, decisions, deliverables, and technical specifics. Omit process metadata such as debate rounds, agent numbers, confidence scores, and workflow details.

{_SUMMARY_BASE_REQUIREMENTS}
''').strip()

# Alias for tests that reference the "default" prompt generically.
# NOT used at runtime -- resolve_summary_prompt() returns source-specific prompts.
DEFAULT_SUMMARY_PROMPT: str = AGENT_SUMMARY_PROMPT


def resolve_summary_prompt(source: str) -> str:
    """Resolve the summary prompt for the given source type.

    Called per-summarization-call (not at init). Returns a prompt containing
    instructions ONLY for the specified source type.

    If SUMMARY_PROMPT env var is set, returns that custom prompt as-is
    (ignoring source-specific logic).

    Args:
        source: The source type ('user' or 'agent').

    Returns:
        The resolved summary prompt text.
    """
    from app.settings import get_settings

    custom_prompt = get_settings().summary.prompt
    if custom_prompt is not None and custom_prompt.strip() != '':
        return custom_prompt
    if source == 'user':
        return USER_SUMMARY_PROMPT
    return AGENT_SUMMARY_PROMPT
