#!/usr/bin/env python3
import json
from pathlib import Path

project = Path('/home/alina/aNavigator')
tile_dir = project / 'src/realistic_roads_bologna'
manifest_path = tile_dir / 'manifest.json'
manifest = json.loads(manifest_path.read_text(encoding='utf-8'))

west, south, east, north = manifest['coverage']
assert west <= 11.18 and east >= 11.52, manifest['coverage']
assert south <= 44.37 and north >= 44.62, manifest['coverage']
assert len(manifest['tiles']) >= 80, len(manifest['tiles'])
assert manifest['bytes'] < 180_000_000, manifest['bytes']

layers = {}
features = total_bytes = 0
for entry in manifest['tiles']:
    path = tile_dir / entry['file']
    assert path.is_file(), path
    assert path.stat().st_size == entry['bytes'], entry
    assert path.stat().st_size < 8_000_000, (path.name, path.stat().st_size)
    data = json.loads(path.read_text(encoding='utf-8'))
    for feature in data.get('features', []):
        features += 1
        props = feature.get('properties') or {}
        layer = props.get('layer')
        layers[layer] = layers.get(layer, 0) + 1
        geometry = feature.get('geometry') or {}
        assert geometry.get('type') in {'Polygon', 'MultiPolygon'}, geometry.get('type')
        if layer == 'road_marking':
            assert props.get('confidence') in {
                'verified_marking', 'verified_lanes_standard_marking', 'verified_osm_control'
            }, props
            assert props.get('osm_way_ids'), props
    total_bytes += path.stat().st_size

assert features == manifest['features'], (features, manifest['features'])
assert total_bytes == manifest['bytes'], (total_bytes, manifest['bytes'])
for required in ('asphalt', 'intersection', 'road_marking', 'crosswalk'):
    assert layers.get(required, 0) > 0, (required, layers)

html = (project / 'src/map.html').read_text(encoding='utf-8')
assert "realistic_roads_bologna/manifest.json" in html
assert "addOsm2StreetsTile" in html
assert "_osm2StreetsWanted" in html
assert "remaining = stableProgress.remaining" not in html

build = (project / 'build_ipa.sh').read_text(encoding='utf-8')
assert 'cp -R "$SRC_DIR/realistic_roads_bologna"' in build
print(f"PASS tiles={len(manifest['tiles'])} features={features} bytes={total_bytes} layers={layers}")
