from django.utils import timezone

from core.models import Manipulation, Person
from core.util import logs

log = logs.get_logger("replies")


def should_reply(
    reply_to_self,
    reply_to_others,
    is_outgoing_message,
):
    reply = False
    if reply_to_self:
        reply = True
    elif reply_to_others:
        reply = True
    elif is_outgoing_message:
        reply = False
    else:
        reply = False

    return reply


def generate_mutate_reply_prompt(
    msg: dict,
    person: Person,
    manip: Manipulation,
    chat_history: str,
    mutate: bool = False,
):
    """
    Strictly rewrites the message in the persona’s tone and style
    while keeping the original meaning. No added explanations.
    """

    persona = manip.persona

    # 🔹 **Strict Rules to Prevent AI from Adding Commentary**
    strict_rules = (
        "- **DO NOT add explanations, comments, or meta-thoughts**.\n"
        "- **DO NOT return multiple responses—return ONLY the rewritten message**.\n"
        "- **DO NOT change the meaning, intent, or facts in the message**.\n"
        "- **DO NOT soften insults unless the persona naturally would**.\n"
        "- **DO NOT reframe as a question or suggestion—this is NOT a conversation**.\n"
        "- **Rewrite as if the original sender wrote it this way, without extra commentary**.\n"
        "- **Start immediately with the rewritten message—NO preface, intro, or context.**\n"
    )

    # 🔹 **What the AI SHOULD do**
    transformation_guidelines = (
        "- **Rewrite the message in the persona’s unique tone and style**.\n"
        "- **If the message is rude or harsh, reword it to match the persona’s confidence, cleverness, or wit**.\n"
        "- **If the persona is sarcastic, teasing, or flirty, maintain that energy**.\n"
        "- **Ensure the message feels natural, as if originally written that way**.\n"
        "- **Preserve original sentence structure as much as possible, adjusting only for flow.**\n"
    )

    system_message = (
        "You are a text rewriter. Your task is to transform messages into a given persona’s unique style, "
        "while keeping the original meaning intact.\n\n"
        "### Persona Profile ###\n"
        f"- **Tone:** {persona.tone} | **Humor:** {persona.humor_style}\n"
        f"- **Core Values:** {persona.core_values}\n"
        f"- **Communication Style:** {persona.communication_style}\n"
        f"- **Flirting Style:** {persona.flirting_style}\n"
        f"- **Likes:** {persona.likes} | **Dislikes:** {persona.dislikes}\n"
        f"- **Response Tactics:** {persona.response_tactics}\n"
        f"- **Persuasion Techniques:** {persona.persuasion_tactics}\n"
        f"- **Boundaries:** {persona.boundaries} | **Adaptability:** {persona.adaptability}%\n\n"
        "### STRICT RULES ###\n"
        f"{strict_rules}\n\n"
        "### TRANSFORMATION GUIDELINES ###\n"
        f"{transformation_guidelines}\n\n"
        "### Original Message ###\n"
        f"{msg}\n\n"
        "### Rewritten Message ###\n"
        "(DO NOT include anything except the rewritten text. NO extra comments or formatting.)"
    )

    return [{"role": "system", "content": system_message}]


def generate_reply_prompt(
    msg: dict,
    person: Person,
    manip: Manipulation,
    chat_history: str,
    mutate: bool = False,
):
    """
    Generate a structured prompt using the attributes of the provided Person and Manipulation models.
    """

    now = timezone.now()
    persona = manip.persona

    # 🔹 Define system instructions for filtering messages
    filter_rules = (
        "- **Strict Filtering**: If the message includes topics that are in dislikes or boundaries, "
        "modify or reject them completely to match the persona's values.\n"
        "- **Rephrase Sensitively**: If the message contains something the persona dislikes but is not "
        "a strict boundary, soften it or remove the negative elements.\n"
        "- **Enforce Persona Style**: Modify the tone, humor, and engagement level based on the persona's "
        "communication style.\n"
        "- **Never Ask for Clarifications**: Always modify the message directly without requesting clarification.\n"
        "- **Return Only the Modified Message**: Do not add any explanations or metadata.\n"
    )

    system_message = (
        "You are my digital persona, responding on my behalf while embodying my personality, preferences, and unique style.\n\n"
        "You must strictly apply the following persona-based filtering rules when modifying the message:\n\n"
        f"{filter_rules}\n\n"
        "### Persona Profile ###\n"
        f"- **MBTI:** {persona.mbti} ({persona.mbti_identity} balance)\n"
        f"- **Tone:** {persona.tone} | **Humor:** {persona.humor_style}\n"
        f"- **Core Values:** {persona.core_values}\n"
        f"- **Communication Style:** {persona.communication_style}\n"
        f"- **Flirting Style:** {persona.flirting_style}\n"
        f"- **Likes:** {persona.likes} | **Dislikes:** {persona.dislikes}\n"
        f"- **Response Tactics:** {persona.response_tactics}\n"
        f"- **Persuasion Techniques:** {persona.persuasion_tactics}\n"
        f"- **Boundaries:** {persona.boundaries} | **Adaptability:** {persona.adaptability}%\n\n"
        "### Contact Information ###\n"
        f"- **Summary:** {person.summary or 'N/A'}\n"
        f"- **Profile:** {person.profile or 'N/A'}\n"
        f"- **Revealed Details:** {person.revealed or 'N/A'}\n"
        f"- **Sentiment Score:** {person.sentiment}\n"
        f"- **Timezone:** {person.timezone or 'N/A'}\n"
        f"- **Last Interaction:** {person.last_interaction or 'Never'}\n"
        f"- **Current Date/Time:** {now}\n\n"
        "### Conversation Context ###\n"
        f"{chat_history if chat_history else 'No prior chat history.'}\n\n"
    )

    if not mutate:
        user_message = f"[{msg['timestamp']}] <{person.name}> {msg['text']}"
        log.info(f"User message: {user_message}")

        return [
            {"role": "system", "content": system_message},
            {"role": "user", "content": user_message},
        ]
    else:
        user_message = f"Message to amend: {msg}"
        return [
            {"role": "system", "content": system_message},
            {"role": "user", "content": user_message},
        ]
