#!/usr/bin/env python3
"""
Fully automated workflow:
1. Sync favorites from Properstar
2. Analyze new properties with GPT
3. Parse individual criteria
4. Unfavorite low-scoring properties based on config

Run this script weekly/monthly to keep your favorites clean!
"""
import asyncio
import json
import os
import sys
import subprocess
from datetime import datetime

CONFIG_FILE = "config.json"

def load_config():
    """Load configuration from config.json"""
    if not os.path.exists(CONFIG_FILE):
        print(f"❌ {CONFIG_FILE} not found. Creating default config...")
        default_config = {
            "unfavorite_thresholds": {
                "overall_score": 0.0,
                "market_garden": 3,
                "guest_accommodation": 3,
                "workshop": 2,
                "rental_units": 3,
                "location": 3,
                "local_market": 3,
                "max_risk": "Gemiddeld",
                "require_all": False
            },
            "openai_model": "gpt-3.5-turbo-1106",
            "auto_unfavorite": True,
            "dry_run": False
        }
        with open(CONFIG_FILE, 'w') as f:
            json.dump(default_config, f, indent=2)
        print(f"✅ Created {CONFIG_FILE}")
        return default_config

    with open(CONFIG_FILE, 'r') as f:
        return json.load(f)

def log(message):
    """Log with timestamp"""
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(f"[{timestamp}] {message}")

def run_command(command, description):
    """Run a shell command and return success status"""
    log(f"▶️  {description}")
    try:
        result = subprocess.run(
            command,
            shell=True,
            check=True,
            capture_output=False,
            text=True
        )
        log(f"✅ {description} - Success")
        return True
    except subprocess.CalledProcessError as e:
        log(f"❌ {description} - Failed with exit code {e.returncode}")
        return False

async def main():
    """Main automated workflow"""
    config = load_config()

    print("=" * 70)
    print("🌿 FARM MATCH - AUTOMATED WORKFLOW")
    print("=" * 70)
    print()

    log("Starting automated property evaluation and cleanup...")
    print()

    # Show configuration
    print("📋 Configuration:")
    print(f"   Auto unfavorite: {config['auto_unfavorite']}")
    print(f"   Dry run: {config['dry_run']}")
    print(f"   Thresholds:")
    for key, value in config['unfavorite_thresholds'].items():
        print(f"      {key}: {value}")
    print()

    if not config['auto_unfavorite']:
        print("⚠️  Auto unfavorite is disabled in config.json")
        print("   Only syncing and analyzing will be performed.")
        print()

    # Confirm to proceed
    if not config['dry_run'] and config['auto_unfavorite']:
        print("⚠️  WARNING: This will actually unfavorite properties!")
        print("   Set 'dry_run': true in config.json to preview first.")
        print()
        confirm = input("Type 'YES' to proceed with real unfavoriting: ")
        if confirm != "YES":
            print("❌ Cancelled")
            return
        print()

    # Step 1: Sync favorites from Properstar
    print("=" * 70)
    log("STEP 1: Syncing favorites from Properstar")
    print("=" * 70)

    # Import and run sync
    try:
        from sync_favorites import sync_and_analyze

        # Temporarily override the interactive behavior
        import builtins
        original_input = builtins.input

        # Auto-answer 'n' to analysis prompt (we'll do it in next step)
        def mock_input(prompt):
            if "analyze new properties now" in prompt.lower():
                log("Auto-skipping immediate analysis (will do in next step)")
                return 'n'
            return original_input(prompt)

        builtins.input = mock_input

        sync_result = await sync_and_analyze()

        builtins.input = original_input

        new_count = len(sync_result.get('new', []))
        removed_count = len(sync_result.get('removed', []))

        log(f"Sync complete: {new_count} new, {removed_count} removed")

    except Exception as e:
        log(f"❌ Sync failed: {e}")
        return

    print()

    # Step 2: Analyze properties (if there are any new ones or first run)
    print("=" * 70)
    log("STEP 2: Analyzing properties with GPT")
    print("=" * 70)

    if not run_command(
        "/usr/bin/python3 analyze_from_urls.py",
        "GPT analysis"
    ):
        log("⚠️  Analysis failed, but continuing...")

    print()

    # Step 3: Parse individual criteria
    print("=" * 70)
    log("STEP 3: Parsing individual criteria scores")
    print("=" * 70)

    if not run_command(
        "/usr/bin/python3 parse_criteria.py",
        "Criteria parsing"
    ):
        log("❌ Criteria parsing failed, cannot continue")
        return

    # Lightweight quality gate before destructive steps
    try:
        from quality_gate import run_quality_gate

        gate_report = run_quality_gate()
        print("\n📊 Quality gate results:")
        for key, value in gate_report.get("stats", {}).items():
            print(f"   {key}: {value}")

        if gate_report.get("errors"):
            log("❌ Quality gate failed - aborting before unfavorite for safety")
            for err in gate_report["errors"]:
                print(f"   - {err}")
            return

        if gate_report.get("warnings"):
            log("⚠️  Quality gate warnings detected")
            for warn in gate_report["warnings"][:5]:
                print(f"   - {warn}")
            if len(gate_report["warnings"]) > 5:
                print(f"   ... and {len(gate_report['warnings']) - 5} more")
    except Exception as e:
        log(f"⚠️  Quality gate check failed unexpectedly: {e}")

    print()

    # Step 4: Unfavorite low-scoring properties
    if config['auto_unfavorite']:
        print("=" * 70)
        log("STEP 4: Unfavoriting low-scoring properties")
        print("=" * 70)

        thresholds = config['unfavorite_thresholds']

        # Build command
        cmd_parts = ["/usr/bin/python3 smart_unfavorite.py"]
        cmd_parts.append(f"--overall-score {thresholds['overall_score']}")
        cmd_parts.append(f"--market-garden {thresholds['market_garden']}")
        cmd_parts.append(f"--guest {thresholds['guest_accommodation']}")
        cmd_parts.append(f"--workshop {thresholds['workshop']}")
        cmd_parts.append(f"--rental {thresholds['rental_units']}")
        cmd_parts.append(f"--location {thresholds['location']}")
        cmd_parts.append(f"--market {thresholds['local_market']}")
        cmd_parts.append(f"--max-risk {thresholds['max_risk']}")

        if thresholds.get('require_all', False):
            cmd_parts.append("--require-all")

        if config['dry_run']:
            cmd_parts.append("--dry-run")
            log("Running in DRY RUN mode (no actual unfavoriting)")

        command = " ".join(cmd_parts)

        # For non-dry-run, we need to handle the confirmation
        if not config['dry_run']:
            log("⚠️  Will attempt to unfavorite (confirmation required)")
            # Note: smart_unfavorite.py will still ask for confirmation
            # User needs to type UNFAVORITE when prompted

        os.system(command)

        print()

    # Step 5: Final report
    print("=" * 70)
    log("WORKFLOW COMPLETE")
    print("=" * 70)
    print()

    # Show summary
    print("📊 Summary:")
    print(f"   ✅ Favorites synced")
    print(f"   ✅ Properties analyzed with GPT")
    print(f"   ✅ Criteria parsed")
    if config['auto_unfavorite']:
        if config['dry_run']:
            print(f"   ✅ Unfavoriting preview completed (dry run)")
        else:
            print(f"   ✅ Unfavoriting executed")
    print()

    # Next steps
    print("💡 Next steps:")
    print("   1. Review results in web interface:")
    print("      ./view_map.sh")
    print("      Open: http://localhost:8000/map_viewer_advanced.html")
    print()
    print("   2. Adjust thresholds in config.json if needed")
    print()
    print("   3. Run this script again weekly to keep favorites clean")
    print()

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\n\n⚠️  Workflow interrupted by user")
        sys.exit(1)
    except Exception as e:
        print(f"\n\n❌ Fatal error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)
