import os
import requests
from bs4 import BeautifulSoup

def fetch_municipal_data(municipality: str) -> str:
    """
    Uses the Tavily Search API to gather sign code or zoning data
    for a given municipality. Returns readable text content.
    """
    api_key = os.getenv("TAVILY_API_KEY")
    if not api_key:
        return "(Missing Tavily API key)"

    query = f"{municipality} sign code site:.gov OR site:.us"
    try:
        # Step 1: Search the web with Tavily
        resp = requests.post(
            "https://api.tavily.com/search",
            json={
                "query": query,
                "num_results": 5,
                "include_answer": False,
                "include_images": False,
                "include_domains": ["gov", "us"]
            },
            headers={"Authorization": f"Bearer {api_key}"},
            timeout=15,
        )
        resp.raise_for_status()
        results = resp.json().get("results", [])
        texts = []
        # Step 2: Download and clean up the found pages
        for r in results:
            url = r.get("url")
            if not url:
                continue
            try:
                html = requests.get(url, timeout=10).text
                soup = BeautifulSoup(html, "html.parser")
                text = " ".join(t.strip() for t in soup.stripped_strings)
                texts.append(f"--- Source: {url} ---\n{text[:8000]}")
            except Exception:
                continue
        return "\n\n".join(texts) if texts else "No relevant municipal data found."
    except Exception as e:
        return f"(Web lookup failed: {e})"
