#!/usr/bin/env python3
"""Select top FarmMatch candidates for Paradiso Session 3."""
import json

with open("enriched_data.json") as f:
    data = json.load(f)

results = []
for p in data:
    if p.get("status") != "Active":
        continue
    c = p.get("criteria", {})
    guest = c.get("guest_accommodation", 0)
    loc = c.get("location", 0)
    garden = c.get("market_garden", 0)
    workshop = c.get("workshop", 0)
    market = c.get("local_market", 0)

    weighted = (guest * 3.0 + loc * 3.0 + garden * 2.5 + workshop * 2.0 + market * 1.0) / 11.5

    risk = p.get("risk_profile", "")
    if risk == "Hoog":
        weighted *= 0.7
    elif risk == "Gemiddeld":
        weighted *= 0.9

    results.append({
        "weighted": round(weighted, 2),
        "title": (p.get("title") or "")[:80],
        "price": p.get("price"),
        "location": p.get("location"),
        "bedrooms": p.get("bedrooms"),
        "land_m2": p.get("land_size_m2"),
        "building_m2": p.get("building_size_m2"),
        "risk": risk,
        "guest": guest,
        "loc": loc,
        "garden": garden,
        "workshop": workshop,
        "market": market,
        "url": p.get("url"),
        "summary": (p.get("summary") or "")[:250],
        "analysis": (p.get("analysis") or "")[:300],
    })

results.sort(key=lambda x: -x["weighted"])

print(f"Total active properties: {len(results)}")
print(f"\n{'='*80}")
print("TOP 20 CANDIDATES (by weighted score)")
print(f"{'='*80}")

for i, r in enumerate(results[:20]):
    print(f"\n--- #{i+1} (score: {r['weighted']}) ---")
    print(f"Title: {r['title']}")
    print(f"Location: {r['location']} | Price: {r['price']} | Beds: {r['bedrooms']}")
    print(f"Land: {r['land_m2']}m2 | Building: {r['building_m2']}m2")
    print(f"Risk: {r['risk']} | Guest:{r['guest']} Loc:{r['loc']} Garden:{r['garden']} Workshop:{r['workshop']} Market:{r['market']}")
    print(f"URL: {r['url']}")
    print(f"Summary: {r['summary']}")
    print()
