#!/usr/bin/env python3
"""
Scraping Diagnostics - Check if everything is ready for scraping
"""
import os
import sys
from pathlib import Path

class Colors:
    GREEN = '\033[92m'
    RED = '\033[91m'
    YELLOW = '\033[93m'
    BLUE = '\033[94m'
    BOLD = '\033[1m'
    END = '\033[0m'

def print_header(text):
    print(f"\n{Colors.BOLD}{Colors.BLUE}{'='*70}{Colors.END}")
    print(f"{Colors.BOLD}{Colors.BLUE}{text:^70}{Colors.END}")
    print(f"{Colors.BOLD}{Colors.BLUE}{'='*70}{Colors.END}\n")

def check(condition, success_msg, fail_msg, fix_msg=""):
    if condition:
        print(f"{Colors.GREEN}✅ {success_msg}{Colors.END}")
        return True
    else:
        print(f"{Colors.RED}❌ {fail_msg}{Colors.END}")
        if fix_msg:
            print(f"{Colors.YELLOW}   💡 Fix: {fix_msg}{Colors.END}")
        return False

def main():
    script_dir = Path(__file__).parent
    os.chdir(script_dir)

    print(f"\n{Colors.BOLD}🔍 FarmMatch Scraping Diagnostics{Colors.END}")
    print(f"{Colors.BOLD}Checking if scraping is ready to work...{Colors.END}")

    all_good = True

    # ============================================================================
    # 1. CHECK AUTHENTICATION
    # ============================================================================
    print_header("1. Authentication Check")

    auth_file = script_dir / "auth.json"
    has_auth = check(
        auth_file.exists(),
        "auth.json found - You have logged in before",
        "auth.json NOT found - You need to log in first!",
        "Run: python3 timed_login.py (or double-click 'Login to Properstar.command')"
    )
    all_good = all_good and has_auth

    if has_auth:
        import json
        try:
            with open(auth_file, 'r') as f:
                auth_data = json.load(f)

            has_cookies = 'cookies' in auth_data and len(auth_data['cookies']) > 0
            check(
                has_cookies,
                f"auth.json has {len(auth_data.get('cookies', []))} cookies",
                "auth.json exists but has no cookies",
                "Delete auth.json and run login again"
            )
        except Exception as e:
            check(False, "", f"auth.json is corrupted: {e}", "Delete auth.json and run login again")
            all_good = False

    # ============================================================================
    # 2. CHECK PLAYWRIGHT
    # ============================================================================
    print_header("2. Playwright Check")

    try:
        from playwright.async_api import async_playwright
        check(True, "Playwright module is installed", "")
    except ImportError:
        check(
            False,
            "",
            "Playwright is NOT installed",
            "Run: pip3 install playwright && playwright install"
        )
        all_good = False

    # ============================================================================
    # 3. CHECK SCRAPER SCRIPT
    # ============================================================================
    print_header("3. Scraper Scripts Check")

    scraper_file = script_dir / "favorites_scraper.py"
    check(
        scraper_file.exists(),
        "favorites_scraper.py found",
        "favorites_scraper.py NOT found",
        "Make sure you're in the correct directory"
    )

    pipeline_file = script_dir / "auto_scrape_favorites.py"
    check(
        pipeline_file.exists(),
        "auto_scrape_favorites.py found",
        "auto_scrape_favorites.py NOT found",
        "Make sure you're in the correct directory"
    )

    # ============================================================================
    # 4. TEST SCRAPER IMPORT
    # ============================================================================
    print_header("4. Script Import Check")

    try:
        sys.path.insert(0, str(script_dir))
        from favorites_scraper import scrape_favorites
        check(True, "favorites_scraper imports successfully", "")
    except Exception as e:
        check(
            False,
            "",
            f"Cannot import favorites_scraper: {e}",
            "Check for syntax errors or missing dependencies"
        )
        all_good = False

    # ============================================================================
    # 5. CHECK EXISTING DATA
    # ============================================================================
    print_header("5. Existing Data Check")

    csv_file = script_dir / "extracted_property_urls.csv"
    if csv_file.exists():
        import csv
        with open(csv_file, 'r') as f:
            reader = csv.DictReader(f)
            rows = list(reader)
        check(
            True,
            f"extracted_property_urls.csv found ({len(rows)} properties)",
            ""
        )
    else:
        check(
            False,
            "",
            "No CSV file found - never scraped before",
            "This is normal for first time - just run the scraper"
        )

    enriched_file = script_dir / "enriched_data.json"
    if enriched_file.exists():
        import json
        with open(enriched_file, 'r') as f:
            data = json.load(f)
        check(
            True,
            f"enriched_data.json found ({len(data)} properties)",
            ""
        )
    else:
        check(
            False,
            "",
            "No enriched_data.json found",
            "Run full pipeline to create this"
        )

    # ============================================================================
    # SUMMARY
    # ============================================================================
    print_header("Summary")

    if not has_auth:
        print(f"{Colors.RED}{Colors.BOLD}❌ CANNOT SCRAPE{Colors.END}")
        print(f"\n{Colors.YELLOW}You MUST log in to Properstar first!{Colors.END}")
        print(f"\n{Colors.BOLD}Three ways to log in:{Colors.END}")
        print(f"  1. {Colors.GREEN}Double-click: 'Login to Properstar.command'{Colors.END} (Easiest!)")
        print(f"  2. Run: {Colors.BLUE}./login.sh{Colors.END}")
        print(f"  3. Run: {Colors.BLUE}python3 timed_login.py{Colors.END}")
        print(f"\nBrowser will open, log in, wait 3 minutes → Done!")
    elif all_good:
        print(f"{Colors.GREEN}{Colors.BOLD}✅ ALL CHECKS PASSED!{Colors.END}")
        print(f"\n{Colors.BOLD}Scraping is ready to work!{Colors.END}")
        print(f"\n{Colors.BOLD}You can now:{Colors.END}")
        print(f"  • Click 'Full Update' button in UI")
        print(f"  • Run: {Colors.BLUE}python3 auto_scrape_favorites.py now{Colors.END}")
        print(f"  • Run: {Colors.BLUE}python3 favorites_scraper.py{Colors.END}")
    else:
        print(f"{Colors.YELLOW}{Colors.BOLD}⚠️  SOME ISSUES FOUND{Colors.END}")
        print(f"\nFix the issues above, then run this script again.")

    # ============================================================================
    # QUICK TEST OPTION
    # ============================================================================
    if has_auth:
        print(f"\n{Colors.BOLD}Want to test scraping now?{Colors.END}")
        print(f"Run: {Colors.GREEN}python3 favorites_scraper.py{Colors.END}")
        print(f"(Should take 5-10 minutes and create extracted_property_urls.csv)")

    print()

if __name__ == "__main__":
    main()
