"""
FrankenClaw — web_scrape tool

Scrapes any URL and returns clean, LLM-ready markdown using Firecrawl.
Better than browser_task for simple page reading — Firecrawl handles
JavaScript rendering, strips nav/ads/chrome, returns just the content.

API key comes from the configured keys file under the "firecrawl" key.
If the key isn't there, returns a helpful error pointing at the keys file.
"""

import json

import httpx

from config import load_keys

FIRECRAWL_API = "https://api.firecrawl.dev/v1/scrape"


def _get_firecrawl_key() -> str:
    """Get Firecrawl API key from the configured keys file."""
    keys = load_keys()
    key = keys.get("firecrawl")
    if not key:
        raise KeyError(
            "No 'firecrawl' key found in keys.json (default ~/.frankenclaw/keys.json). "
            "Add it like this: {\"firecrawl\": \"fc-YOUR_KEY_HERE\"} "
            "Sign up at https://firecrawl.dev to get an API key."
        )
    return key


async def web_scrape(url: str, formats: str = "markdown") -> str:
    """
    Scrape a URL and return clean content using Firecrawl.

    Args:
        url: The URL to scrape.
        formats: Output format — "markdown" (default), "html", or "links".

    Returns:
        JSON with the scraped content, title, and source URL.
    """
    try:
        api_key = _get_firecrawl_key()
    except (FileNotFoundError, KeyError) as e:
        return json.dumps({"error": str(e)}, indent=2)

    # Build the request
    format_list = [f.strip() for f in formats.split(",")]
    payload = {
        "url": url,
        "formats": format_list,
    }

    try:
        async with httpx.AsyncClient(timeout=60) as client:
            resp = await client.post(
                FIRECRAWL_API,
                headers={
                    "Authorization": f"Bearer {api_key}",
                    "Content-Type": "application/json",
                },
                json=payload,
            )
            resp.raise_for_status()
            data = resp.json()

        # Extract the good stuff
        result = data.get("data", {})
        output = {
            "url": result.get("metadata", {}).get("sourceURL", url),
            "title": result.get("metadata", {}).get("title", ""),
        }

        # Include whichever formats were requested
        if "markdown" in format_list:
            output["markdown"] = result.get("markdown", "")
        if "html" in format_list:
            output["html"] = result.get("html", "")
        if "links" in format_list:
            output["links"] = result.get("links", [])

        return json.dumps(output, indent=2)

    except httpx.HTTPStatusError as e:
        error_body = e.response.text
        return json.dumps({
            "error": f"Firecrawl API error ({e.response.status_code}): {error_body}",
            "url": url,
        }, indent=2)
    except Exception as e:
        return json.dumps({
            "error": f"web_scrape failed: {str(e)}",
            "url": url,
        }, indent=2)
