from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING, List

if TYPE_CHECKING:
    from core.pydantic_ai_agent import AgentDeps, ConversationState, CustomerMessage, CustomerServiceAgent


@dataclass
class PromptBundle:
    user_prompt: str
    deps: "AgentDeps"
    history: List


def build_prompt_bundle(
    agent: "CustomerServiceAgent",
    *,
    message: "CustomerMessage",
    state: "ConversationState",
) -> PromptBundle:
    from core.pydantic_ai_agent import AgentDeps

    user_prompt = agent._build_prompt(message, state)

    profile_context = agent._get_customer_profile_context(message.from_id)
    if profile_context:
        user_prompt = profile_context + "\n\n" + user_prompt

    refusal_hint = agent._get_refusal_context_hint(message.from_id, message.msg, profile_context or "")
    if refusal_hint:
        user_prompt = refusal_hint + "\n\n" + user_prompt

    conv_context = agent._get_conversation_context(message.from_id, acc_id=message.acc_id or "")
    if conv_context:
        user_prompt = conv_context + user_prompt

    intent_hint = agent._get_intent_emotion_hint(message.msg)
    if intent_hint:
        user_prompt = intent_hint + "\n\n" + user_prompt

    deps = AgentDeps(
        msg_id=message.msg_id,
        acc_id=message.acc_id,
        from_id=message.from_id,
        platform=message.acc_type,
    )
    history = agent.message_histories.get(message.from_id, [])
    return PromptBundle(user_prompt=user_prompt, deps=deps, history=history)
