#!/usr/bin/env python3
"""
Complete Paradisomatch System Check
Finds all missing pieces and issues
"""
import os
import sys
import json
import subprocess
from pathlib import Path

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

issues = []
warnings = []

def check(condition, success_msg, fail_msg, fix_msg="", is_warning=False):
    global issues, warnings
    if condition:
        print(f"{Colors.GREEN}✅ {success_msg}{Colors.END}")
        return True
    else:
        if is_warning:
            print(f"{Colors.YELLOW}⚠️  {fail_msg}{Colors.END}")
            warnings.append((fail_msg, fix_msg))
        else:
            print(f"{Colors.RED}❌ {fail_msg}{Colors.END}")
            issues.append((fail_msg, fix_msg))
        if fix_msg:
            print(f"   💡 {fix_msg}")
        return False

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

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

    print(f"\n{Colors.BOLD}🔍 COMPLETE FARMMATCH SYSTEM CHECK{Colors.END}\n")

    # ========================================================================
    # 1. AUTHENTICATION
    # ========================================================================
    section("1. Authentication & Login")

    auth_file = script_dir / "auth.json"
    has_auth = auth_file.exists()

    if has_auth:
        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 valid ({len(auth_data.get('cookies', []))} cookies)",
                  "auth.json exists but no cookies")
        except:
            check(False, "", "auth.json is corrupted", "Delete and re-login")
    else:
        check(False, "", "auth.json NOT found - Login required!",
              "Run: python3 timed_login.py (or double-click 'Login to Properstar.command')")

    check((script_dir / "timed_login.py").exists(), "timed_login.py found", "")
    check((script_dir / "login.sh").exists(), "login.sh found", "")
    check((script_dir / "Login to Properstar.command").exists(),
          "Login to Properstar.command found (double-click to login)", "")

    # ========================================================================
    # 2. DEPENDENCIES
    # ========================================================================
    section("2. Python Dependencies")

    deps = {
        'playwright': 'Browser automation',
        'requests': 'HTTP requests',
        'pandas': 'Data processing',
        'flask': 'API server',
        'flask_cors': 'CORS support',
    }

    for module, desc in deps.items():
        try:
            __import__(module)
            check(True, f"{module} installed - {desc}", "")
        except ImportError:
            check(False, "", f"{module} NOT installed - {desc}",
                  f"Run: pip3 install {module}")

    # ========================================================================
    # 3. CORE SCRIPTS
    # ========================================================================
    section("3. Core Scripts")

    scripts = {
        'favorites_scraper.py': 'Scrapes Properstar favorites',
        'auto_scrape_favorites.py': 'Full pipeline automation',
        'check_availability.py': 'Checks if properties are still listed',
        'geocode_properties.py': 'Geocodes property locations',
        'custom_criteria.py': 'Custom criteria evaluation',
        'criteria_api.py': 'API server',
        'test_update_pipeline.py': 'Testing suite',
        'diagnose_scraping.py': 'Scraping diagnostics',
    }

    for script, desc in scripts.items():
        exists = (script_dir / script).exists()
        check(exists, f"{script} - {desc}", f"{script} missing")

    # ========================================================================
    # 4. HELPER SCRIPTS
    # ========================================================================
    section("4. Helper Scripts & Tools")

    helpers = {
        'start_system.sh': 'Starts API + web server',
        'stop_system.sh': 'Stops all services',
        'system_check.py': 'This system check script',
    }

    for script, desc in helpers.items():
        exists = (script_dir / script).exists()
        is_exec = exists and os.access(script_dir / script, os.X_OK)

        if exists:
            if script.endswith('.sh'):
                check(is_exec, f"{script} - {desc} (executable)",
                      f"{script} - {desc} (not executable)",
                      f"Run: chmod +x {script}", is_warning=True)
            else:
                check(True, f"{script} - {desc}", "")

    # ========================================================================
    # 5. WEB FILES
    # ========================================================================
    section("5. Web Interface Files")

    web_files = {
        'criteria_manager.html': 'Main UI for managing criteria',
        'map_viewer_advanced.html': 'Map viewer with filters',
    }

    for file, desc in web_files.items():
        exists = (script_dir / file).exists()
        check(exists, f"{file} - {desc}", f"{file} missing")

    # ========================================================================
    # 6. DATA FILES
    # ========================================================================
    section("6. Data Files")

    data_files = {
        'enriched_data.json': 'Property data with analysis',
        'extracted_property_urls.csv': 'Scraped property URLs',
    }

    for file, desc in data_files.items():
        filepath = script_dir / file
        if filepath.exists():
            size = filepath.stat().st_size
            if file.endswith('.json'):
                with open(filepath, 'r') as f:
                    data = json.load(f)
                check(True, f"{file} - {len(data)} properties ({size:,} bytes)", "")
            else:
                check(True, f"{file} - ({size:,} bytes)", "")
        else:
            check(False, "", f"{file} missing - {desc}",
                  "Run full pipeline to create", is_warning=True)

    # ========================================================================
    # 7. DOCUMENTATION
    # ========================================================================
    section("7. Documentation")

    docs = [
        'START_HERE.md',
        'QUICK_REFERENCE.md',
        'SESSION_SUMMARY.md',
        'COMPLETE_UX_IMPLEMENTATION.md',
        'EASY_LOGIN_GUIDE.md',
        'TESTING_GUIDE.md',
        'HOW_TO_TEST.md',
    ]

    doc_count = sum(1 for doc in docs if (script_dir / doc).exists())
    check(doc_count >= 5, f"{doc_count}/{len(docs)} documentation files found",
          f"Only {doc_count}/{len(docs)} docs found")

    # ========================================================================
    # 8. SERVICES STATUS
    # ========================================================================
    section("8. Running Services")

    # Check API server
    try:
        import requests
        response = requests.get('http://localhost:5001/api/system-status', timeout=2)
        if response.ok:
            data = response.json()
            check(True, f"API server running on port 5001", "")

            # Check data freshness
            if data.get('data_freshness'):
                hours = data.get('data_age_hours', 0)
                if hours < 24:
                    check(True, f"Data is fresh ({hours:.1f} hours old)", "")
                else:
                    check(False, "", f"Data is stale ({hours/24:.1f} days old)",
                          "Run full update", is_warning=True)
        else:
            check(False, "", "API server not responding",
                  "Run: python3 criteria_api.py")
    except:
        check(False, "", "API server not running",
              "Run: ./start_system.sh or python3 criteria_api.py")

    # Check web server
    try:
        import requests
        response = requests.get('http://localhost:8000', timeout=2)
        check(response.ok, "Web server running on port 8000",
              "Web server not running", "Run: ./start_system.sh")
    except:
        check(False, "", "Web server not running",
              "Run: ./start_system.sh or python3 -m http.server 8000")

    # ========================================================================
    # 9. SCRIPT INTEGRITY
    # ========================================================================
    section("9. Script Import Check")

    test_imports = [
        ('favorites_scraper', 'Scraper'),
        ('check_availability', 'Availability checker'),
        ('custom_criteria', 'Criteria evaluator'),
    ]

    sys.path.insert(0, str(script_dir))
    for module, desc in test_imports:
        try:
            __import__(module)
            check(True, f"{module} imports successfully - {desc}", "")
        except Exception as e:
            check(False, "", f"{module} import fails - {desc}: {str(e)[:50]}",
                  "Check for syntax errors")

    # ========================================================================
    # 10. KNOWN ISSUES
    # ========================================================================
    section("10. Known Issues Check")

    # Check for Airbnb criterion bug
    try:
        with open(script_dir / 'custom_criteria.py', 'r') as f:
            content = f.read()
            if 'if building_size > 200:' in content:
                check(False, "", "Bug found in Airbnb criterion (building_size can be None)",
                      "Needs fix: Add null check before comparison", is_warning=True)
            else:
                check(True, "Airbnb criterion looks OK", "")
    except:
        pass

    # ========================================================================
    # SUMMARY
    # ========================================================================
    section("SUMMARY")

    print(f"{Colors.BOLD}Issues Found:{Colors.END} {len(issues)}")
    print(f"{Colors.BOLD}Warnings:{Colors.END} {len(warnings)}")

    if not issues and not warnings:
        print(f"\n{Colors.GREEN}{Colors.BOLD}🎉 PERFECT! Everything is working!{Colors.END}\n")
        print(f"{Colors.BOLD}Your system is ready to use:{Colors.END}")
        print(f"  • Open: http://localhost:8000/criteria_manager.html")
        print(f"  • Click: 'Full Update' button")
        print(f"  • Or run: python3 auto_scrape_favorites.py now")
    elif not issues:
        print(f"\n{Colors.YELLOW}{Colors.BOLD}⚠️  System works but has {len(warnings)} warnings{Colors.END}\n")
        print(f"{Colors.BOLD}Warnings:{Colors.END}")
        for i, (msg, fix) in enumerate(warnings, 1):
            print(f"  {i}. {msg}")
            if fix:
                print(f"     💡 {fix}")
    else:
        print(f"\n{Colors.RED}{Colors.BOLD}❌ Found {len(issues)} critical issues{Colors.END}\n")
        print(f"{Colors.BOLD}Critical Issues:{Colors.END}")
        for i, (msg, fix) in enumerate(issues, 1):
            print(f"  {i}. {msg}")
            if fix:
                print(f"     💡 {fix}")

        if warnings:
            print(f"\n{Colors.BOLD}Warnings:{Colors.END}")
            for i, (msg, fix) in enumerate(warnings, 1):
                print(f"  {i}. {msg}")
                if fix:
                    print(f"     💡 {fix}")

    # ========================================================================
    # NEXT STEPS
    # ========================================================================
    if issues or warnings:
        print(f"\n{Colors.BOLD}📋 RECOMMENDED NEXT STEPS:{Colors.END}\n")

        if not has_auth:
            print(f"  1. {Colors.GREEN}Login to Properstar{Colors.END} (Most Important!)")
            print(f"     Double-click: 'Login to Properstar.command'")
            print(f"     Or run: python3 timed_login.py\n")

        if any('API server not running' in msg for msg, _ in issues):
            print(f"  2. {Colors.GREEN}Start the system{Colors.END}")
            print(f"     Run: ./start_system.sh\n")

        if any('building_size' in msg for msg, _ in issues + warnings):
            print(f"  3. {Colors.YELLOW}Fix Airbnb criterion bug{Colors.END}")
            print(f"     I can fix this automatically\n")

        print(f"  4. Run this check again: python3 system_check.py")

    print()

if __name__ == "__main__":
    main()
