"""RAG-powered banking insurance customer service agent using the OpenAI Agents SDK."""

import uuid
from pathlib import Path
from typing import Any

from agents import Agent as SDKAgent
from agents import Runner, function_tool

from .retriever import FaissRetriever, build_rag

_AGENT_SERVER_DIR = Path(__file__).parent.parent
_knowledge_config = [{"type": "local", "source": "./data"}]

build_rag(str(_AGENT_SERVER_DIR), _knowledge_config)
_retriever = FaissRetriever.load(str(_AGENT_SERVER_DIR))


@function_tool
async def retrieve_context(query: str) -> str:
    """Search the knowledge base for information relevant to the customer's question.

    Args:
        query: A concise, standalone question derived from the conversation.

    Returns:
        Relevant document excerpts, separated by dividers, or a fallback message.
    """
    results = await _retriever.retrieve(query)
    if not results:
        return "No relevant information found in the knowledge base."

    parts: list[str] = []
    for r in results:
        header = r["title"] or r["source"] or "Document"
        parts.append(f"[{header}]\n{r['content']}")
    return "\n\n---\n\n".join(parts)


SYSTEM_INSTRUCTIONS = (
    "You are a customer service agent for a bank and insurance company. "
    "Before answering, always call the retrieve_context tool with a concise "
    "standalone version of the customer's question to look up relevant information. "
    "Keep your final response concise & short and direct to the point."
)


class Agent:
    """Stateful per-session wrapper around the shared SDK agent.

    Maintains the full conversation history for the session and passes it on
    every ``Runner.run`` call so the model has multi-turn context.
    """

    def __init__(
        self,
        context_id: str | None = None,
        history: list[dict[str, Any]] | None = None,
    ) -> None:
        self.sdk_agent = SDKAgent(
            name="BankInsuranceAgent",
            instructions=SYSTEM_INSTRUCTIONS,
            tools=[retrieve_context],
            model="gpt-5.1",
        )
        self.context_id = context_id or str(uuid.uuid4())
        self._history: list[dict[str, Any]] = list(history) if history else []

    async def invoke(self, question: str) -> str:
        """Process a user message and return the agent's response.

        Args:
            question: The user's latest message.

        Returns:
            The agent's text response.
        """
        self._history.append({"role": "user", "content": question})
        result = await Runner.run(self.sdk_agent, input=self._history)
        answer: str = result.final_output
        self._history.append({"role": "assistant", "content": answer})
        return answer
