"""
System prompt builder for Konstruct AI employees.

Assembles a coherent system prompt from wizard inputs (name, role, persona,
tools, escalation rules) and always appends the mandatory AI transparency
clause. The transparency clause is non-negotiable and cannot be omitted.
"""

from __future__ import annotations

# Non-negotiable AI transparency clause (Phase 1 architectural decision).
# Agents must always disclose their AI nature when directly asked.
AI_TRANSPARENCY_CLAUSE = (
    "When directly asked if you are an AI, always disclose that you are an AI assistant."
)

# Language detection instruction (Phase 7 multilanguage feature).
# Instructs agents to respond in the language the user writes in.
# Supports English, Spanish, and Portuguese.
LANGUAGE_INSTRUCTION = (
    "Detect the language of each user message and respond in that same language. "
    "You support English, Spanish, and Portuguese."
)


def build_system_prompt(
    name: str,
    role: str,
    persona: str = "",
    tool_assignments: list[str] | None = None,
    escalation_rules: list[dict[str, str]] | None = None,
) -> str:
    """
    Build a system prompt for an AI employee from wizard inputs.

    Args:
        name: The agent's display name (e.g. "Mara").
        role: The agent's role title (e.g. "Customer Support Rep").
        persona: Optional paragraph describing the agent's communication style
                 and personality traits.
        tool_assignments: Optional list of tool names available to the agent.
                          Omitted from the prompt when empty or None.
        escalation_rules: Optional list of escalation rule dicts, each with
                          "condition" and "action" keys. Omitted when empty/None.

    Returns:
        A complete system prompt string, always ending with the AI transparency
        clause.
    """
    sections: list[str] = []

    # --- Identity header ---
    identity = f"You are {name}, {role}."
    if persona:
        identity = f"{identity}\n\n{persona}"
    sections.append(identity)

    # --- Tools section (omitted when empty) ---
    effective_tools = tool_assignments if tool_assignments else []
    if effective_tools:
        tool_lines = "\n".join(f"- {tool}" for tool in effective_tools)
        sections.append(f"You have access to the following tools:\n{tool_lines}")

    # --- Escalation rules section (omitted when empty) ---
    effective_rules = escalation_rules if escalation_rules else []
    if effective_rules:
        rule_lines = "\n".join(
            f"- If {rule.get('condition', '')}: {rule.get('action', '')}"
            for rule in effective_rules
        )
        sections.append(f"Escalation rules:\n{rule_lines}")

    # --- Language instruction (always present — Phase 7 multilanguage) ---
    sections.append(LANGUAGE_INSTRUCTION)

    # --- AI transparency clause (always present, non-negotiable) ---
    sections.append(AI_TRANSPARENCY_CLAUSE)

    return "\n\n".join(sections)
