import asyncio
from pathlib import Path

from playwright.async_api import async_playwright


OUTPUT_FILE: Path = Path("screenshot.png")
TARGET_URL: str = "https://linux.org"


async def capture_screenshot(url: str, output_path: Path) -> None:
    """Navigate to a URL, wait for the page to load, and save a screenshot.

    Args:
        url: The URL to navigate to.
        output_path: The file path where the screenshot will be saved.
    """
    async with async_playwright() as pw:
        # Launch Chromium in headless mode
        browser = await pw.chromium.launch(headless=True)
        page = await browser.new_page(
            viewport={"width": 1280, "height": 900}
        )

        # Navigate and wait until network is idle (page fully loaded)
        await page.goto(url, wait_until="networkidle", timeout=30_000)

        # Persist screenshot to disk
        await page.screenshot(path=str(output_path), full_page=False)
        await browser.close()

    print(f"Screenshot saved to {output_path.resolve()}")


def main() -> None:
    """Entry point: capture a screenshot of the target URL."""
    asyncio.run(capture_screenshot(TARGET_URL, OUTPUT_FILE))


if __name__ == "__main__":
    main()
