import sys
import json
import requests
from bs4 import BeautifulSoup

def read_website(url: str) -> str:
    """
    Fetches the URL and extracts visible text.
    """
    try:
        # Use a standard user-agent to prevent simple bot-blocks
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
        }
        
        response = requests.get(url, headers=headers, timeout=15)
        response.raise_for_status()
        
        # Parse HTML
        soup = BeautifulSoup(response.content, "html.parser")
        
        # Remove noisy elements
        for script in soup(["script", "style", "noscript", "nav", "footer", "header", "aside"]):
            script.decompose()
            
        # Get text
        text = soup.get_text(separator="\n")
        
        # Clean up whitespace
        lines = (line.strip() for line in text.splitlines())
        chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
        text = "\n".join(chunk for chunk in chunks if chunk)
        
        # Truncate text to a reasonable limit (e.g., 20,000 characters) 
        # so it doesn't blow up the LLM context completely
        max_chars = 30000
        if len(text) > max_chars:
            text = text[:max_chars] + "\n... [Content Truncated]"
            
        return text

    except requests.exceptions.RequestException as e:
        return f"Error: Failed to fetch the URL. {str(e)}"
    except Exception as e:
        return f"Error: Failed to parse the website. {str(e)}"

if __name__ == "__main__":
    if len(sys.argv) > 1:
        payload = json.loads(sys.argv[1])
        url = payload.get("url")
        if not url:
            print(json.dumps({"error": "Missing required parameter 'url'"}))
            sys.exit(1)
            
        result = read_website(url)
        print(json.dumps({
            "status": "success" if not result.startswith("Error") else "error",
            "content": result
        }))
    else:
        print(json.dumps({"error": "No input payload provided"}))
        sys.exit(1)
