#!/usr/bin/env python3
"""Merge batches 1-7 + batch-8/user-*.csv into all-users.csv."""
import csv
import glob
import os

BASE = r"C:\claudetools\clients\cascades-tucson\reports\user-detail-batches"
OUT = os.path.join(BASE, "all-users.csv")

sources = []
for i in range(1, 8):
    sources.append(os.path.join(BASE, f"batch-{i}.csv"))
sources.extend(sorted(glob.glob(os.path.join(BASE, "batch-8", "user-*.csv"))))

fieldnames = None
rows = []
seen_upns = set()

for src in sources:
    with open(src, "r", encoding="utf-8", newline="") as f:
        reader = csv.DictReader(f)
        if fieldnames is None:
            fieldnames = reader.fieldnames
        for row in reader:
            upn_key = (row.get("UPN") or "").lower()
            if upn_key in seen_upns:
                print(f"  SKIP duplicate: {upn_key} (from {os.path.basename(src)})")
                continue
            seen_upns.add(upn_key)
            rows.append(row)

with open(OUT, "w", encoding="utf-8", newline="") as f:
    w = csv.DictWriter(f, fieldnames=fieldnames, quoting=csv.QUOTE_MINIMAL)
    w.writeheader()
    for r in rows:
        w.writerow(r)

print(f"\nWrote {len(rows)} rows from {len(sources)} source files -> {OUT}")
