import asyncio
from playwright.async_api import async_playwright
import os

async def main():
    html_path = os.path.abspath("app/static/bim/index.html")
    uri = f"file:///{html_path.replace(chr(92), '/')}"
    
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        
        errors = []
        page.on("pageerror", lambda err: errors.append(f"PageError: {err}"))
        page.on("console", lambda msg: errors.append(f"Console {msg.type}: {msg.text}") if msg.type == "error" else None)

        await page.set_viewport_size({"width": 1920, "height": 1080})
        await page.goto(uri, wait_until="networkidle")
        
        # Wait a bit for 3D to render
        await asyncio.sleep(2)
        
        print("Clicking export video button...")
        # Evaluate startVideoExport() or exportLifetimeVideo()
        await page.evaluate("exportLifetimeVideo(500);") # test with 500ms
        
        await asyncio.sleep(2)
        
        if errors:
            print("ERRORS FOUND:")
            for e in errors:
                print(e)
        else:
            print("No errors during export!")

        await browser.close()

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