"""
Street View Imagery for Cities: hands-on lab, Part B (run on your own laptop).
Downloads open street view images around Singapore cafes with ZenSVI, segments them,
scores them for perception, and joins the results to the review comfort lens.

Setup (once):
    pip install zensvi            # brings PyTorch; a GPU helps but is not needed for ~300 images
    Make a free Mapillary token: mapillary.com/dashboard/developers -> Register application
    Save it yourself in a text file:  ~/.mapillary_token   (never paste tokens into slides or chats)
Needs sg_places.csv and sg_reviews.csv from the reviews guide (class sample) in this folder.
Run:  python zensvi_lab.py      (first run downloads model weights; expect 10-30 minutes on a laptop)
"""
from pathlib import Path
import numpy as np
import pandas as pd

TOKEN = (Path.home() / '.mapillary_token').read_text().strip()
OUT = Path('svi_sg')
N_PLACES, BUFFER_M = 30, 100

# ---------- Step 1: choose places and download images around them ----------
places = pd.read_csv('sg_places.csv')
pick = places[places.category.isin(['Cafe', 'Coffee shop'])].sample(N_PLACES, random_state=0)
pick.rename(columns={'lat': 'latitude', 'lon': 'longitude'})[['place_id', 'name', 'latitude', 'longitude']].to_csv('pick.csv', index=False)

from zensvi.download import MLYDownloader
mly = MLYDownloader(mly_api_key=TOKEN)
mly.download_svi(str(OUT), input_csv_file='pick.csv', buffer=BUFFER_M, resolution=1024)
pids = pd.read_csv(OUT / 'mly_pids.csv')          # id, captured_at, compass_angle, is_pano, lon, lat ...
batches = sorted((OUT / 'mly_svi').glob('batch_*'))
print(f'images found: {len(pids)} in {len(batches)} batch folder(s)')

# ---------- Step 2: semantic segmentation (what share of each image is tree, sky, road...) ----------
from zensvi.cv import Segmenter
seg = Segmenter(dataset='mapillary', task='semantic')
for b in batches:
    seg.segment(b, dir_image_output=OUT / 'seg_images' / b.name, dir_summary_output=OUT / 'seg_summary' / b.name,
                csv_format='wide', save_format='csv', save_image_options='blend_image')
ratios = pd.concat(pd.read_csv(f) for f in (OUT / 'seg_summary').glob('*/pixel_ratios.csv'))
ratios['id'] = ratios.filename_key.astype(str)
green_cols = [c for c in ratios.columns if c.lower() in ('vegetation', 'terrain')]
ratios['green_view_index'] = ratios[green_cols].sum(axis=1)

# ---------- Step 3: perception scores from a model trained on Place Pulse votes ----------
from zensvi.cv import ClassifierPerception
scores = []
for study in ['more beautiful', 'safer']:
    clf = ClassifierPerception(perception_study=study)
    for b in batches:
        d = OUT / 'perception' / study.replace(' ', '_') / b.name
        clf.classify(b, dir_summary_output=d, save_format='csv')
        scores.append(pd.read_csv(d / 'results.csv').set_index('filename_key'))
per = pd.concat(scores).groupby(level=0).first().reset_index()
per['id'] = per.filename_key.astype(str)

# ---------- Step 4: join images -> places -> reviews ----------
img = pids.assign(id=pids.id.astype(str)).merge(ratios[['id', 'green_view_index']], on='id').merge(per.drop(columns='filename_key'), on='id')
rows = []
for p in pick.itertuples():
    dist = np.hypot((img.lat - p.lat) * 111000, (img.lon - p.lon) * 111000 * np.cos(np.radians(1.35)))
    near = img[dist <= BUFFER_M]
    rows.append({'place_id': p.place_id, 'name': p.name, 'n_img': len(near), 'green': near.green_view_index.mean(),
                 'beautiful': near['more beautiful'].mean(), 'safe': near['safer'].mean()})
street = pd.DataFrame(rows)

rev = pd.read_csv('sg_reviews.csv')
low = rev.text.str.lower()
rev['quiet'] = low.str.contains(r"\b(?:quiet|peaceful|calm|serene|tranquil)")
rev['heat'] = low.str.contains(r"\b(?:stuffy|humid|sweat\w*|too warm|hot inside|no air ?-?con\w*|poor ventilation)")
g = rev.groupby('place_id').agg(n=('text', 'size'), quiet=('quiet', 'mean'), heat=('heat', 'mean'), atm=('atmosphere', 'mean'))
res = street.join(g, on='place_id').dropna(subset=['green'])
res.to_csv(OUT / 'places_street_and_reviews.csv', index=False)
print(res.sort_values('green').round(2).to_string(index=False))
print(f'correlation green vs atmosphere: {res.green.corr(res.atm):.2f} over {len(res)} places (small n: treat as a pilot)')
