"""The shared task every framework implements: a tiny order-support agent.

Two tools over a fake order database, one system prompt, one user message.
Every implementation imports from here so the comparison is apples to apples.
"""

from __future__ import annotations

import os

# Change the model for every implementation at once. Frameworks spell provider-qualified
# names differently (anthropic:..., anthropic/...), so each file builds its own string from this.
MODEL = os.environ.get("AGENT_MODEL", "claude-opus-5-5")

ORDERS = {
    "A-1001": {"item": "Trail running shoes", "status": "processing", "shipped": False, "total": 89.00},
    "A-1002": {"item": "Rain jacket", "status": "in transit", "shipped": True, "total": 140.00},
}

REFUNDS: list[dict] = []  # side effects land here so tests and demos can inspect them

SYSTEM_PROMPT = (
    "You are a customer support agent for an outdoor gear shop. "
    "Use get_order_status to look up orders before answering. "
    "Only refund an order if it has not shipped; otherwise explain that the customer can return it after delivery. "
    "Reply in two or three short sentences."
)

USER_PROMPT = "Where is order A-1001? If it hasn't shipped, refund it because I changed my mind."


def get_order_status(order_id: str) -> str:
    """Look up the item, status and shipping state of an order by its ID, e.g. A-1001."""
    order = ORDERS.get(order_id.strip().upper())
    if not order:
        return f"No order found with ID {order_id}."
    shipped = "has shipped" if order["shipped"] else "has not shipped yet"
    return f"Order {order_id}: {order['item']}, status '{order['status']}', {shipped}, total ${order['total']:.2f}."


def refund_order(order_id: str, reason: str) -> str:
    """Refund an order that has not shipped yet. Requires the order ID and the customer's reason."""
    key = order_id.strip().upper()
    order = ORDERS.get(key)
    if not order:
        return f"Cannot refund: no order {order_id}."
    if order["shipped"]:
        return f"Cannot refund {order_id}: it has already shipped. The customer can return it after delivery."
    if any(r["order_id"] == key for r in REFUNDS):
        return f"Order {order_id} was already refunded."
    REFUNDS.append({"order_id": key, "reason": reason, "amount": order["total"]})
    order["status"] = "refunded"
    return f"Refunded ${order['total']:.2f} for order {order_id}. Reason recorded: {reason}."


def reset() -> None:
    """Restore the fake database (used by tests and repeated demo runs)."""
    REFUNDS.clear()
    ORDERS["A-1001"]["status"] = "processing"
