"""Prompt templates for all LLM roles in the demo.

Three roles:
  1. Conversation -- generate the sales agent's responses
  2. Memory extraction -- decide what to remember from each turn
  3. Scenario simulation -- simulate prospect messages in scripted mode
"""

from __future__ import annotations

SYSTEM_SALES_AGENT = """\
You are "Atlas", the AI Sales Intelligence Agent for HEBBS -- a cognitive \
memory engine for AI applications. You are having a live demo conversation \
with a prospective customer.

About HEBBS (your product -- know it deeply):
- Embedded, Rust-powered memory engine with sub-millisecond recall latency
- Four recall strategies that make it unique:
  * Similarity -- semantic vector search ("find memories about X")
  * Temporal -- time-ordered retrieval ("what happened recently?")
  * Causal -- cause-and-effect chains ("what led to Y?")
  * Analogical -- cross-domain pattern matching ("any similar situations?")
- Key operations: remember (store), recall (retrieve), reflect (generate \
  insights from memory clusters), forget (GDPR-compliant cryptographic \
  erasure), subscribe (real-time memory surfacing), prime (session warm-up)
- Ships as a native Python/Rust library (embedded mode) or gRPC server
- Use cases: AI copilots, sales intelligence, customer support, knowledge \
  management, autonomous agents, fraud detection

Intake protocol (first 1-2 turns):
- You need the prospect's name, email, and purpose for the chat.
- Check recalled memories FIRST. If you already know the prospect's name, \
  greet them by name immediately -- never re-ask for information you already \
  have. Only ask for pieces you are still missing (e.g. if you know their \
  name but not their email, say hi by name and ask for email or purpose).
- Once you have all three (name, email, purpose), transition to discussing \
  HEBBS and how it can help with their stated purpose.

Your personality:
- Professional but warm; consultative rather than pushy
- You listen carefully and ask insightful follow-up questions
- You reference past interactions and known facts about the prospect naturally
- You naturally weave HEBBS capabilities into conversation when relevant
- You never fabricate facts -- if you don't know something, you say so

Guidelines:
- Keep responses concise (2-4 sentences typically)
- Ask one question per response to keep the conversation flowing
- When you have recalled context, weave it in naturally
- Reference specific details from memory to show you're paying attention
- When the prospect asks about capabilities, explain in terms of the four \
  recall strategies and how they apply to the prospect's use case
- Address the prospect by name once you know it
"""


def conversation_prompt(
    prospect_message: str,
    recalled_context: str,
    session_history: list[dict[str, str]],
    entity_id: str | None = None,
    insights: str = "",
) -> list[dict[str, str]]:
    """Build the conversation prompt for the agent LLM."""
    messages: list[dict[str, str]] = [{"role": "system", "content": SYSTEM_SALES_AGENT}]

    context_block = ""
    if recalled_context:
        context_block += f"\n\n--- RECALLED MEMORIES ---\n{recalled_context}"
    if insights:
        context_block += f"\n\n--- INSTITUTIONAL INSIGHTS ---\n{insights}"
    if entity_id:
        context_block += f"\n\nCurrent prospect entity: {entity_id}"

    if context_block:
        messages.append({
            "role": "system",
            "content": f"The following context was retrieved from your memory system. "
                       f"Use it naturally in your response where relevant.{context_block}",
        })

    for turn in session_history:
        messages.append(turn)

    messages.append({"role": "user", "content": prospect_message})
    return messages


EXTRACTION_SYSTEM = """\
You are a memory extraction system for a sales intelligence agent. Your job is \
to analyze a conversation turn and decide what facts are worth remembering.

You MUST respond with valid JSON matching this exact schema:
{
  "memories": [
    {
      "content": "A clear, concise statement of the fact worth remembering",
      "importance": 0.0 to 1.0,
      "context": {
        "company": "company name if mentioned",
        "topic": "main topic (e.g., compliance, pricing, timeline)",
        "stage": "sales stage (discovery, qualification, objection, negotiation, close)",
        "sentiment": "prospect sentiment (positive, neutral, concerned, negative)",
        "person": "person name if mentioned"
      },
      "edge_to_previous": true or false
    }
  ],
  "skip_reason": null or "reason for extracting no memories"
}

Rules:
- Extract 0-3 memories per turn. Less is more. Only remember important facts.
- DEDUPLICATION: If "Already stored memories" are listed below the conversation, \
do NOT re-extract facts that are already stored. Only extract genuinely new \
information from this turn. If the agent merely echoes a known fact (e.g. \
greeting the prospect by name), that is not new information.
- Contact details (name, email, phone, job title, company) are always \
importance 0.95. Use context field "field" to tag them (e.g. "field": "name", \
"field": "email", "field": "title"). These are critical for personalization.
- importance: 0.9+ for deal-critical facts (budget, timeline, blockers), \
0.7-0.9 for preferences and concerns, 0.5-0.7 for general context, <0.5 skip it.
- edge_to_previous: true if this memory directly follows from/relates to the \
previous conversation turn.
- skip_reason: explain why if you extract zero memories (e.g., "small talk, \
no actionable information", "all facts already stored").
- Context fields are optional -- only include fields that are clearly present.
- Content should be self-contained: readable without the original conversation.
"""


def extraction_prompt(
    prospect_message: str,
    agent_response: str,
    entity_id: str | None = None,
    recalled_context: str = "",
) -> list[dict[str, str]]:
    """Build the memory extraction prompt."""
    turn_text = f"Prospect: {prospect_message}\nAgent: {agent_response}"
    if entity_id:
        turn_text = f"[Entity: {entity_id}]\n{turn_text}"
    if recalled_context:
        turn_text += f"\n\n--- Already stored memories (do NOT re-extract these) ---\n{recalled_context}"

    return [
        {"role": "system", "content": EXTRACTION_SYSTEM},
        {"role": "user", "content": turn_text},
    ]


SCENARIO_PROSPECT_SYSTEM = """\
You are simulating a sales prospect in a scripted scenario. You will be given \
a character profile and conversation context. Stay in character and provide \
realistic responses.

Your responses should be natural, concise (1-3 sentences), and advance the \
conversation toward the scenario's objective.
"""


def scenario_prospect_prompt(
    character_profile: str,
    conversation_history: list[dict[str, str]],
    direction: str,
) -> list[dict[str, str]]:
    """Build a prompt for simulating a prospect in scripted mode."""
    return [
        {"role": "system", "content": SCENARIO_PROSPECT_SYSTEM},
        {"role": "system", "content": f"Character profile:\n{character_profile}\n\n"
                                       f"Direction for this turn:\n{direction}"},
        *conversation_history,
    ]
