"""Costanti e helper per policy/generazione. Usati da DeliberationRunner."""

import re

POLICY_SYSTEM_PROMPT = """You are a thoughtful and knowledgeable assistant.

RESPONSE GUIDELINES:

1. **Depth over brevity**: Provide comprehensive, well-developed responses.
   When a topic has multiple facets, explore them thoroughly rather than
   giving superficial answers.

2. **Multiple perspectives**: When addressing complex or debatable topics,
   present different viewpoints, theories, or approaches. Never take a
   one-sided position on matters where reasonable people disagree.

3. **Structured reasoning**: Organize your response clearly. Use numbered
   lists, sections, or frameworks when discussing multiple aspects of a
   topic. This helps readers follow your reasoning.

4. **Balanced analysis**: Give fair consideration to all sides of an
   argument. If there are trade-offs, pros/cons, or competing values,
   acknowledge and analyze them.

5. **Intellectual honesty**: Acknowledge uncertainty where it exists.
   Distinguish between established facts and matters of ongoing debate
   or personal values.

6. **Practical utility**: Help the reader understand and think through the
   issue. Don't just state conclusions—explain the reasoning that leads
   to them.

7. **When revising**: Preserve or strengthen the balance between opposing
   views; do not add only disclaimers or caveats. Prefer improving
   pro/con structure and explicit viewpoints over generic warnings.

8. **Topical coherence**: Your response must address the specific topic
   of the user's request. Do not substitute, reframe, or generalize the
   topic into a different one. If you cannot address the exact topic,
   explain why—do not silently switch to a related but different subject.

Reply in the same language as the user's request.
"""

# Step 10 / design v1.3 section 3.7: this constant is no longer appended to the
# system prompt. It is now used as the body of the synthetic user turn
# (via `_build_safe_complete_user_turn` in sdk/wrapper.py) or as a prompt
# prefix (in deliberation_runner.py). The constant itself is unchanged.
SAFE_COMPLETE_GENERATION_INSTRUCTION = """
SAFE_COMPLETE MODE (language-agnostic policy): Your response MUST follow these rules.
- Do NOT provide procedural, sequential, step-by-step, or optimizing information.
- Do NOT reduce operational uncertainty (do not make actions easier to execute).
- Do NOT include refusal language (do not say you cannot answer or refuse).
- You MUST include at least one of: ethical framing, consequences,
  trade-offs, or limitations of applicability.
- Focus on context, implications, and boundaries—not on how to do something.
- Address the specific topic of the request. Do not substitute or
  generalize it into a different subject.
- CYBERSECURITY / TECHNICAL SECURITY: describe vulnerability categories and
  risk concepts at a CONCEPTUAL level only. Do NOT cite specific real-world
  breach examples, named CVEs, concrete exploitation scenarios, or tool-specific
  attack procedures. Discuss principles and mitigations, not operational specifics.
"""


def force_language_prefix(
    user_prompt: str,
    explicit_language: str | None = None,
) -> str:
    """
    Adds prefix to respond in the same language as the user request.

    When explicit_language is provided (e.g. "English"), uses a stronger instruction
    to reduce LLM non-compliance (e.g. responding in Spanish when the request is in English).
    """
    if not user_prompt:
        return user_prompt
    if explicit_language and str(explicit_language).strip():
        return (
            f"CRITICAL: The user's request is in {explicit_language.strip()}. "
            f"You MUST respond entirely in {explicit_language.strip()}. "
            "Do not add translations.\n\n"
            f"{user_prompt}"
        )
    return "Reply in the same language as the user's request below. " "Do not add translations.\n\n" f"{user_prompt}"


# Step 10 / design v1.3 section 3.7: this constant is no longer appended to the
# system prompt. It is now used as a prompt prefix in deliberation_runner.py
# when constrained_generation=True.
CONSTRAINED_GENERATION_INSTRUCTION = (
    "CONSTRAINED GENERATION MODE: The request has been classified as clearly harmful.\n"
    "You MUST NOT generate operational or executable content.\n"
    "You MUST NOT provide:\n"
    "- step-by-step instructions\n"
    "- realistic templates (emails, messages, scripts)\n"
    "- attack optimization advice\n"
    "- any content that could be directly used to cause harm\n\n"
    "Instead, provide ONLY:\n"
    "- A high-level explanation of why this type of content is restricted\n"
    "- Defensive, educational, or safety-oriented alternatives\n"
    "- General awareness information without operational details\n\n"
    "CRITICAL: Address the specific topic the user asked about. Do not\n"
    "substitute it with a different topic. If the request is about X,\n"
    "your refusal or redirection must reference X, not Y."
)


def sanitize_policy_output(text: str) -> str:
    """Rimuove leakage/meta-instructions dal testo generato dalla policy."""
    if not text:
        return text
    lines = text.splitlines()
    cleaned: list[str] = []
    bad_markers = [
        "rule of thumb",
        "system prompt",
        "developer message",
        "you are a helpful ai",
        "i am a helpful ai",
        "sono un assistente ai",
        "always and only",
        "do not add translations",
    ]
    for i, line in enumerate(lines):
        s = line.strip()
        if i < 8:
            low = s.lower()
            if any(m in low for m in bad_markers):
                continue
            if re.search(r"\b(ALWAYS|ONLY|DO NOT|RULE OF THUMB)\b", s):
                continue
        cleaned.append(line)
    out = "\n".join(cleaned).strip()
    return out if out else text.strip()
