import json
from pathlib import Path

BASE = Path("/home/bowerybay/public_html/wilgus-automation")
SOURCE = BASE / "wilgus-property-amenities.json"
OUTPUT = BASE / "wilgus-property-attributes.json"

with SOURCE.open() as f:
    properties = json.load(f)

RULES = {
    "dog_friendly": [
        "Dog Friendly",
    ],
    "pets_considered": [
        "pets considered",
    ],
    "ocean_front": [
        "Ocean Front",
        "Oceanfront",
        "Beachfront",
    ],
    "ocean_view": [
        "Ocean",
        "Ocean/Beach",
        "Beach View",
        "Ocean view",
        "Ocean View",
    ],
    "waterfront": [
        "Bay Front/Water Views/Canal",
        "Water Front-Pond/Creek/Bay/Canal",
        "Waterfront",
        "Water front (Lake)",
    ],
    "pool_access": [
        "Community Pool",
        "Kiddie Pool",
        "Pool",
        "Communal Pool",
        "Pool - Heated/Private",
        "Pool - Private",
    ],
    "private_pool": [
        "Pool - Heated/Private",
        "Pool - Private",
    ],
    "elevator": [
        "Elevator",
    ],
    "walk_to_beach": [
        "Walk to beach",
    ],
    "walk_to_bethany": [
        "Close to Town (Bethany-walking)",
    ],
    "near_beach_east_rt1": [
        "Near Beach/East of Rt 1",
    ],
    "first_floor_bed_bath": [
        "1st floor Bed/Bath",
    ],
    "screened_porch": [
        "Screened Porch",
    ],
    "balcony": [
        "Balcony",
    ],
    "hot_tub": [
        "Hot Tub-Community",
        "Hot Tub-Private",
        "Hot Tub",
    ],
    "tennis": [
        "Community Tennis",
        "Tennis",
        "Tennis Courts",
    ],
    "pickleball": [
        "Pickle Ball - Community",
    ],
    "house": [
        "House",
    ],
    "condo": [
        "Condo",
    ],
    "townhouse_duplex": [
        "Town House/Duplex",
    ],
}

def all_amenities(prop):
    return {
        item
        for items in prop.get("amenities", {}).values()
        for item in items
    }

def make_attribute(amenities, terms):
    matched = sorted(term for term in terms if term in amenities)
    return {
        "value": bool(matched),
        "matched": matched,
    }

result = {}

for property_id, prop in properties.items():
    amenities = all_amenities(prop)

    attributes = {
        name: make_attribute(amenities, terms)
        for name, terms in RULES.items()
    }

    # Broader pet flag, kept separate from explicit Dog Friendly.
    pet_matches = sorted(
        set(attributes["dog_friendly"]["matched"])
        | set(attributes["pets_considered"]["matched"])
    )

    attributes["pet_possible"] = {
        "value": bool(pet_matches),
        "matched": pet_matches,
    }

    result[property_id] = {
        "id": prop.get("id", property_id),
        "address": prop.get("address"),
        "city": prop.get("city"),
        "url": prop.get("url"),
        "attributes": attributes,
    }

with OUTPUT.open("w") as f:
    json.dump(result, f, indent=2)

print(f"Properties: {len(result)}")
print(f"Saved: {OUTPUT}")

for name in list(RULES) + ["pet_possible"]:
    count = sum(
        p["attributes"][name]["value"]
        for p in result.values()
    )
    print(f"{name:24} {count:3}  {count / len(result):6.1%}")