"""cashier.py - an agent with one tool. The model reads the order, Python does the sum.

    python cashier.py "2 khao soi, 1 thai iced tea"
"""
import sys

from agno.agent import Agent
from agno.models.ollama import Ollama

MODEL = "qwen2.5:3b"
PRICES = {"khao soi": 60, "nam ngiao": 50, "pad thai": 60, "pad see ew": 55,
          "thai iced tea": 30, "longan juice": 25, "water": 10}


def bill(order: str) -> str:
    """Add up an order of the noodle shop.

    Args:
        order: the dishes and how many of each, such as "2 khao soi, 1 water"
    """
    lines = []
    total = 0
    for part in order.lower().split(","):
        words = part.split()
        if len(words) < 2 or not words[0].isdigit():
            return "I cannot read this part of the order: " + part.strip()
        count = int(words[0])
        dish = " ".join(words[1:])
        if dish not in PRICES:
            return "I do not know the price of " + dish + "."
        cost = count * PRICES[dish]
        lines.append(f"- {count} x {dish} = {cost} baht")
        total = total + cost
    lines.append(f"- Total: {total} baht")
    return "\n".join(lines)


def main():
    order = sys.argv[1]
    cashier = Agent(
        name="Cashier",
        # temperature 0: the model gives the likeliest answer every time
        model=Ollama(id=MODEL, options={"temperature": 0}),
        tools=[bill],
        instructions="You are the cashier of a noodle shop. Use the bill tool for "
                     "every order. Then reply with the bill the tool returned, word "
                     "for word. Never work out or guess a price yourself.",
    )
    run = cashier.run(order)
    for call in run.tools:
        print("The agent called:", call.tool_name, call.tool_args)
        print("The tool returned:")
        print(call.result)
    print("The agent says:")
    print(run.content)


if __name__ == "__main__":
    main()
