#!/usr/bin/env python3
"""
Timed Login - Saves session after 3 minutes
Just log in within 3 minutes, it will automatically save
"""
import asyncio
from playwright.async_api import async_playwright

AUTH_JSON = "auth.json"
FAVORITES_URL = "https://www.properstar.nl/favorites"

async def timed_login():
    print("🚀 Timed Properstar Login (3 minute window)")
    print("=" * 70)
    print()

    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        context = await browser.new_context()
        page = await context.new_page()

        # Open favorites page
        print("🌐 Opening Properstar favorites page...")
        await page.goto(FAVORITES_URL, timeout=60000)

        # Give you 3 minutes to log in
        print()
        print("⏰ YOU HAVE 3 MINUTES!")
        print()
        print("1. Log in to Properstar in the browser window that just opened")
        print("2. Make sure you navigate to and see your favorites list")
        print("3. Wait - the script will automatically save after 3 minutes")
        print()

        for remaining in range(180, 0, -30):
            mins = remaining // 60
            secs = remaining % 60
            print(f"⏳ {mins}:{secs:02d} remaining...")
            await asyncio.sleep(30)

        print()
        print("⏱️  Time's up! Saving session now...")
        await asyncio.sleep(5)  # Extra buffer

        # Save session
        try:
            await context.storage_state(path=AUTH_JSON)
            print(f"\n✅ Session saved to {AUTH_JSON}!")
            print()
            print("🎉 SUCCESS! You can now use:")
            print("   - python3 favorites_scraper.py (will run headless)")
            print("   - python3 auto_scrape_favorites.py now (full pipeline)")
            print("   - UI button 'Full Update' (works automatically)")
            print()
        except Exception as e:
            print(f"\n❌ Error saving session: {e}")
            print("\nTroubleshooting:")
            print("1. Make sure you're logged in")
            print("2. Make sure you're on the favorites page")
            print("3. Try running the script again")

        await browser.close()

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