import asyncio
from playwright.async_api import async_playwright
from aegis.core.state import AegisStateTracker
from aegis.injector import AegisBrowserInjector

async def run_stripe_injection_test():
    db_path = ":memory:"
    seal_id = "test-stripe-seal-001"
    
    # 1. Prepare fake card data in the database
    tracker = AegisStateTracker(db_path=db_path)
    print("[Aegis DB] Inserting test virtual card into Vault...")
    tracker.record_seal(
        seal_id=seal_id,
        amount=50.0,
        vendor="Stripe Demo",
        status="Issued",
        card_number="4242424242424242",
        cvv="123",
        expiration_date="12/30"
    )
    
    # 2. Launch browser with CDP port open
    print("[Playwright] Launching browser with --remote-debugging-port=9222...")
    async with async_playwright() as p:
        # We launch a persistent context or normal browser with the debug port
        browser = await p.chromium.launch(
            headless=False,
            args=["--remote-debugging-port=9222"]
        )
        
        page = await browser.new_page()
        import os
        # 3. Navigate to the simulated Stripe cross-origin layout
        demo_url = f"file://{os.path.abspath('examples/dummy_checkout.html')}"
        print(f"[Playwright] Navigating to {demo_url} ...")
        try:
            await page.goto(demo_url, wait_until="domcontentloaded", timeout=15000)
        except Exception as e:
            print(f"Navigation error: {e}")
        
        # 4. Wait for Stripe's cross-origin iframes to load
        print("[Playwright] Waiting 2 seconds to simulate Stripe iframes to load...")
        await asyncio.sleep(2)
        
        # 5. Instantiate the AegisBrowserInjector and connect via CDP
        print("[Aegis Injector] Connecting via CDP to inject payment info...")
        injector = AegisBrowserInjector(tracker)
        
        success = await injector.inject_payment_info(seal_id, cdp_url="http://localhost:9222")
        
        if success:
            print("[Aegis Injector] ✅ SUCCESS! Card details injected into Stripe iframe.")
        else:
            print("[Aegis Injector] ❌ FAILED! Could not find Stripe input fields.")
            
        # Save screenshot for proof in correct Artifact Path
        screenshot_path = "/Users/tpemist/.gemini/antigravity/brain/511eb8dc-e470-4195-995a-2d214717f5e9/stripe_proof.png"
        await page.screenshot(path=screenshot_path)
        print(f"Screenshot saved to {screenshot_path}")
            
        print("[Playwright] Leaving browser open for 5 seconds for visual inspection...")
        await asyncio.sleep(5)
        
        # 6. Cleanup
        await browser.close()
        tracker.close()

if __name__ == "__main__":
    asyncio.run(run_stripe_injection_test())
