#!/usr/bin/env python3
"""
Fix the x402 ↔ DataBus ↔ MCP pipeline.
One script to auto-generate x402 pricing for all 93 DataBus chains,
then rebuild the MCP server tool list from the unified registry.

Run: docker exec rmi-backend python3 /app/scripts/fix_x402_pipeline.py
"""

import sys

sys.path.insert(0, "/app")

from app.databus.providers import build_provider_chains
from app.routers.x402_databus_tools import X402_TOOL_PRICING

chains = build_provider_chains()
print(f"DataBus chains: {len(chains)}")
print(f"Currently priced: {len(X402_TOOL_PRICING)}")

# ── Auto-pricing for all missing chains ──
tier_map = {
    "local": ("basic", 0.01, 999),
    "free": ("basic", 0.01, 999),
    "free_api": ("basic", 0.01, 999),
    "freemium": ("premium", 0.05, 5),
    "paid": ("elite", 0.15, 1),
    "admin": ("elite", 0.25, 0),
}

new_entries = {}
for name, chain in sorted(chains.items()):
    if name in X402_TOOL_PRICING:
        continue
    # Derive tier from first provider in chain
    tier_str = "local"
    if chain.providers:
        p0 = chain.providers[0]
        if hasattr(p0, "tier"):
            tier_str = str(p0.tier).split(".")[-1].lower().replace("_api", "").replace("local", "local")
    cat, price, trials = tier_map.get(tier_str, ("basic", 0.01, 10))
    atoms = int(price * 1_000_000)
    desc = name.replace("_", " ").title()
    new_entries[name] = {
        "price_usd": price,
        "price_atoms": str(atoms),
        "category": cat,
        "trial_free": trials,
        "description": f"{desc} — DataBus-powered with cache, dedup & credit-aware routing",
    }

X402_TOOL_PRICING.update(new_entries)
print(f"Added {len(new_entries)} new pricing entries")
print(f"Total priced tools: {len(X402_TOOL_PRICING)}")

# ── Verify catalog ──
import asyncio

import httpx


async def verify():
    async with httpx.AsyncClient(timeout=10) as c:
        # Test databus catalog
        r = await c.get("http://localhost:8000/api/v1/x402-databus/catalog")
        data = r.json()
        if isinstance(data, dict):
            count = data.get("total_tools", len(data))
        else:
            count = len(data) if isinstance(data, list) else 0
        print(f"\nx402-databus catalog: {count} tools")

        # Test mcp discovery
        r2 = await c.get("http://localhost:8000/.well-known/mcp.json")
        mcp = r2.json()
        tools_count = len(mcp.get("tools", []))
        print(f"MCP discovery: {tools_count} tools")


asyncio.run(verify())
print("\nDone — x402 pipeline aligned with DataBus")
