"""Read-only NYC .3dm census, run in Rhino 8 CPython. No active-document edits."""
import collections
import csv
import gc
import hashlib
import json
import math
import os
import random
import threading
import time
import traceback

import Rhino
import Rhino.Geometry as G
import System

SOURCE = os.path.join(os.path.expanduser("~"), "Downloads", "3D_NYC")
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "research-output", "evidence")
SAMPLE_PER_LAYER = 64
STATE = {"status": "not_started"}


def layer_role(path):
    leaf = path.split("::")[-1].lower().replace(" ", "").replace("_", "")
    if leaf == "buildingfootprint":
        return "footprint_curve"
    if leaf in ("rooftopsurface", "surfacerooftop", "buildingrooftopsurface"):
        return "roof_surface"
    if leaf in ("footprintsurface", "surfacefootprint", "buildingfootprintsurface"):
        return "footprint_surface"
    if leaf in ("facadesurface", "surfacefacade", "buildingfacadesurface"):
        return "facade_surface"
    return "context_or_other"


def save_json(path, value):
    with open(path + ".tmp", "w", encoding="utf-8") as f:
        json.dump(value, f, indent=2, allow_nan=False)
    os.replace(path + ".tmp", path)


def progress(**values):
    STATE.update(values)
    save_json(os.path.join(OUT, "progress.json"), STATE)


def summary(values):
    a = sorted(v for v in values if v is not None and math.isfinite(v))
    if not a:
        return {"n": 0}
    def q(p):
        x = (len(a) - 1) * p
        i = int(x)
        return a[i] + (a[min(i + 1, len(a) - 1)] - a[i]) * (x - i)
    return dict(n=len(a), min=a[0], p10=q(.1), p25=q(.25), median=q(.5),
                p75=q(.75), p90=q(.9), p99=q(.99), max=a[-1])


def polygon_metrics(points):
    # Shift before shoelace arithmetic: source XY values can exceed 300 million mm.
    ox, oy = points[0]
    p = [(x - ox, y - oy) for x, y in points]
    cross = sum(a[0] * b[1] - b[0] * a[1] for a, b in zip(p, p[1:] + p[:1]))
    area = abs(cross) / 2
    edges = [(b[0] - a[0], b[1] - a[1]) for a, b in zip(p, p[1:] + p[:1])]
    lengths = [math.hypot(x, y) for x, y in edges]
    perimeter = sum(lengths)
    longest = max(range(len(edges)), key=lambda i: lengths[i])
    angle = math.degrees(math.atan2(edges[longest][1], edges[longest][0])) % 180
    return area, perimeter, angle


def aligned_shape(points, angle):
    """Longest-edge aligned extents, not a minimum-area bounding rectangle."""
    ox, oy = points[0]
    c, s = math.cos(math.radians(angle)), math.sin(math.radians(angle))
    u = [(x - ox) * c + (y - oy) * s for x, y in points]
    v = [-(x - ox) * s + (y - oy) * c for x, y in points]
    return max(u) - min(u), max(v) - min(v)


def inside(x, y, points):
    hit = False
    for (ax, ay), (bx, by) in zip(points, points[1:] + points[:1]):
        if (ay > y) != (by > y) and x < (bx - ax) * (y - ay) / (by - ay) + ax:
            hit = not hit
    return hit


def test_helpers():
    rect = [(300000000., 70000000.), (300010000., 70000000.),
            (300010000., 70020000.), (300000000., 70020000.)]
    a, p, angle = polygon_metrics(rect)
    assert a == 200000000 and p == 60000 and angle == 90
    assert all(abs(a - b) < 1e-8 for a, b in zip(sorted(aligned_shape(rect, angle)), [10000, 20000]))
    assert inside(300005000, 70010000, rect)
    assert not inside(299999999, 70010000, rect)
    concave = [(0, 0), (4, 0), (4, 1), (1, 1), (1, 4), (0, 4)]
    assert polygon_metrics(concave)[0] == 7
    assert inside(.5, 3, concave) and not inside(3, 3, concave)
    assert summary([1, 2, 3, 4])["median"] == 2.5


def vec(p):
    return [p.X, p.Y, p.Z]


def inspect_file(path, index, total):
    filename = os.path.basename(path)
    before = os.stat(path)
    started = time.time()
    progress(current=filename, phase="reading", index=index, total=total)
    model = Rhino.FileIO.File3dm.Read(path)
    if model is None:
        raise RuntimeError("File3dm.Read returned None")
    read_seconds = time.time() - started
    try:
        unit_to_m = Rhino.RhinoMath.UnitScale(model.Settings.ModelUnitSystem, Rhino.UnitSystem.Meters)
        layers = {l.Index: l.FullPath for l in model.AllLayers}
        counts = collections.Counter()
        type_counts = collections.Counter()
        layer_boxes = {}
        samples = collections.defaultdict(list)
        seen = collections.Counter()
        named = collections.Counter()
        modes = collections.Counter()
        rng = random.Random(42)
        footprints = []
        roofs = []
        invalid_bounds = 0
        sample_attrs = []
        fp_exclusions = collections.Counter()
        context_ids = []
        n = 0
        csv_path = os.path.join(OUT, filename[:-4] + "-footprints.csv")
        with open(csv_path, "w", newline="", encoding="utf-8") as fp_csv:
            writer = csv.writer(fp_csv)
            writer.writerow(["object_guid", "layer", "area_m2", "perimeter_m", "bbox_x_m", "bbox_y_m",
                             "base_z_m", "vertices", "compactness", "bbox_fill_ratio",
                             "longest_edge_azimuth_deg", "x_source_units", "y_source_units",
                             "aligned_long_m", "aligned_short_m", "aligned_fill_ratio", "aligned_aspect_ratio"])
            for obj in model.Objects:
                g, attrs = obj.Geometry, obj.Attributes
                li = attrs.LayerIndex
                layer = layers.get(li, "<missing layer>")
                typ = g.GetType().Name
                role = layer_role(layer)
                counts[(li, typ)] += 1
                type_counts[typ] += 1
                seen[li] += 1
                n += 1
                if layer.startswith("Community District"):
                    context_ids.append(str(attrs.ObjectId))
                if attrs.Name:
                    named[layer] += 1
                modes[str(attrs.Mode)] += 1
                # Uniform reservoir within each layer; keep references only while model lives.
                group = samples[li]
                pos = len(group) if len(group) < SAMPLE_PER_LAYER else rng.randrange(seen[li])
                if pos < SAMPLE_PER_LAYER:
                    if pos == len(group):
                        group.append(obj)
                    else:
                        group[pos] = obj
                bb = g.GetBoundingBox(False)
                if not bb.IsValid:
                    invalid_bounds += 1
                    continue
                if li not in layer_boxes:
                    layer_boxes[li] = G.BoundingBox(bb.Min, bb.Max)
                else:
                    b = layer_boxes[li]
                    b.Union(bb)
                    layer_boxes[li] = b
                if len(sample_attrs) < 12 and layer.startswith("Buildings"):
                    us = attrs.GetUserStrings()
                    sample_attrs.append({"guid": str(attrs.ObjectId), "name": attrs.Name,
                                         "layer": layer, "user_strings": {k: attrs.GetUserString(k) for k in us.AllKeys}})
                if role == "roof_surface" and isinstance(g, G.Brep):
                    roofs.append((bb.Min.X, bb.Min.Y, bb.Max.X, bb.Max.Y, bb.Min.Z, bb.Max.Z, str(attrs.ObjectId)))
                if role == "footprint_curve" and isinstance(g, G.Curve):
                    if not g.IsClosed:
                        fp_exclusions["open_curve"] += 1
                        continue
                    ok, poly = g.TryGetPolyline()
                    if not ok:
                        fp_exclusions["not_polyline"] += 1
                        continue
                    pts = [(p.X, p.Y) for p in poly]
                    if pts[0] == pts[-1]:
                        pts.pop()
                    if len(pts) < 3:
                        fp_exclusions["fewer_than_3_vertices"] += 1
                        continue
                    if bb.Max.Z - bb.Min.Z > max(model.Settings.ModelAbsoluteTolerance * 10, 1 / unit_to_m * .001):
                        fp_exclusions["nonhorizontal_over_1mm"] += 1
                        continue
                    area, perimeter, angle = polygon_metrics(pts)
                    dx, dy = bb.Max.X - bb.Min.X, bb.Max.Y - bb.Min.Y
                    if area <= 0 or dx <= 0 or dy <= 0:
                        fp_exclusions["zero_area_or_extent"] += 1
                        continue
                    compact = 4 * math.pi * area / perimeter ** 2
                    au, av = aligned_shape(pts, angle)
                    item = {"id": str(attrs.ObjectId), "points": pts,
                            "bbox": [bb.Min.X, bb.Min.Y, bb.Max.X, bb.Max.Y], "z": bb.Min.Z,
                            "area": area * unit_to_m ** 2, "perimeter": perimeter * unit_to_m,
                            "width": dx * unit_to_m, "depth": dy * unit_to_m,
                            "vertices": len(pts), "compactness": compact, "fill": area / (dx * dy),
                            "azimuth": angle, "height": None, "roof_matches": 0,
                            "aligned_long": max(au, av) * unit_to_m,
                            "aligned_short": min(au, av) * unit_to_m,
                            "aligned_fill": area / (au * av), "aligned_aspect": max(au, av) / min(au, av)}
                    footprints.append(item)
                    writer.writerow([item["id"], layer, item["area"], item["perimeter"], item["width"],
                                     item["depth"], item["z"] * unit_to_m, len(pts), compact, item["fill"],
                                     angle, bb.Center.X, bb.Center.Y, item["aligned_long"], item["aligned_short"],
                                     item["aligned_fill"], item["aligned_aspect"]])
                if n % 25000 == 0:
                    progress(phase="object census", objects_processed=n, elapsed_s=round(time.time() - started, 1))
        progress(phase="sample validation + roof matching", objects_processed=n)
        layer_results = []
        for li, layer in sorted(layers.items()):
            sample = samples[li]
            validity = collections.Counter()
            elevations = []
            examples = []
            faces = []
            naked = []
            areas = []
            curve_lengths = []
            invalid_logs = []
            normal_angles = []
            for obj in sample:
                g = obj.Geometry
                valid = g.IsValid
                validity["valid" if valid else "invalid"] += 1
                if not valid and len(invalid_logs) < 5:
                    try:
                        log = str(g.IsValidWithLog())
                    except Exception as exc:
                        log = str(exc)
                    invalid_logs.append({"guid": str(obj.Attributes.ObjectId), "log": log})
                bb = g.GetBoundingBox(True)
                if bb.IsValid:
                    elevations.append((bb.Max.Z - bb.Min.Z) * unit_to_m)
                attrs = obj.Attributes
                us = attrs.GetUserStrings()
                if isinstance(g, G.Brep):
                    validity["solid" if g.IsSolid else "not_solid"] += 1
                    faces.append(g.Faces.Count)
                    naked.append(sum(1 for e in g.Edges if e.Valence == G.EdgeAdjacency.Naked))
                    if g.Faces.Count:
                        face = g.Faces[0]
                        normal = face.NormalAt(face.Domain(0).Mid, face.Domain(1).Mid)
                        if normal.IsValid and normal.Length > 0:
                            normal_angles.append(math.degrees(math.acos(min(1, abs(normal.Z) / normal.Length))))
                    # Area measurements are sampled and never silently treated as population totals.
                    am = G.AreaMassProperties.Compute(g)
                    if am:
                        areas.append(am.Area * unit_to_m ** 2)
                        am.Dispose()
                elif isinstance(g, G.Curve):
                    validity["closed_curve" if g.IsClosed else "open_curve"] += 1
                    curve_lengths.append(g.GetLength() * unit_to_m)
                elif isinstance(g, G.Mesh):
                    validity["closed_mesh" if g.IsClosed else "open_mesh"] += 1
                if len(examples) < 3:
                    examples.append({"guid": str(attrs.ObjectId), "type": g.GetType().Name,
                                     "valid": valid, "name": attrs.Name,
                                     "bbox": {"min": vec(bb.Min), "max": vec(bb.Max)} if bb.IsValid else None,
                                     "user_strings": {k: attrs.GetUserString(k) for k in us.AllKeys}})
            bb = layer_boxes.get(li)
            layer_results.append({"index": li, "path": layer, "role": layer_role(layer), "count": seen[li],
                                  "types": {t: v for (j, t), v in counts.items() if j == li},
                                  "bounds_source_units": {"min": vec(bb.Min), "max": vec(bb.Max)} if bb else None,
                                  "sample_n": len(sample), "sample_checks": dict(validity), "invalid_sample_logs": invalid_logs,
                                  "sample_z_extent_m": summary(elevations), "sample_face_counts": summary(faces),
                                  "sample_naked_edges": summary(naked), "sample_area_m2": summary(areas),
                                  "sample_first_face_normal_angle_from_vertical_deg": summary(normal_angles),
                                  "sample_curve_length_m": summary(curve_lengths), "examples": examples})
        # Conservative spatial association, not a building-ID join. Ambiguous roof matches excluded.
        cell = 100 / unit_to_m
        grid = collections.defaultdict(list)
        for i, fp in enumerate(footprints):
            x0, y0, x1, y1 = fp["bbox"]
            for ix in range(math.floor(x0 / cell), math.floor(x1 / cell) + 1):
                for iy in range(math.floor(y0 / cell), math.floor(y1 / cell) + 1):
                    grid[(ix, iy)].append(i)
        match_stats = collections.Counter()
        tol = .10 / unit_to_m
        for rx0, ry0, rx1, ry1, rz0, rz1, rid in roofs:
            cx, cy = (rx0 + rx1) / 2, (ry0 + ry1) / 2
            candidates = []
            for i in grid.get((math.floor(cx / cell), math.floor(cy / cell)), []):
                fp = footprints[i]
                x0, y0, x1, y1 = fp["bbox"]
                if rx0 < x0 - tol or ry0 < y0 - tol or rx1 > x1 + tol or ry1 > y1 + tol:
                    continue
                if rz1 <= fp["z"] or not inside(cx, cy, fp["points"]):
                    continue
                candidates.append(i)
            if len(candidates) == 1:
                fp = footprints[candidates[0]]
                height = (rz1 - fp["z"]) * unit_to_m
                fp["height"] = max(fp["height"] or 0, height)
                fp["roof_matches"] += 1
                match_stats["unique_roof_matches"] += 1
            else:
                match_stats["unmatched" if not candidates else "ambiguous"] += 1
        fields = ["area", "perimeter", "width", "depth", "vertices", "compactness", "fill", "height",
                  "aligned_long", "aligned_short", "aligned_fill", "aligned_aspect"]
        distributions = {k: summary([fp[k] for fp in footprints]) for k in fields}
        azimuth_bins = collections.Counter(int(fp["azimuth"] // 10) * 10 for fp in footprints)
        examples = []
        if footprints:
            picks = sorted(footprints, key=lambda f: f["area"])
            chosen = [picks[0], picks[len(picks)//2], picks[-1]]
            with_height = [f for f in footprints if f["height"] is not None]
            if with_height:
                chosen.append(max(with_height, key=lambda f: f["height"]))
            for fp in chosen:
                examples.append({k: v for k, v in fp.items() if k != "points"})
        height_path = os.path.join(OUT, filename[:-4] + "-height-proxies.csv")
        with open(height_path, "w", newline="", encoding="utf-8") as f:
            w = csv.writer(f)
            w.writerow(["footprint_guid", "matched_roof_height_proxy_m", "roof_surface_matches"])
            w.writerows((fp["id"], fp["height"], fp["roof_matches"]) for fp in footprints)
        # Cross-check the local-origin shoelace areas against Rhino's independent area engine.
        crosschecks = []
        fp_by_id = {fp["id"]: fp for fp in footprints}
        checked = 0
        for obj in model.Objects:
            if checked >= 8:
                break
            fp = fp_by_id.get(str(obj.Attributes.ObjectId))
            if not fp:
                continue
            am = G.AreaMassProperties.Compute(obj.Geometry)
            if am:
                native_area = am.Area * unit_to_m ** 2
                delta = abs(native_area - fp["area"]) / max(native_area, 1e-15)
                crosschecks.append({"guid": fp["id"], "shoelace_m2": fp["area"], "rhino_m2": native_area, "relative_error": delta})
                am.Dispose()
                checked += 1
        notes = model.Notes.Notes if model.Notes else ""
        try:
            created = str(model.Created)
            date_error = None
        except Exception as exc:
            created = None
            date_error = str(exc).splitlines()[0]
        report = {"schema_version": 2, "file": filename, "source_path": path, "bytes": before.st_size,
                  "mtime_ns": before.st_mtime_ns, "archive_version": model.ArchiveVersion,
                  "application": model.ApplicationName, "application_details": model.ApplicationDetails,
                  "created_by": model.CreatedBy, "created": created, "created_date_error": date_error, "revision": model.Revision,
                  "units": str(model.Settings.ModelUnitSystem), "unit_to_m": unit_to_m,
                  "absolute_tolerance_source_units": model.Settings.ModelAbsoluteTolerance,
                  "relative_tolerance": model.Settings.ModelRelativeTolerance,
                  "angle_tolerance_degrees": model.Settings.ModelAngleToleranceDegrees,
                  "basepoint": vec(model.Settings.ModelBasepoint), "notes": notes,
                  "object_count": n, "geometry_types": dict(type_counts), "modes": dict(modes),
                  "context_guid_signature": hashlib.sha256("\n".join(sorted(context_ids)).encode()).hexdigest(),
                  "context_object_count": len(context_ids),
                  "named_objects_by_layer": dict(named), "invalid_bounds": invalid_bounds,
                  "layers": layer_results, "footprint_count": len(footprints),
                  "footprint_exclusions": dict(fp_exclusions), "distributions": distributions,
                  "longest_edge_azimuth_10degree_bins": dict(sorted(azimuth_bins.items())),
                  "roof_matching": dict(match_stats), "footprint_examples": examples,
                  "area_crosschecks": crosschecks, "early_building_attribute_samples": sample_attrs,
                  "read_seconds": round(read_seconds, 3), "census_seconds": round(time.time()-started, 3)}
    finally:
        model.Dispose()
        gc.collect()
        System.GC.Collect()
        System.GC.WaitForPendingFinalizers()
    progress(phase="source SHA256", objects_processed=n)
    digest = hashlib.sha256()
    with open(path, "rb") as source:
        for block in iter(lambda: source.read(8 * 1024 * 1024), b""):
            digest.update(block)
    after = os.stat(path)
    report["sha256"] = digest.hexdigest()
    report["source_unchanged_during_read"] = (before.st_size, before.st_mtime_ns) == (after.st_size, after.st_mtime_ns)
    report["total_seconds"] = round(time.time()-started, 3)
    save_json(os.path.join(OUT, filename[:-4] + ".json"), report)
    return {k: report[k] for k in ["file", "bytes", "sha256", "object_count", "footprint_count", "units", "total_seconds", "source_unchanged_during_read"]}


def run():
    try:
        test_helpers()
        files = sorted(os.path.join(SOURCE, n) for n in os.listdir(SOURCE) if n.lower().endswith(".3dm"))
        # Small files first establish schema quickly and limit expensive work before errors are found.
        files.sort(key=lambda p: (os.path.getsize(p), p))
        initial = [{"file": os.path.basename(p), "bytes": os.stat(p).st_size,
                    "mtime_ns": os.stat(p).st_mtime_ns} for p in files]
        save_json(os.path.join(OUT, "inventory.json"), initial)
        results = []
        errors = []
        progress(status="running", started_utc=time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
                 rhino_version=str(Rhino.RhinoApp.Version), sample_per_layer=SAMPLE_PER_LAYER,
                 total=len(files), completed=0, source=SOURCE)
        for i, path in enumerate(files, 1):
            if STATE.get("stop_requested"):
                progress(status="stopped")
                return
            try:
                results.append(inspect_file(path, i, len(files)))
            except Exception:
                errors.append({"file": os.path.basename(path), "error": traceback.format_exc()})
            save_json(os.path.join(OUT, "results.json"), {"files": results, "errors": errors})
            progress(completed=i, errors=len(errors))
        progress(status="complete", phase="finished", current=None, errors=len(errors))
    except Exception:
        progress(status="failed", error=traceback.format_exc())


def start():
    if STATE.get("status") in ("running", "normalizing"):
        return dict(STATE)
    if not os.path.isdir(SOURCE):
        raise ValueError("Set SOURCE to an extracted folder containing .3dm files")
    if not any(n.lower().endswith(".3dm") for n in os.listdir(SOURCE)):
        raise ValueError("No .3dm files in SOURCE (subfolders are not traversed)")
    source_root, output_root = os.path.realpath(SOURCE), os.path.realpath(OUT)
    if os.path.splitdrive(source_root)[0].lower() == os.path.splitdrive(output_root)[0].lower() and os.path.commonpath([source_root, output_root]) == source_root:
        raise ValueError("Choose OUT outside the source model folder")
    if os.path.exists(os.path.join(OUT, "inventory.json")):
        raise ValueError("OUT already contains a census; choose a fresh output folder")
    os.makedirs(OUT, exist_ok=True)
    thread = threading.Thread(target=run, name="NYC-file3dm-read-only-census", daemon=True)
    thread.start()
    return {"started": True, "source": SOURCE, "output": OUT}


if __name__ == "__main__":
    print(start())
