"""Compare Dublin cycle parking records with the first row at each coordinate."""

from collections import Counter, defaultdict
from csv import DictReader
from decimal import Decimal
from pathlib import Path
from sys import argv

source = Path(argv[1]) if len(argv) > 1 else Path("dublin-cycle-parking-source.csv")
with source.open(encoding="utf-8-sig", newline="") as handle:
    records = list(DictReader(handle))

types_by_coordinate = defaultdict(list)
for record in records:
    coordinate = (Decimal(record["Latitude"]), Decimal(record["Longitude"]))
    types_by_coordinate[coordinate].append(record["type_stands"])

original_counts = Counter(record["type_stands"] for record in records)
kept_counts = Counter(types[0] for types in types_by_coordinate.values())
shared_coordinates = sum(len(types) > 1 for types in types_by_coordinate.values())
mixed_coordinates = sum(len(set(types)) > 1 for types in types_by_coordinate.values())

print(f"Source records: {len(records):,}")
print(f"Exact coordinate pairs: {len(types_by_coordinate):,}")
print(f"Pairs used by multiple records: {shared_coordinates}")
print(f"Pairs used by multiple parking types: {mixed_coordinates}")
print("Parking type: original -> first per coordinate pair")
for stand_type, count in sorted(original_counts.items()):
    print(f"{stand_type}: {count} -> {kept_counts[stand_type]}")
