import asyncio
from playwright.async_api import async_playwright
import os

async def capture_screenshots(url, name_prefix):
    async with async_playwright() as p:
        # Launch browser
        browser = await p.chromium.launch()

        resolutions = [
            {"name": "smartwatch", "width": 300, "height": 300},
            {"name": "smartphone", "width": 375, "height": 667},
            {"name": "tablet", "width": 768, "height": 1024},
            {"name": "laptop", "width": 1366, "height": 768},
            {"name": "pc", "width": 1920, "height": 1080},
            {"name": "4k", "width": 3840, "height": 2160},
            {"name": "8k", "width": 7680, "height": 4320},
        ]

        for res in resolutions:
            # Create a context with the specific viewport
            context = await browser.new_context(viewport={"width": res["width"], "height": res["height"]})
            page = await context.new_page()

            try:
                await page.goto(url, wait_until="networkidle", timeout=10000)
                # Wait for some time to ensure layout is settled
                await asyncio.sleep(2)

                output_dir = f"verification/{name_prefix}"
                os.makedirs(output_dir, exist_ok=True)

                # Take screenshot of the viewport (not full page) to see what's visible
                await page.screenshot(path=f"{output_dir}/{res['name']}.png")
                print(f"Captured {name_prefix} - {res['name']}")
            except Exception as e:
                print(f"Failed to capture {name_prefix} - {res['name']}: {e}")

            await page.close()
            await context.close()

        await browser.close()

async def main():
    # Test User Home
    await capture_screenshots("http://localhost:8000/user/index.php", "user_home")
    # Test Admin Dashboard
    await capture_screenshots("http://localhost:8000/admin/index.php", "admin_dashboard")
    # Test Fleet Management
    await capture_screenshots("http://localhost:8000/admin/fleet.php", "admin_fleet")

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