"""
Sync script: Get new/removed favorites from Properstar and update analysis
"""
import asyncio
import csv
import os
import pandas as pd
from playwright.async_api import async_playwright

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

async def get_current_favorites():
    """Scrape current favorites from Properstar"""
    print("🔄 Fetching current favorites from Properstar...\n")

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

        # Load existing auth if available
        if os.path.exists(AUTH_JSON):
            context = await browser.new_context(storage_state=AUTH_JSON)
            print("✅ Using existing authentication\n")
        else:
            context = await browser.new_context()
            print("⚠️ No auth.json found. You'll need to log in manually.\n")

        page = await context.new_page()
        await page.goto(FAVORITES_URL)

        # Wait for manual login if needed
        try:
            await page.wait_for_selector("article.item-adaptive.card-basic", timeout=5000)
        except:
            print("⏳ Please log in manually in the browser window...")
            input("✅ Press Enter once you're logged in and on the favorites page...")
            # Save auth for next time
            await context.storage_state(path=AUTH_JSON)
            print("💾 Authentication saved\n")

        await page.wait_for_load_state("networkidle")

        property_data = []
        page_number = 1

        while True:
            print(f"📄 Scanning page {page_number}...")
            await page.wait_for_timeout(2000)

            listings = await page.query_selector_all("article.item-adaptive.card-basic")
            if not listings:
                print(f"⛔️ No listings found on page {page_number}")
                break

            for listing in listings:
                # Skip archived
                class_attr = await listing.get_attribute("class")
                if "archived" in class_attr:
                    continue

                url_element = await listing.query_selector("a[href*='/property-for-sale']")
                if url_element:
                    url = await url_element.get_attribute("href")
                    url = f"https://www.properstar.com{url}" if url.startswith("/") else url
                else:
                    url = ""

                location_element = await listing.query_selector("div.item-location")
                location = await location_element.inner_text() if location_element else ""
                location = location.replace("Het juiste adres aanvragen", "").strip()

                price_element = await listing.query_selector("span[itemprop='price']")
                price_text = await price_element.inner_text() if price_element else ""
                price_number = ''.join(filter(str.isdigit, price_text))

                if url:
                    property_data.append({
                        "URL": url,
                        "Locatie": location,
                        "Prijs": price_number
                    })

            # Try next page
            next_button = await page.query_selector("a[aria-label='Next page']")
            if next_button and await next_button.is_enabled():
                await next_button.click()
                page_number += 1
                await page.wait_for_load_state("load")
            else:
                break

        await browser.close()

        print(f"\n✅ Found {len(property_data)} current favorites\n")
        return property_data

async def sync_and_analyze():
    """Compare current favorites with existing data and identify changes"""

    # Get current favorites from Properstar
    current_favorites = await get_current_favorites()
    current_urls = set(prop['URL'] for prop in current_favorites)

    # Load existing data
    if os.path.exists(CURRENT_CSV):
        existing_df = pd.read_csv(CURRENT_CSV)
        if 'URL' in existing_df.columns:
            existing_urls = set(existing_df['URL'].tolist())
        else:
            existing_urls = set()
    else:
        existing_urls = set()

    # Identify new and removed properties
    new_urls = current_urls - existing_urls
    removed_urls = existing_urls - current_urls

    # Mark removed properties in analysis CSV
    if removed_urls and os.path.exists("analysis_output.csv"):
        analysis_df = pd.read_csv("analysis_output.csv")
        if 'Status' not in analysis_df.columns:
            analysis_df['Status'] = 'Active'

        if 'URL' in analysis_df.columns:
            # Mark removed properties
            analysis_df.loc[analysis_df['URL'].isin(removed_urls), 'Status'] = 'Removed'
            analysis_df.to_csv("analysis_output.csv", index=False, encoding='utf-8')
            print(f"💾 Marked {len(removed_urls)} properties as 'Removed' in analysis_output.csv\n")

    print("=" * 60)
    print("📊 SYNC REPORT")
    print("=" * 60)
    print(f"Current favorites: {len(current_urls)}")
    print(f"Previously tracked: {len(existing_urls)}")
    print(f"🆕 New properties: {len(new_urls)}")
    print(f"🗑️ Removed properties: {len(removed_urls)}")
    print("=" * 60)

    if new_urls:
        print("\n🆕 NEW PROPERTIES:")
        for url in new_urls:
            prop = next(p for p in current_favorites if p['URL'] == url)
            print(f"  • {prop['Locatie']} - €{prop['Prijs']} - {url}")
        print("\n💡 Run 'analyze_from_urls.py' to analyze these new properties")

    if removed_urls:
        print("\n🗑️ REMOVED PROPERTIES:")
        for url in list(removed_urls)[:10]:  # Show first 10
            print(f"  • {url}")
        if len(removed_urls) > 10:
            print(f"  ... and {len(removed_urls) - 10} more")

    # Save updated list
    with open(CURRENT_CSV, "w", newline="", encoding="utf-8") as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=["URL", "Locatie", "Prijs"])
        writer.writeheader()
        for prop in current_favorites:
            writer.writerow({
                "URL": prop["URL"],
                "Locatie": prop["Locatie"],
                "Prijs": prop["Prijs"]
            })

    print(f"\n💾 Updated {CURRENT_CSV}")

    # If there are new properties, optionally analyze them
    if new_urls:
        print("\n" + "=" * 60)
        proceed = input("📊 Do you want to analyze new properties now? (y/n): ")
        if proceed.lower() == 'y':
            print("⚠️ Note: This will use OpenAI API credits")
            print("Starting analysis in 3 seconds...")
            await asyncio.sleep(3)

            # Import and run analysis
            import subprocess
            result = subprocess.run(["python3", "analyze_from_urls.py"])

            if result.returncode == 0:
                print("\n✅ Analysis complete!")
                print("💡 Run 'parse_criteria.py' to update the enriched data")
            else:
                print("\n❌ Analysis failed. Check the error messages above.")

    return {
        'new': list(new_urls),
        'removed': list(removed_urls),
        'current': list(current_urls)
    }

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