"""Logistics chat agent using MCPTools to connect to Optovia MCP server."""

import os
from pathlib import Path

from agno.agent import Agent
from agno.models.google import Gemini
from agno.tools.mcp import MCPTools


def get_mcp_tools() -> MCPTools:
    """Create MCPTools instance for Optovia MCP server."""
    mcp_path = Path(__file__).parent.parent / "mcp" / "src" / "index.js"

    return MCPTools(
        command="node",
        args=[str(mcp_path)],
        env={
            "GEO_GRAPHQL_URL": os.getenv("GEO_GRAPHQL_URL", ""),
            "EXCHANGE_GRAPHQL_URL": os.getenv("EXCHANGE_GRAPHQL_URL", ""),
            "ORDERS_GRAPHQL_URL": os.getenv("ORDERS_GRAPHQL_URL", ""),
            "GEO_M2M_TOKEN": os.getenv("GEO_M2M_TOKEN", ""),
        },
    )


def create_logistics_agent(mcp_tools: MCPTools) -> Agent:
    """Create the logistics chat agent."""
    return Agent(
        id="logistics-agent",
        name="Logistics Agent",
        model=Gemini(id="gemini-2.0-flash"),
        tools=[mcp_tools],
        instructions="""You are a logistics assistant for the Optovia commodity trading platform.

You help users with:
- Finding products and offers on the exchange (cocoa, coffee, grains, etc.)
- Searching logistics nodes (ports, terminals, warehouses) by name or location
- Building optimal routes from offer sources to destination hubs
- Tracking order status and shipment timelines

Available tools:
- search_products: Find products by name
- search_offers: Find active offers, can filter by product/category/location
- search_nodes: Find logistics nodes by name or transport type
- nearest_nodes: Find nodes near given coordinates
- match_offers_with_route: Get route options from product offers to destination
- order_timeline: Get order details with stages and trips (requires auth)

Always be concise and provide actionable information. When showing routes,
include distances and estimated times. Format prices with currency.""",
        markdown=True,
    )
