#!/usr/bin/env python3
"""
Debug script to see what's on a property page
"""
import asyncio
from playwright.async_api import async_playwright
from pathlib import Path

async def debug_property_page(url):
    async with async_playwright() as p:
        # Check if auth.json exists
        auth_file = Path("auth.json")
        auth_exists = auth_file.exists()

        if auth_exists:
            browser = await p.chromium.launch(headless=False)  # Visible for debugging
            context = await browser.new_context(storage_state="auth.json")
        else:
            browser = await p.chromium.launch(headless=False)
            context = await browser.new_context()

        page = await context.new_page()

        try:
            print(f"Loading: {url}")
            await page.goto(url, timeout=30000)
            await page.wait_for_load_state("domcontentloaded")
            await asyncio.sleep(3)

            # Get the full HTML
            html = await page.content()

            # Save to file
            with open('/tmp/property_page_debug.html', 'w', encoding='utf-8') as f:
                f.write(html)

            print("\n✅ HTML saved to /tmp/property_page_debug.html")

            # Try to find any text containing size keywords
            print("\n🔍 Looking for size-related text...")
            body_text = await page.evaluate('() => document.body.innerText')

            lines = body_text.split('\n')
            for i, line in enumerate(lines):
                line_lower = line.lower()
                if any(keyword in line_lower for keyword in ['m²', 'm2', 'oppervlakte', 'size', 'square']):
                    print(f"Line {i}: {line.strip()}")

            # Keep browser open for manual inspection
            print("\n🔍 Browser window is open - inspect the page manually")
            print("Press Enter to close...")
            await asyncio.sleep(30)  # Keep open for 30 seconds

        finally:
            await browser.close()

if __name__ == "__main__":
    import sys
    url = sys.argv[1] if len(sys.argv) > 1 else "https://www.properstar.nl/listing/102754054"
    asyncio.run(debug_property_page(url))
