"""Stateless Agent executor."""

from __future__ import annotations

from typing import TYPE_CHECKING

from src.models import AgentConfig, ScratchpadEntry

if TYPE_CHECKING:
    from src.llm_client import LLMClient


def format_scratchpad_summary(entries: list[ScratchpadEntry]) -> str:
    """Format scratchpad entries into a readable summary.

    Args:
        entries: List of ScratchpadEntry objects from the shared whiteboard.

    Returns:
        A newline-joined string of "[agent_name]: content" lines,
        or "No discussion yet." when the list is empty.
    """
    if not entries:
        return "No discussion yet."
    lines: list[str] = []
    for entry in entries:
        lines.append(f"[{entry.agent_name}]: {entry.content}")
    return "\n".join(lines)


def run_agent(
    config: AgentConfig,
    prompt: str,
    scratchpad_summary: str,
    client: LLMClient,
) -> str:
    """Execute a single Agent turn.

    The Agent sees its own system_prompt, a summary of the current
    whiteboard, and the specific prompt from PM.

    Args:
        config: AgentConfig holding the agent's name, role, and system_prompt.
        prompt: The specific question or task sent by the PM router.
        scratchpad_summary: Pre-formatted string of the shared whiteboard state.
        client: Any object satisfying the LLMClient Protocol.

    Returns:
        The agent's text response from the LLM.
    """
    user_content = (
        f"## Current Meeting Whiteboard\n\n{scratchpad_summary}\n\n"
        f"## Your Task\n\n{prompt}"
    )

    return client.chat(
        system=config.system_prompt,
        messages=[{"role": "user", "content": user_content}],
    )
