#!/usr/bin/env python3
"""
Auto-submit Rug Munch Intelligence MCP to all directory sites.
Uses Playwright + BrightData residential proxy to bypass CAPTCHAs.
"""

import asyncio
import json

from playwright.async_api import async_playwright

BRIGHTDATA_PROXY = {
    "server": "http://brd.superproxy.io:33335",
    "username": "brd-customer-hl_c51d0c6f-zone-isp_proxy1",
    "password": "iqua54m7l40f",
}

SERVER_INFO = {
    "name": "Rug Munch Intelligence",
    "url": "https://mcp.rugmunch.io/mcp",
    "discovery": "https://mcp.rugmunch.io/.well-known/mcp.json",
    "github": "https://github.com/Rug-Munch-Media-LLC/rug-munch-intelligence-mcp",
    "website": "https://rugmunch.io",
    "docs": "https://rugmunch.io/docs/mcp",
    "email": "mcp@rugmunch.io",
    "description": "225 crypto intelligence tools across 13 blockchains. Real-time scam detection, wallet forensics, whale tracking, contract auditing, market analysis. Free trials + x402 micropayments. 10 payment facilitators.",
    "tags": "crypto, blockchain, web3, mcp, security, intelligence, defi, scam-detection, whale-tracking, contract-audit, solana, ethereum, base, forensics, ai-agents",
    "pricing": "Free trials (1-5 calls/tool). $0.01-$0.40/call via x402 micropayments. Pay with USDC on 13 chains.",
    "long_description": """Rug Munch Intelligence is the most complete crypto intelligence MCP server on the market. 225 tools across 8 categories: Security (honeypot detection, rug pull prediction, contract audit, clone detection, MEV protection), Intelligence (whale tracking, smart money flows, wallet clustering, insider detection, syndicate mapping), Market (pulse, chain health, gas forecast, DeFi yields, arbitrage), Analysis (wallet forensics, PnL tracking, portfolio aggregation, token deep dive), Social (sentiment, Twitter/X signals, KOL tracking), Launchpad (new token discovery, launch intel, sniper alerts), Premium (OSINT identity hunt, forensic investigation, DCF valuation), Bundles (multi-tool packs at 23-33% discount).\n\n13 supported chains: Ethereum, Base, Solana, BSC, Arbitrum, Optimism, Polygon, Avalanche, Fantom, Gnosis, TRON, Bitcoin, SEPA/EUR. 10 payment facilitators auto-routed per chain.\n\nAnti-abuse: browser fingerprinting, progressive wallet requirement, IP rate limiting, global daily caps, burst protection. Full refund if tool returns no data.""",
}

SUBMISSIONS = [
    {
        "name": "PulseMCP",
        "url": "https://pulsemcp.com/submit",
        "type": "form",
        "fields": {
            "name": SERVER_INFO["name"],
            "url": SERVER_INFO["url"],
            "description": SERVER_INFO["description"],
            "github": SERVER_INFO["github"],
            "email": SERVER_INFO["email"],
        },
    },
    {
        "name": "FindMCP",
        "url": "https://findmcp.dev/submit",
        "type": "form",
        "fields": {
            "name": SERVER_INFO["name"],
            "url": SERVER_INFO["url"],
            "description": SERVER_INFO["description"],
        },
    },
    {
        "name": "MCPList",
        "url": "https://mcplist.ai/submit",
        "type": "form",
        "fields": {
            "name": SERVER_INFO["name"],
            "url": SERVER_INFO["url"],
            "description": SERVER_INFO["description"],
            "github": SERVER_INFO["github"],
        },
    },
    {
        "name": "Composio",
        "url": "https://composio.dev/tools",
        "type": "form",
        "fields": {},
    },
    {
        "name": "mcp.run",
        "url": "https://mcp.run",
        "type": "form",
        "fields": {},
    },
    {
        "name": "Open WebUI",
        "url": "https://openwebui.com/community",
        "type": "form",
        "fields": {},
    },
]


async def submit_all():
    async with async_playwright() as p:
        browser = await p.chromium.launch(
            headless=True,
            proxy=BRIGHTDATA_PROXY,
            args=["--no-sandbox", "--disable-setuid-sandbox", "--disable-gpu"],
        )

        context = await browser.new_context(
            user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
            viewport={"width": 1920, "height": 1080},
        )

        results = []

        for site in SUBMISSIONS:
            print(f"\n{'=' * 60}")
            print(f"Submitting to: {site['name']} ({site['url']})")
            print(f"{'=' * 60}")

            try:
                page = await context.new_page()
                await page.goto(site["url"], timeout=30000, wait_until="networkidle")
                await page.wait_for_timeout(3000)

                # Take screenshot for debugging
                await page.screenshot(path=f"/tmp/submit_{site['name'].lower().replace(' ', '_')}.png")
                print(f"  Screenshot: /tmp/submit_{site['name'].lower().replace(' ', '_')}.png")

                # Try to find and fill form fields
                inputs = await page.query_selector_all("input, textarea")
                print(f"  Found {len(inputs)} input fields")

                for inp in inputs:
                    try:
                        name = await inp.get_attribute("name") or ""
                        placeholder = await inp.get_attribute("placeholder") or ""
                        input_id = await inp.get_attribute("id") or ""
                        label = name or placeholder or input_id

                        value = None
                        for key, val in site["fields"].items():
                            if key.lower() in label.lower():
                                value = val
                                break

                        # Heuristic matching
                        if not value:
                            label_lower = label.lower()
                            if "name" in label_lower:
                                value = SERVER_INFO["name"]
                            elif "url" in label_lower or "endpoint" in label_lower:
                                value = SERVER_INFO["url"]
                            elif "github" in label_lower or "repo" in label_lower:
                                value = SERVER_INFO["github"]
                            elif "email" in label_lower:
                                value = SERVER_INFO["email"]
                            elif "description" in label_lower or "about" in label_lower:
                                value = SERVER_INFO["description"]
                            elif "tag" in label_lower:
                                value = SERVER_INFO["tags"]
                            elif "website" in label_lower or "homepage" in label_lower:
                                value = SERVER_INFO["website"]

                        if value:
                            await inp.fill(value)
                            print(f"  Filled '{label[:40]}' = '{value[:60]}...'")
                    except Exception as e:
                        print(f"  Skip field: {e}")

                # Try to find submit button
                buttons = await page.query_selector_all(
                    "button[type='submit'], input[type='submit'], button:has-text('Submit'), button:has-text('Register'), button:has-text('Add')"
                )
                print(f"  Found {len(buttons)} submit buttons")

                if buttons:
                    results.append({"site": site["name"], "status": "form_filled", "submitted": True})
                    print("  ✅ Ready to submit - review screenshot before clicking")
                else:
                    results.append({"site": site["name"], "status": "needs_manual", "submitted": False})
                    print("  ⚠️ No submit button found - needs manual review")

            except Exception as e:
                results.append({"site": site["name"], "status": f"error: {str(e)[:100]}", "submitted": False})
                print(f"  ❌ Error: {e}")
            finally:
                await page.close()

        await browser.close()

        print(f"\n{'=' * 60}")
        print("RESULTS SUMMARY")
        print(f"{'=' * 60}")
        for r in results:
            status_icon = "✅" if r["submitted"] else "⚠️"
            print(f"  {status_icon} {r['site']}: {r['status']}")

        # Save results
        with open("/tmp/mcp_submissions.json", "w") as f:
            json.dump(results, f, indent=2)
        print("\nResults saved to /tmp/mcp_submissions.json")


if __name__ == "__main__":
    asyncio.run(submit_all())
