"""
Test 2: Bank Account Data Extraction

Demonstrates:
- Login to bank portal
- Extract account balances
- Extract transaction history
- Extract credit card info
- Handle multi-page data
"""

import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from browser_tool import BrowserTool


def test_bank_extraction():
    """Test extracting data from a bank account"""
    
    browser = BrowserTool(
        agent_id="sofiia-test",
        headless=False,
        stealth=True
    )
    
    print("=== Starting browser session ===")
    session = browser.start_session()
    print(f"Session: {session['session_id']}")
    
    # Navigate to demo bank site (using a simple example)
    print("\n=== Navigating to bank login ===")
    browser.goto("https://demo.aponline.com.au/login")
    
    # Take screenshot
    browser.screenshot("/tmp/bank_login.png")
    
    # Observe login fields
    print("\n=== Observing bank login page ===")
    actions = browser.observe("What login fields are available?")
    print(f"Found {len(actions)} elements")
    
    # Note: In production, you'd use actual bank credentials
    # For demo, we'll just show the extraction part
    
    # After login, extract account data
    print("\n=== Extracting account balances ===")
    balances = browser.extract(
        "Extract all account balances and account numbers",
        schema={
            "account_name": "string",
            "account_number": "string",
            "balance": "number",
            "currency": "string"
        }
    )
    print(f"Found {balances.get('count', 0)} accounts")
    for acc in balances.get('data', [])[:5]:
        print(f"  - {acc}")
    
    # Extract transactions
    print("\n=== Extracting transactions ===")
    transactions = browser.extract(
        "Extract recent transactions with date, description, amount",
        schema={
            "date": "string",
            "description": "string",
            "amount": "number",
            "type": "string"
        }
    )
    print(f"Found {transactions.get('count', 0)} transactions")
    for tx in transactions.get('data', [])[:10]:
        print(f"  - {tx}")
    
    # Extract credit card info
    print("\n=== Extracting credit card info ===")
    cards = browser.extract(
        "Extract credit card details",
        schema={
            "card_name": "string",
            "last_4": "string",
            "balance": "number",
            "limit": "number"
        }
    )
    print(f"Found {cards.get('count', 0)} cards")
    
    # Extract pending payments
    print("\n=== Extracting pending payments ===")
    pending = browser.extract("Extract pending payments or bills")
    print(f"Found {pending.get('count', 0)} pending items")
    
    # Screenshot of dashboard
    browser.screenshot("/tmp/bank_dashboard.png")
    
    # Close
    print("\n=== Closing session ===")
    browser.close_session()
    
    return True


def test_crypto_exchange():
    """Test extracting data from crypto exchange"""
    
    browser = BrowserTool(agent_id="sofiia-test", headless=True)
    session = browser.start_session()
    
    # Navigate to crypto exchange (e.g., Binance demo)
    print("\n=== Navigating to exchange ===")
    browser.goto("https://www.binance.com/en/trade/BTC_USDT")
    
    # Wait for page load
    import time
    time.sleep(3)
    
    # Extract balances
    print("\n=== Extracting wallet balances ===")
    balances = browser.extract("Extract all wallet balances")
    print(f"Found {balances.get('count', 0)} wallets")
    
    # Extract current price
    print("\n=== Extracting BTC price ===")
    price = browser.extract("Extract BTC/USDT current price")
    print(f"Price data: {price}")
    
    # Extract open orders
    orders = browser.extract("Extract open orders")
    print(f"Open orders: {orders.get('count', 0)}")
    
    browser.screenshot("/tmp/exchange_dashboard.png")
    browser.close_session()
    
    return True


if __name__ == "__main__":
    test_bank_extraction()
