#!/usr/bin/env python3
"""
Browser Agent for Genesis System
Powered by Playwright and Gemini Vision.
"""

import os
import sys
import time
import base64
from pathlib import Path
from typing import Dict, Any, Optional

try:
    from playwright.sync_api import sync_playwright, Page, Browser, BrowserContext
except ImportError:
    print("ERROR: Playwright not installed. Run: pip install playwright && playwright install")
    sys.exit(1)

try:
    import google.generativeai as genai
except ImportError:
    print("ERROR: google-generativeai not installed. Run: pip install google-generativeai")
    sys.exit(1)


# Configuration
DEFAULT_MODEL = "gemini-3-pro-preview"
SCREENSHOT_DIR = Path("/mnt/e/genesis-system/data/screenshots")
SCREENSHOT_DIR.mkdir(parents=True, exist_ok=True)

# Ensure LD_LIBRARY_PATH is set for Playwright in this environment
LIBS_PATH = "/mnt/e/genesis-system/.venvs/playwright-libs/usr/lib/x86_64-linux-gnu"
if LIBS_PATH not in os.environ.get("LD_LIBRARY_PATH", ""):
    os.environ["LD_LIBRARY_PATH"] = f"{LIBS_PATH}:{os.environ.get('LD_LIBRARY_PATH', '')}"

import random

class BrowserAgent:
    def __init__(self, headless: bool = True, user_agents: list = None):
        self.headless = headless
        self.playwright = None
        self.browser = None
        self.context = None
        self.page = None
        self.api_key = os.environ.get("GOOGLE_API_KEY")
        self.user_agents = user_agents or [
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
            'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'
        ]
        
        if not self.api_key:
            # Try to load from file
            key_path = Path("/mnt/e/genesis-system/Credentials/gemini_api_key.txt")
            if key_path.exists():
                self.api_key = key_path.read_text().strip()
                os.environ["GOOGLE_API_KEY"] = self.api_key
        
        if self.api_key:
            genai.configure(api_key=self.api_key)
        else:
            print("WARNING: GOOGLE_API_KEY not found. Vision features will be disabled.")

    def start(self):
        """Start the browser."""
        self.playwright = sync_playwright().start()
        self.browser = self.playwright.chromium.launch(
            headless=self.headless,
            args=['--no-sandbox', '--disable-gpu']
        )
        
    def new_page(self):
        """Create a new page with a random user agent."""
        user_agent = random.choice(self.user_agents)
        self.context = self.browser.new_context(
            viewport={'width': 1920, 'height': 1080},
            user_agent=user_agent
        )
        self.page = self.context.new_page()
        print(f"Browser started (headless={self.headless}, user_agent={user_agent})")

    def stop(self):
        """Stop the browser."""
        if self.context:
            self.context.close()
        if self.browser:
            self.browser.close()
        if self.playwright:
            self.playwright.stop()
        print("Browser stopped")


    def navigate(self, url: str, wait_until: str = "domcontentloaded"):
        """Navigate to a URL with retries."""
        if not self.page:
            raise RuntimeError("Browser not started")
        
        for i in range(3): # 3 retries
            try:
                print(f"Navigating to {url} (attempt {i+1})...")
                response = self.page.goto(url, wait_until=wait_until, timeout=60000)
                print(f"Loaded {url} (Status: {response.status if response else 'Unknown'})")
                return True
            except Exception as e:
                print(f"Attempt {i+1} failed: Error navigating to {url}: {e}")
                time.sleep(5) # Wait before retrying
        
        return False

    def screenshot(self, filename: str = None) -> str:
        """Take a screenshot and return the path."""
        if not self.page:
            raise RuntimeError("Browser not started")
        
        if not filename:
            timestamp = int(time.time())
            filename = f"screenshot_{timestamp}.png"
            
        path = SCREENSHOT_DIR / filename
        self.page.screenshot(path=str(path), full_page=True)
        print(f"Screenshot saved to {path}")
        return str(path)

    def analyze_page(self, prompt: str = "Analyze this webpage.", screenshot_path: str = None) -> str:
        """Analyze the current page (or a specific screenshot) using Gemini Vision."""
        if not self.api_key:
            return "Error: No API key configured for vision analysis."
            
        if not screenshot_path:
            screenshot_path = self.screenshot()
            
        print(f"Analyzing {screenshot_path} with {DEFAULT_MODEL}...")
        
        try:
            model = genai.GenerativeModel(DEFAULT_MODEL)
            
            # Read image data
            with open(screenshot_path, "rb") as f:
                image_data = f.read()
                
            image_parts = [
                {
                    "mime_type": "image/png",
                    "data": image_data
                }
            ]
            
            response = model.generate_content([prompt, image_parts[0]])
            print("Analysis complete.")
            return response.text
        except Exception as e:
            print(f"Error during analysis: {e}")
            return f"Error: {e}"

    def get_html(self) -> str:
        """Get current page HTML."""
        if not self.page:
            raise RuntimeError("Browser not started")
        return self.page.content()

import argparse

def main():
    """Test the BrowserAgent."""
    parser = argparse.ArgumentParser(description="Browser Agent")
    parser.add_argument("--url", default="https://www.cairnsplumber.com", help="URL to visit")
    args = parser.parse_args()

    agent = BrowserAgent(headless=True)
    try:
        agent.start()
        
        # Test target
        url = args.url
        
        if agent.navigate(url):
            # Take screenshot
            screenshot_path = agent.screenshot("test_site.png")
            
            # Analyze
            prompt = """
            Analyze this webpage.
            1. What is the main purpose of this page?
            2. Describe the visual style.
            3. Are there any visible interactive elements?
            """
            analysis = agent.analyze_page(prompt, screenshot_path)
            
            print("\\n=== ANALYSIS RESULT ===\\n")
            print(analysis)
            print("\\n=======================\\n")
            
            # Save analysis
            Path("test_analysis.md").write_text(analysis)
            print("Analysis saved to test_analysis.md")
            
    except Exception as e:
        print(f"Test failed: {e}")
    finally:
        agent.stop()

if __name__ == "__main__":
    main()
